添加自定义字段Laravel排队的作业记录?

问题描述 投票:8回答:2

我有一个工作Laravel 5排队叫“SendMyEmail”使用“数据库”司机的工作类。数据库“工作”表是否正确充满了这样的派遣工作。

我想显示在网站上的这些工作,因此,我想他们正在建造时,在所谓的“名”这些工作记录的自定义字段添加和保存价值。 (我会通过这个名字作为参数传递给SendMyEmail类的构造函数。)

有谁知道如何做到这一点?

php laravel laravel-5
2个回答
6
投票

好了,所以你要保持排队/处理作业的历史记录,对吧。

没有用于自定义数据库字段的任何内置支持。

看到:

https://github.com/laravel/framework/blob/7212b1e9620c36bf806e444f6931cf5f379c68ff/src/Illuminate/Queue/DatabaseQueue.php#L170

http://i.imgur.com/nFciWi9.png

从我的理解这种行为是有意的,因为你真的不应该与原来的“工作”表一团糟。它被设计为工作无状态的。这意味着,工作记录被删除它已被处理之后。

如果你想保持跟踪你的工作(如历史)的,你可能只是创建一个新的雄辩模型,并把它传递给你的工作的构造。这是有用的,为了保持原有的工作和你的历史同步。


好吧,让我们开始编码,好吗?

创建通过输入一个新的迁移:

PHP的工匠制作:迁移create_jobs_history_table

现在打开迁移和添加下面的列类型。

数据库/迁移/ xyz_create_jobs_history_table:

<?php

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

class CreateJobsHistoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs_history', function(Blueprint $table) {

            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->string('job', 40);
            $table->integer('status')->default(0);
            $table->timestamps();

            if (Schema::hasColumn('users', 'id'))
            {
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            }

        });
    }

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

说明:

正如你可以看到,我们增加了三个新类型称为USER_ID,作业和状态。

user_ID的引用用户的实际ID。

该作业场只是工作的描述/名称。

状态字段表示状态。 0 =尚未处理连线,1 = DONE


现在,我们的迁移准备好了,让我们为它定义一个新的模式:

应用程序/ JobHistory.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class JobHistory extends Model
{
    protected $table = 'jobs_history';

    protected $hidden = [];

}

甜。现在,我们可以很容易地与我们在我们的应用程序作业历史交互。


现在是时候创建一个作业。让我们通过下面的代码这样做:

应用程序/工作/ ProvisionUser.php

<?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 App\User;
use App\JobHistory;

class ProvisonUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    protected $history;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user, JobHistory $history)
    {
        $this->user = $user;
        $this->history = $history;

        // Set up our new history record.

        $this->history->user_id = $this->user->id;
        $this->history->job = 'Provison User';
        $this->history->status = 0;

        $this->history->save();
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Do other stuff here....

        // Once the job has finished, set history status to 1.
        $this->history->status = 1;
        $this->history->save();
    }
}

说明:

在这里,我们包括在用户和JobHistory模型。在我们的构造函数中,我们需要两个模型,我们建立了一个新的历史纪录。

实际的工作现在正与我们的新的历史纪录同步!

好。

当正在处理的作业handle()函数被调用。在这里,我们将状态设置为1,一旦完成。

而最后只是派遣在你的控制器的工作:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;

use App\User;
use App\JobHistory;
use App\Jobs\ProvisionUser;

class SomeController extends Controller
{
    public function provision()
    {
        $user = User::find(1);

        $job = (new ProvisionUser($user, new JobHistory))
            ->delay(Carbon::now()->addMinutes(1));

        dispatch($job);

        return view('provision');
    }
}

说明:

我们通过现有的用户和新的工作历史上的构造。之后,我们派遣延迟工作。

注:延迟仅仅是示范目的。

打开你的数据库,并检查您jobs_history表。只要你的工作被派往,相应的历史记录的状态应该是0。一旦工匠队列工作已处理的作业,历史记录状态应为1。

我测试了这种设置与Laravel 5.4和我用同样的逻辑在我的应用程序。

编码愉快!


0
投票

请尝试失败的作业此代码。

 <?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 App\User;
    use App\JobHistory;

    class ProvisonUser implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

        protected $user;
        protected $history;
        protected $id;

        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct(User $user, JobHistory $history)
        {
            $this->user = $user;
            $this->history = $history;

            // Set up our new history record and get unique id
            $this->id = DB::table('jobs_history')->insertGetId([
                'user_id' => $this->user->id,
                'job' => 'Provison User',
                'status' => 0

            ]);

        }

        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            // Do other stuff here....

            try{
                $JobHistory = JobHistory::findOrfail($this->id);
                // Once the job has finished, set history status to 1.
                $JobHistory->status = 1;
                $JobHistory->save();
            }catch (\Exception $e){
                // failed job, set history status to 1.
                $JobHistory->status = 2;
                $JobHistory->save();
            }

        }
        public function failed()
        {
            $JobHistory = JobHistory::findOrfail($this->id);
            // failed job, set history status to 1.
            $JobHistory->status = 2;
            $JobHistory->save();
        }
    }

在我而言,这个代码工作。

© www.soinside.com 2019 - 2024. All rights reserved.