forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunTaskJob.php
More file actions
139 lines (121 loc) · 4.23 KB
/
RunTaskJob.php
File metadata and controls
139 lines (121 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Pterodactyl\Jobs\Schedule;
use Exception;
use Pterodactyl\Jobs\Job;
use Carbon\CarbonImmutable;
use Pterodactyl\Models\Task;
use InvalidArgumentException;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
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
{
use DispatchesJobs;
use InteractsWithQueue;
use SerializesModels;
/**
* @var \Pterodactyl\Models\Task
*/
public $task;
/**
* @var bool
*/
public $manualRun;
/**
* RunTaskJob constructor.
*/
public function __construct(Task $task, $manualRun = false)
{
$this->queue = config('pterodactyl.queues.standard');
$this->task = $task;
$this->manualRun = $manualRun;
}
/**
* Run the job and send actions to the daemon running the server.
*
* @throws \Throwable
*/
public function handle(
DaemonCommandRepository $commandRepository,
InitiateBackupService $backupService,
DaemonPowerRepository $powerRepository
) {
// Do not process a task that is not set to active, unless it's been manually triggered.
if (!$this->task->schedule->is_active && !$this->manualRun) {
$this->markTaskNotQueued();
$this->markScheduleComplete();
return;
}
$server = $this->task->server;
// Perform the provided task against the daemon.
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('Invalid task action provided: ' . $this->task->action);
}
} catch (Exception $exception) {
// If this isn't a DaemonConnectionException on a task that allows for failures
// throw the exception back up the chain so that the task is stopped.
if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {
throw $exception;
}
}
$this->markTaskNotQueued();
$this->queueNextTask();
}
/**
* Handle a failure while sending the action to the daemon or otherwise processing the job.
*/
public function failed(Exception $exception = null)
{
$this->markTaskNotQueued();
$this->markScheduleComplete();
}
/**
* Get the next task in the schedule and queue it for running after the defined period of wait time.
*/
private function queueNextTask()
{
/** @var \Pterodactyl\Models\Task|null $nextTask */
$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)
->where('sequence_id', $this->task->sequence_id + 1)
->first();
if (is_null($nextTask)) {
$this->markScheduleComplete();
return;
}
$nextTask->update(['is_queued' => true]);
$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));
}
/**
* Marks the parent schedule as being complete.
*/
private function markScheduleComplete()
{
$this->task->schedule()->update([
'is_processing' => false,
'last_run_at' => CarbonImmutable::now()->toDateTimeString(),
]);
}
/**
* Mark a specific task as no longer being queued.
*/
private function markTaskNotQueued()
{
$this->task->update(['is_queued' => false]);
}
}