Add underlying data changes necessary for new task & schedule features

This commit is contained in:
Dane Everitt 2021-05-01 10:44:40 -07:00
parent cf1ac04e39
commit 92cd659db3
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
13 changed files with 201 additions and 107 deletions

View file

@ -16,7 +16,6 @@ use Pterodactyl\Services\Schedules\ProcessScheduleService;
use Pterodactyl\Transformers\Api\Client\ScheduleTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\ViewScheduleRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\StoreScheduleRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\DeleteScheduleRequest;
@ -81,6 +80,7 @@ class ScheduleController extends ClientApiController
'cron_hour' => $request->input('hour'),
'cron_minute' => $request->input('minute'),
'is_active' => (bool) $request->input('is_active'),
'only_when_online' => (bool) $request->input('only_when_online'),
'next_run_at' => $this->getNextRunAt($request),
]);
@ -128,6 +128,7 @@ class ScheduleController extends ClientApiController
'cron_hour' => $request->input('hour'),
'cron_minute' => $request->input('minute'),
'is_active' => $active,
'only_when_online' => (bool) $request->input('only_when_online'),
'next_run_at' => $this->getNextRunAt($request),
];

View file

@ -59,6 +59,7 @@ class ScheduleTaskController extends ClientApiController
'action' => $request->input('action'),
'payload' => $request->input('payload') ?? '',
'time_offset' => $request->input('time_offset'),
'continue_on_failure' => (bool) $request->input('continue_on_failure'),
]);
return $this->fractal->item($task)
@ -84,6 +85,7 @@ class ScheduleTaskController extends ClientApiController
'action' => $request->input('action'),
'payload' => $request->input('payload') ?? '',
'time_offset' => $request->input('time_offset'),
'continue_on_failure' => (bool) $request->input('continue_on_failure'),
]);
return $this->fractal->item($task->refresh())

View file

@ -7,6 +7,7 @@ use Pterodactyl\Jobs\Job;
use Carbon\CarbonImmutable;
use Pterodactyl\Models\Task;
use InvalidArgumentException;
use Illuminate\Http\Response;
use Pterodactyl\Models\Schedule;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
@ -15,6 +16,7 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
use Pterodactyl\Services\Backups\InitiateBackupService;
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class RunTaskJob extends Job implements ShouldQueue
{
@ -62,18 +64,32 @@ class RunTaskJob extends Job implements ShouldQueue
$server = $this->task->server;
// Perform the provided task against the daemon.
switch ($this->task->action) {
case 'power':
$powerRepository->setServer($server)->send($this->task->payload);
break;
case 'command':
$commandRepository->setServer($server)->send($this->task->payload);
break;
case 'backup':
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
break;
default:
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
try {
switch ($this->task->action) {
case Task::ACTION_POWER:
$powerRepository->setServer($server)->send($this->task->payload);
break;
case Task::ACTION_COMMAND:
$commandRepository->setServer($server)->send($this->task->payload);
break;
case Task::ACTION_BACKUP:
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
break;
default:
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
}
} catch (Exception $exception) {
if ($exception instanceof DaemonConnectionException) {
// If the task "failed" because the server is offline and it was sending a command or
// executing a power action (which shouldn't happen?) then just stop trying to process
// the schedule, but don't actually log the failure.
if ($this->task->action === Task::ACTION_POWER || $this->task->action === Task::ACTION_COMMAND) {
// Do the thing
if ($exception->getStatusCode() === Response::HTTP_CONFLICT) {
}
}
}
throw $exception;
}
$this->markTaskNotQueued();

View file

@ -18,6 +18,7 @@ use Pterodactyl\Contracts\Extensions\HashidsInterface;
* @property string $cron_minute
* @property bool $is_active
* @property bool $is_processing
* @property bool $only_when_online
* @property \Carbon\Carbon|null $last_run_at
* @property \Carbon\Carbon|null $next_run_at
* @property \Carbon\Carbon $created_at
@ -63,6 +64,7 @@ class Schedule extends Model
'cron_minute',
'is_active',
'is_processing',
'only_when_online',
'last_run_at',
'next_run_at',
];
@ -75,6 +77,7 @@ class Schedule extends Model
'server_id' => 'integer',
'is_active' => 'boolean',
'is_processing' => 'boolean',
'only_when_online' => 'boolean',
];
/**
@ -99,6 +102,7 @@ class Schedule extends Model
'cron_minute' => '*',
'is_active' => true,
'is_processing' => false,
'only_when_online' => false,
];
/**
@ -114,6 +118,7 @@ class Schedule extends Model
'cron_minute' => 'required|string',
'is_active' => 'boolean',
'is_processing' => 'boolean',
'only_when_online' => 'boolean',
'last_run_at' => 'nullable|date',
'next_run_at' => 'nullable|date',
];
@ -122,6 +127,7 @@ class Schedule extends Model
* Returns the schedule's execution crontab entry as a string.
*
* @return \Carbon\CarbonImmutable
*
* @throws \Exception
*/
public function getNextRunDate()

View file

@ -14,6 +14,7 @@ use Pterodactyl\Contracts\Extensions\HashidsInterface;
* @property string $payload
* @property int $time_offset
* @property bool $is_queued
* @property bool $continue_on_failure
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $hashid
@ -30,6 +31,13 @@ class Task extends Model
*/
public const RESOURCE_NAME = 'schedule_task';
/**
* The default actions that can exist for a task in Pterodactyl.
*/
public const ACTION_POWER = 'power';
public const ACTION_COMMAND = 'command';
public const ACTION_BACKUP = 'backup';
/**
* The table associated with the model.
*
@ -56,6 +64,7 @@ class Task extends Model
'payload',
'time_offset',
'is_queued',
'continue_on_failure',
];
/**
@ -69,6 +78,7 @@ class Task extends Model
'sequence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
];
/**
@ -79,6 +89,7 @@ class Task extends Model
protected $attributes = [
'time_offset' => 0,
'is_queued' => false,
'continue_on_failure' => false,
];
/**
@ -91,6 +102,7 @@ class Task extends Model
'payload' => 'required_unless:action,backup|string',
'time_offset' => 'required|numeric|between:0,900',
'is_queued' => 'boolean',
'continue_on_failure' => 'boolean',
];
/**

View file

@ -45,6 +45,7 @@ class ScheduleTransformer extends BaseClientTransformer
],
'is_active' => $model->is_active,
'is_processing' => $model->is_processing,
'only_when_online' => $model->only_when_online,
'last_run_at' => $model->last_run_at ? $model->last_run_at->toIso8601String() : null,
'next_run_at' => $model->next_run_at ? $model->next_run_at->toIso8601String() : null,
'created_at' => $model->created_at->toIso8601String(),

View file

@ -28,6 +28,7 @@ class TaskTransformer extends BaseClientTransformer
'payload' => $model->payload,
'time_offset' => $model->time_offset,
'is_queued' => $model->is_queued,
'continue_on_failure' => $model->continue_on_failure,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];