Add custom field to Laravel in job entry in queue?

I have a working class of Laravel 5 class called "SendMyEmail" using the database driver. The database jobs table is populated with such submitted jobs correctly.

I would like to show these tasks on the website, and therefore I would like to add and save a value in a custom field called "name" in these task entries when they are created. (I would pass this name as a parameter to the constructor of the SendMyEmail class ..)

Does anyone know how to do this?

+6
source share
2 answers

Good, so you want to keep the job history in the queue / processed jobs, right.

There is no built-in support for customizing database fields.

See:

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

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

As far as I understand, this behavior is intended because you should not be linked to the source table of "jobs". It was designed for stateless work. This means that the work record is deleted immediately after its processing.

If you want to keep track of your assignments (e.g. history), you can simply create a new Eloquent model and pass it to the assignment designer. This is useful for synchronizing the original work and your story.


Ok, let's start coding, right?

Create a new migration by typing:

php artisan does: create_jobs_history_table migration

Now open this migration and add the following column types.

database / migration / 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(); } } 

Explanation:

As you can see, we have added three new types: user_id , job and status .

User_id refers to the actual user id.

The task field is simply a description / title of the task.

The status field represents the status. 0 = not yet processed, 1 = completed


Now that our migration is ready, let's define a new model for it:

app / JobHistory.php

 <?php namespace App; use Illuminate\Database\Eloquent\Model; class JobHistory extends Model { protected $table = 'jobs_history'; protected $hidden = []; } 

Cute. Now we can easily interact with our work history in our application.


It is time to create a job. Let's do it with the following code:

app / Jobs / 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(); } } 

Explanation:

Here we have included the User and JobHistory models. In our constructor, we need both models, and we set a new history record.

Actual work is now synchronized with our new story!

Good.

The handle() function is called during job processing. Here we set status 1 as soon as it ends.

And finally, just submit the work to your controller:

 <?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'); } } 

Explanation:

We pass on the existing user and the new task history to the designer. After that we send the deferred work.

Note: the delay is for demonstration purposes only.

Open your database and check your jobs_history table. As soon as your task has been sent, the status of the corresponding history record should be 0. After the worker of the artisan queue has processed the task, the status of the history record should be 1.

I tested this setting with Laravel 5.4 and use the same logic in my application.

Good coding!

+5
source

Please try this code for unsuccessful work.

  <?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(); } } 

In my case, this code works.

0
source

Source: https://habr.com/ru/post/1232799/


All Articles