Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Jobs/Schedule/RunTaskJob.php
14044 views
1
<?php
2
3
namespace Pterodactyl\Jobs\Schedule;
4
5
use Carbon\CarbonImmutable;
6
use Pterodactyl\Models\Task;
7
use Illuminate\Bus\Queueable;
8
use Illuminate\Queue\SerializesModels;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
use Illuminate\Foundation\Bus\DispatchesJobs;
11
use Pterodactyl\Services\Backups\InitiateBackupService;
12
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
13
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
14
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
15
16
class RunTaskJob implements ShouldQueue
17
{
18
use Queueable;
19
use DispatchesJobs;
20
use SerializesModels;
21
22
/**
23
* RunTaskJob constructor.
24
*/
25
public function __construct(public Task $task, public bool $manualRun = false)
26
{
27
$this->queue = 'standard';
28
}
29
30
/**
31
* Run the job and send actions to the daemon running the server.
32
*
33
* @throws \Throwable
34
*/
35
public function handle(
36
DaemonCommandRepository $commandRepository,
37
InitiateBackupService $backupService,
38
DaemonPowerRepository $powerRepository,
39
) {
40
// Do not process a task that is not set to active, unless it's been manually triggered.
41
if (!$this->task->schedule->is_active && !$this->manualRun) {
42
$this->markTaskNotQueued();
43
$this->markScheduleComplete();
44
45
return;
46
}
47
48
$server = $this->task->server;
49
// If we made it to this point and the server status is not null it means the
50
// server was likely suspended or marked as reinstalling after the schedule
51
// was queued up. Just end the task right now — this should be a very rare
52
// condition.
53
if (!is_null($server->status)) {
54
$this->failed();
55
56
return;
57
}
58
59
// Perform the provided task against the daemon.
60
try {
61
switch ($this->task->action) {
62
case Task::ACTION_POWER:
63
$powerRepository->setServer($server)->send($this->task->payload);
64
break;
65
case Task::ACTION_COMMAND:
66
$commandRepository->setServer($server)->send($this->task->payload);
67
break;
68
case Task::ACTION_BACKUP:
69
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
70
break;
71
default:
72
throw new \InvalidArgumentException('Invalid task action provided: ' . $this->task->action);
73
}
74
} catch (\Exception $exception) {
75
// If this isn't a DaemonConnectionException on a task that allows for failures
76
// throw the exception back up the chain so that the task is stopped.
77
if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {
78
throw $exception;
79
}
80
}
81
82
$this->markTaskNotQueued();
83
$this->queueNextTask();
84
}
85
86
/**
87
* Handle a failure while sending the action to the daemon or otherwise processing the job.
88
*/
89
public function failed(?\Exception $exception = null)
90
{
91
$this->markTaskNotQueued();
92
$this->markScheduleComplete();
93
}
94
95
/**
96
* Get the next task in the schedule and queue it for running after the defined period of wait time.
97
*/
98
private function queueNextTask()
99
{
100
/** @var Task|null $nextTask */
101
$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)
102
->orderBy('sequence_id', 'asc')
103
->where('sequence_id', '>', $this->task->sequence_id)
104
->first();
105
106
if (is_null($nextTask)) {
107
$this->markScheduleComplete();
108
109
return;
110
}
111
112
$nextTask->update(['is_queued' => true]);
113
114
$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));
115
}
116
117
/**
118
* Marks the parent schedule as being complete.
119
*/
120
private function markScheduleComplete()
121
{
122
$this->task->schedule()->update([
123
'is_processing' => false,
124
'last_run_at' => CarbonImmutable::now()->toDateTimeString(),
125
]);
126
}
127
128
/**
129
* Mark a specific task as no longer being queued.
130
*/
131
private function markTaskNotQueued()
132
{
133
$this->task->update(['is_queued' => false]);
134
}
135
}
136
137