33namespace Pterodactyl \Jobs \Schedule ;
44
55use Exception ;
6- use Carbon \Carbon ;
76use Pterodactyl \Jobs \Job ;
7+ use Carbon \CarbonImmutable ;
8+ use Pterodactyl \Models \Task ;
89use InvalidArgumentException ;
9- use Illuminate \Container \Container ;
1010use Illuminate \Queue \SerializesModels ;
1111use Illuminate \Queue \InteractsWithQueue ;
1212use Illuminate \Contracts \Queue \ShouldQueue ;
1515use Pterodactyl \Services \Backups \InitiateBackupService ;
1616use Pterodactyl \Repositories \Wings \DaemonPowerRepository ;
1717use Pterodactyl \Repositories \Wings \DaemonCommandRepository ;
18- use Pterodactyl \Contracts \Repository \TaskRepositoryInterface ;
19- use Pterodactyl \Contracts \Repository \ScheduleRepositoryInterface ;
2018
2119class RunTaskJob extends Job implements ShouldQueue
2220{
2321 use DispatchesJobs, InteractsWithQueue, SerializesModels;
2422
2523 /**
26- * @var int
27- */
28- public $ schedule ;
29-
30- /**
31- * @var int
24+ * @var \Pterodactyl\Models\Task
3225 */
3326 public $ task ;
3427
35- /**
36- * @var \Pterodactyl\Repositories\Eloquent\TaskRepository
37- */
38- protected $ taskRepository ;
39-
4028 /**
4129 * RunTaskJob constructor.
4230 *
43- * @param int $task
44- * @param int $schedule
31+ * @param \Pterodactyl\Models\Task $task
4532 */
46- public function __construct (int $ task, int $ schedule )
33+ public function __construct (Task $ task )
4734 {
4835 $ this ->queue = config ('pterodactyl.queues.standard ' );
4936 $ this ->task = $ task ;
50- $ this ->schedule = $ schedule ;
5137 }
5238
5339 /**
@@ -58,7 +44,6 @@ public function __construct(int $task, int $schedule)
5844 * @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
5945 * @param \Pterodactyl\Repositories\Eloquent\TaskRepository $taskRepository
6046 *
61- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
6247 * @throws \Throwable
6348 */
6449 public function handle (
@@ -67,36 +52,32 @@ public function handle(
6752 DaemonPowerRepository $ powerRepository ,
6853 TaskRepository $ taskRepository
6954 ) {
70- $ this ->taskRepository = $ taskRepository ;
71-
72- $ task = $ this ->taskRepository ->getTaskForJobProcess ($ this ->task );
73- $ server = $ task ->getRelation ('server ' );
74-
7555 // Do not process a task that is not set to active.
76- if (! $ task ->getRelation ( ' schedule ' ) ->is_active ) {
56+ if (! $ this -> task ->schedule ->is_active ) {
7757 $ this ->markTaskNotQueued ();
7858 $ this ->markScheduleComplete ();
7959
8060 return ;
8161 }
8262
63+ $ server = $ this ->task ->server ;
8364 // Perform the provided task against the daemon.
84- switch ($ task ->action ) {
65+ switch ($ this -> task ->action ) {
8566 case 'power ' :
86- $ powerRepository ->setServer ($ server )->send ($ task ->payload );
67+ $ powerRepository ->setServer ($ server )->send ($ this -> task ->payload );
8768 break ;
8869 case 'command ' :
89- $ commandRepository ->setServer ($ server )->send ($ task ->payload );
70+ $ commandRepository ->setServer ($ server )->send ($ this -> task ->payload );
9071 break ;
9172 case 'backup ' :
92- $ backupService ->setIgnoredFiles (explode (PHP_EOL , $ task ->payload ))->handle ($ server , null );
73+ $ backupService ->setIgnoredFiles (explode (PHP_EOL , $ this -> task ->payload ))->handle ($ server , null );
9374 break ;
9475 default :
9576 throw new InvalidArgumentException ('Cannot run a task that points to a non-existent action. ' );
9677 }
9778
9879 $ this ->markTaskNotQueued ();
99- $ this ->queueNextTask ($ task -> sequence_id );
80+ $ this ->queueNextTask ();
10081 }
10182
10283 /**
@@ -112,46 +93,41 @@ public function failed(Exception $exception = null)
11293
11394 /**
11495 * Get the next task in the schedule and queue it for running after the defined period of wait time.
115- *
116- * @param int $sequence
117- *
118- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
119- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
12096 */
121- private function queueNextTask ($ sequence )
97+ private function queueNextTask ()
12298 {
123- $ nextTask = $ this ->taskRepository ->getNextTask ($ this ->schedule , $ sequence );
99+ /** @var \Pterodactyl\Models\Task|null $nextTask */
100+ $ nextTask = Task::query ()->where ('schedule_id ' , $ this ->task ->schedule_id )
101+ ->where ('sequence_id ' , $ this ->task ->sequence_id + 1 )
102+ ->first ();
103+
124104 if (is_null ($ nextTask )) {
125105 $ this ->markScheduleComplete ();
126106
127107 return ;
128108 }
129109
130- $ this ->taskRepository ->update ($ nextTask ->id , ['is_queued ' => true ]);
131- $ this ->dispatch ((new self ($ nextTask ->id , $ this ->schedule ))->delay ($ nextTask ->time_offset ));
110+ $ nextTask ->update (['is_queued ' => true ]);
111+
112+ $ this ->dispatch ((new self ($ nextTask ))->delay ($ nextTask ->time_offset ));
132113 }
133114
134115 /**
135116 * Marks the parent schedule as being complete.
136117 */
137118 private function markScheduleComplete ()
138119 {
139- Container::getInstance ()
140- ->make (ScheduleRepositoryInterface::class)
141- ->withoutFreshModel ()
142- ->update ($ this ->schedule , [
143- 'is_processing ' => false ,
144- 'last_run_at ' => Carbon::now ()->toDateTimeString (),
145- ]);
120+ $ this ->task ->schedule ()->update ([
121+ 'is_processing ' => false ,
122+ 'last_run_at ' => CarbonImmutable::now ()->toDateTimeString (),
123+ ]);
146124 }
147125
148126 /**
149127 * Mark a specific task as no longer being queued.
150128 */
151129 private function markTaskNotQueued ()
152130 {
153- Container::getInstance ()
154- ->make (TaskRepositoryInterface::class)
155- ->update ($ this ->task , ['is_queued ' => false ]);
131+ $ this ->task ->update (['is_queued ' => false ]);
156132 }
157133}
0 commit comments