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

153
app/Models/Schedule.php Normal file
View file

@ -0,0 +1,153 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Schedule extends Model implements CleansAttributes, ValidableContract
{
use Eloquence, Validable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'schedules';
/**
* Mass assignable attributes on this model.
*
* @var array
*/
protected $fillable = [
'server_id',
'name',
'cron_day_of_week',
'cron_day_of_month',
'cron_hour',
'cron_minute',
'is_active',
'is_processing',
'last_run_at',
'next_run_at',
];
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'server_id' => 'integer',
'is_active' => 'boolean',
'is_processing' => 'boolean',
];
/**
* Columns to mutate to a date.
*
* @var array
*/
protected $dates = [
self::CREATED_AT,
self::UPDATED_AT,
'last_run_at',
'next_run_at',
];
/**
* @var array
*/
protected $attributes = [
'name' => null,
'cron_day_of_week' => '*',
'cron_day_of_month' => '*',
'cron_hour' => '*',
'cron_minute' => '*',
'is_active' => true,
'is_processing' => false,
];
/**
* @var array
*/
protected static $applicationRules = [
'server_id' => 'required',
'cron_day_of_week' => 'required',
'cron_day_of_month' => 'required',
'cron_hour' => 'required',
'cron_minute' => 'required',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'server_id' => 'exists:servers,id',
'name' => 'nullable|string|max:255',
'cron_day_of_week' => 'string',
'cron_day_of_month' => 'string',
'cron_hour' => 'string',
'cron_minute' => 'string',
'is_active' => 'boolean',
'is_processing' => 'boolean',
'last_run_at' => 'nullable|timestamp',
'next_run_at' => 'nullable|timestamp',
];
/**
* Return a hashid encoded string to represent the ID of the schedule.
*
* @return string
*/
public function getHashidAttribute()
{
return app()->make('hashids')->encode($this->id);
}
/**
* Return tasks belonging to a schedule.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class);
}
/**
* Return the server model that a schedule belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
}

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