Begin refactoring Tasks to be apart of the Scheduler system

This commit is contained in:
Dane Everitt 2017-09-12 23:45:19 -05:00
parent 07965d0ce7
commit 2ac90b50f2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
28 changed files with 902 additions and 468 deletions

View file

@ -42,11 +42,25 @@ class Task extends Model implements CleansAttributes, ValidableContract
protected $table = 'tasks';
/**
* Fields that are not mass assignable.
* Relationships to be updated when this model is updated.
*
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $touches = ['schedule'];
/**
* Fields that are mass assignable.
*
* @var array
*/
protected $fillable = [
'schedule_id',
'squence_id',
'action',
'payload',
'time_offset',
'is_queued',
];
/**
* Cast values to correct type.
@ -55,10 +69,10 @@ class Task extends Model implements CleansAttributes, ValidableContract
*/
protected $casts = [
'id' => 'integer',
'user_id' => 'integer',
'server_id' => 'integer',
'queued' => 'boolean',
'active' => 'boolean',
'schedule_id' => 'integer',
'squence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
];
/**
@ -67,54 +81,32 @@ class Task extends Model implements CleansAttributes, ValidableContract
* @var array
*/
protected $attributes = [
'parent_task_id' => null,
'chain_order' => null,
'active' => true,
'day_of_week' => '*',
'day_of_month' => '*',
'hour' => '*',
'minute' => '*',
'chain_delay' => null,
'queued' => false,
'is_queued' => false,
];
/**
* @var array
*/
protected static $applicationRules = [
'server_id' => 'required',
'schedule_id' => 'required',
'squence_id' => 'required',
'action' => 'required',
'data' => 'required',
'payload' => 'required',
'time_offset' => 'required',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'name' => 'nullable|string|max:255',
'parent_task_id' => 'nullable|numeric|exists:tasks,id',
'chain_order' => 'nullable|numeric|min:1',
'server_id' => 'numeric|exists:servers,id',
'active' => 'boolean',
'schedule_id' => 'numeric|exists:schedules,id',
'squence_id' => 'numeric|min:1',
'action' => 'string',
'data' => 'string',
'queued' => 'boolean',
'day_of_month' => 'string',
'day_of_week' => 'string',
'hour' => 'string',
'minute' => 'string',
'chain_delay' => 'nullable|numeric|between:1,900',
'last_run' => 'nullable|timestamp',
'next_run' => 'nullable|timestamp',
'payload' => 'string',
'time_offset' => 'numeric|between:0,900',
'is_queued' => 'boolean',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['last_run', 'next_run', 'created_at', 'updated_at'];
/**
* Return a hashid encoded string to represent the ID of the task.
*
@ -126,32 +118,26 @@ class Task extends Model implements CleansAttributes, ValidableContract
}
/**
* Gets the server associated with a task.
* Return the schedule that a task belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function schedule()
{
return $this->belongsTo(Schedule::class);
}
/**
* Return the server a task is assigned to, acts as a belongsToThrough.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
/**
* Gets the user associated with a task.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Return chained tasks for a parent task.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function chained()
{
return $this->hasMany(self::class, 'parent_task_id')->orderBy('chain_order', 'asc');
if ($schedule = $this->schedule) {
return $schedule->server();
} else {
throw new \InvalidArgumentException('Instance of Task must have an associated Schedule in the database.');
}
}
}