无法将口才模型传递给Laravel Job

时间:2018-12-22 03:30:28

标签: php laravel laravel-queue

我正在关注Laravel的文档,以将Eloquent模型传递给Laravel Job。但是,尽管我尽了最大努力,但我无法通过Eloquent模型-它始终是“未定义”或另一个奇怪的错误。

这是针对Laravel 5.7应用程序的,目前在MacOS / Darwin,MySQL 5.5和PHP 7.2上运行。此作业是从另一个作业启动的。我有一份工作,然后分派了许多子工作。

// From the Job itself
public function __construct(\App\Course $courseToDB)
{
    $this->courseToDB = $courseToDB;
}

public function handle(App\Course $courseToDB)
{        
    Log::info($this->courseToDB);
}
// From where I am dispatching the job
\App\Jobs\syncCourse::dispatch($courseToDB)->onConnection('database');

我希望,当我使用$courseToDB(一个\ App \ Course模型)调度该作业时,该作业将能够选择该模型,并对其进行处理。我的问题是,它不会将模型传递给工作。即使在最简单的记录方案中也尝试使用它失败。

更新:我的模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Course extends Model
{
    protected $fillable = ['id', 'name', 'category_id', 'teacher_id', 'moodle_id', 'summary'];

    public function teacher() {
        return $this->belongsTo('App\Teacher');
    }
    public function category() {
        return $this->belongsTo('App\Category');
    }
    public function views() {
        return $this->morphMany(
            \App\view::class,
            'viewable'
        );
    }
// A few other functions cut out (very simple one-liners)

}

更新:我的迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCoursesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('courses', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('moodle_id');
            $table->integer('teacher_id');
            $table->integer('category_id');
            $table->longText('summary')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('courses');
    }
}

更新:我的工作(syncCourse),即使不执行任何操作也会失败

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use App\Course;

class syncCourse implements ShouldQueue
{
    public $courseToDB;

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Course $courseToDB)
    {
        //\App\Course $courseToDB
        $this->courseToDB = $courseToDB;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {        

    }
}

更新:从终端(运行时)

[2018-12-23 01:51:28][3891] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3891] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3892] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3892] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3893] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3893] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3894] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3894] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3895] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3895] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3896] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3896] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3897] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3897] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3898] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3898] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3899] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3899] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3900] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3900] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3901] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3901] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3902] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3902] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3903] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3903] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3904] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3904] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3905] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3905] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3906] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3906] Failed:     App\Jobs\syncCourse
[2018-12-23 01:51:28][3907] Processing: App\Jobs\syncCourse
[2018-12-23 01:51:28][3907] Failed:     App\Jobs\syncCourse

在我的应用中,dispatchSync调度了数百个syncCourse并传递了一个雄辩的模型。传递Eloquent模型是很麻烦的,因为即使我绝对对传递的模型不做任何事情,它也会出错。 Laravel.log的错误跟踪为零。

更新2:运行“登录服务提供商”后

打开服务提供者的登录后,所有syncCourse事件都未完成。现在我的queue:work看起来像这样:

[2018-12-23 02:10:41][5829] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5830] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5831] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5832] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5833] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5834] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5835] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5836] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5837] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5838] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5839] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5840] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5841] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5842] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5843] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5844] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5845] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5846] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5847] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5848] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5849] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5850] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5851] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5852] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5853] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5854] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5855] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5856] Processing: App\Jobs\syncCourse
[2018-12-23 02:10:41][5857] Processing: App\Jobs\syncCourse

在日志中:

[2018-12-23 02:10:34] local.ERROR: database
[2018-12-23 02:10:34] local.ERROR: Object of class Illuminate\Queue\Jobs\DatabaseJob could not be converted to string {"exception":"[object] (ErrorException(code: 0): Object of class Illuminate\\Queue\\J

2 个答案:

答案 0 :(得分:0)

尝试如下使用

在类声明上方添加const example1 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo1'); }, 500); }); const example2 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo2'); }, 500); }); const doStuff = () => { const listExample = ['a','b','c']; return listExample.reduce((lastPromise, item) => ( lastPromise .then(() => console.log(item)) .then(example1) .then(() => console.log("Fisrt")) .then(example2) .then(() => console.log('Second')) ), Promise.resolve()) .then(() => console.log("The End")); }; doStuff();

您的方法内部使用use \App\Course;调用模型

在模型中,函数声明必须喜欢Course::function_name()

或分享您现在遇到的错误。

答案 1 :(得分:0)

确保调试出了问题的地方。这是complete reference的用法。

1。要为failed_jobs表创建迁移,可以使用queue:failed-table命令:

   php artisan queue:failed-table

   php artisan migrate
  1. handle()方法之后添加以下方法。

    public function failed(Exception $exception)
    {
       //Your expection
       dd($exception)
    }