Implemented job queue table and the gubbins around it

This commit is contained in:
2022-05-24 16:37:45 +01:00
parent fb24b2ee72
commit 6806291554
11 changed files with 439 additions and 78 deletions

View File

@@ -0,0 +1,60 @@
<?php
/*
* AVSDev UF Worker (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Worker\Database\Migrations\v100;
use Illuminate\Database\Schema\Blueprint;
use UserFrosting\Sprinkle\Core\Database\Migration;
/**
* Queued Jobs table migration
* Version 1.0.0.
*
* @author Craig Williams (https://avsdev.uk)
*/
class QueuedJobsTable extends Migration
{
/**
* {@inheritdoc}
*/
public static $dependencies = [
];
/**
* {@inheritdoc}
*/
public function up()
{
if (!$this->schema->hasTable('queued_jobs')) {
$this->schema->create('queued_jobs', function (Blueprint $table) {
$table->increments('id');
$table->string('job');
$table->json('params')->default('[]');
$table->boolean('flag_started')->default(false);
$table->boolean('flag_completed')->default(false);
$table->boolean('flag_failed')->default(false);
$table->timestamp('started_at')->nullable();
$table->timestamp('completed_at')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->collation = 'utf8_unicode_ci';
$table->charset = 'utf8';
});
}
}
/**
* {@inheritdoc}
*/
public function down()
{
$this->schema->drop('queued_jobs');
}
}

View File

@@ -0,0 +1,68 @@
<?php
/*
* AVSDev UF Worker (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Worker\Database\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Capsule\Manager as DB;
use UserFrosting\Sprinkle\Core\Database\Models\Model;
/**
* Queued Job class
*
* @author Craig Williams (https://avsdev.uk)
*/
class QueuedJob extends Model
{
/**
* @var string The name of the table for the current model.
*/
protected $table = 'queued_jobs';
/**
* Fields that should be mass-assignable when creating a new Organisation.
*
* @var string[]
*/
protected $fillable = [
'job',
'params',
];
/**
* The attributes that should be mutated to dates.
*
* @var string[]
*/
protected $dates = [
'created_at',
'updated_at',
'started_at',
'completed_at',
];
/**
* @var bool Enable timestamps for this class.
*/
public $timestamps = true;
/**
* Dequeues the current model as a job object
*/
public function dequeue()
{
$jobClass = $this->job;
$params = json_decode($this->params, true);
$job = new $jobClass();
$job->setParams($params);
return $job;
}
}