22
33namespace Pterodactyl \Services \Schedules ;
44
5- use Cron \CronExpression ;
65use Pterodactyl \Models \Schedule ;
76use Illuminate \Contracts \Bus \Dispatcher ;
87use Pterodactyl \Jobs \Schedule \RunTaskJob ;
9- use Pterodactyl \ Contracts \ Repository \ TaskRepositoryInterface ;
10- use Pterodactyl \Contracts \ Repository \ ScheduleRepositoryInterface ;
8+ use Illuminate \ Database \ ConnectionInterface ;
9+ use Pterodactyl \Exceptions \ DisplayException ;
1110
1211class ProcessScheduleService
1312{
@@ -17,62 +16,51 @@ class ProcessScheduleService
1716 private $ dispatcher ;
1817
1918 /**
20- * @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
19+ * @var \Illuminate\Database\ConnectionInterface
2120 */
22- private $ scheduleRepository ;
23-
24- /**
25- * @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
26- */
27- private $ taskRepository ;
21+ private $ connection ;
2822
2923 /**
3024 * ProcessScheduleService constructor.
3125 *
26+ * @param \Illuminate\Database\ConnectionInterface $connection
3227 * @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
33- * @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $scheduleRepository
34- * @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
3528 */
36- public function __construct (
37- Dispatcher $ dispatcher ,
38- ScheduleRepositoryInterface $ scheduleRepository ,
39- TaskRepositoryInterface $ taskRepository
40- ) {
29+ public function __construct (ConnectionInterface $ connection , Dispatcher $ dispatcher )
30+ {
4131 $ this ->dispatcher = $ dispatcher ;
42- $ this ->scheduleRepository = $ scheduleRepository ;
43- $ this ->taskRepository = $ taskRepository ;
32+ $ this ->connection = $ connection ;
4433 }
4534
4635 /**
4736 * Process a schedule and push the first task onto the queue worker.
4837 *
4938 * @param \Pterodactyl\Models\Schedule $schedule
39+ * @param bool $now
5040 *
51- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
52- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
41+ * @throws \Throwable
5342 */
54- public function handle (Schedule $ schedule )
43+ public function handle (Schedule $ schedule, bool $ now = false )
5544 {
56- $ this ->scheduleRepository ->loadTasks ($ schedule );
57-
5845 /** @var \Pterodactyl\Models\Task $task */
59- $ task = $ schedule ->getRelation ( ' tasks ' )->where ('sequence_id ' , 1 )->first ();
46+ $ task = $ schedule ->tasks ( )->where ('sequence_id ' , 1 )->first ();
6047
61- $ formattedCron = sprintf ('%s %s %s * %s ' ,
62- $ schedule ->cron_minute ,
63- $ schedule ->cron_hour ,
64- $ schedule ->cron_day_of_month ,
65- $ schedule ->cron_day_of_week
66- );
48+ if (is_null ($ task )) {
49+ throw new DisplayException (
50+ 'Cannot process schedule for task execution: no tasks are registered. '
51+ );
52+ }
6753
68- $ this ->scheduleRepository ->update ($ schedule ->id , [
69- 'is_processing ' => true ,
70- 'next_run_at ' => CronExpression::factory ($ formattedCron )->getNextRunDate (),
71- ]);
54+ $ this ->connection ->transaction (function () use ($ schedule , $ task ) {
55+ $ schedule ->forceFill ([
56+ 'is_processing ' => true ,
57+ 'next_run_at ' => $ schedule ->getNextRunDate (),
58+ ])->saveOrFail ();
7259
73- $ this ->taskRepository ->update ($ task ->id , ['is_queued ' => true ]);
60+ $ task ->update (['is_queued ' => true ]);
61+ });
7462
75- $ this ->dispatcher ->dispatch (
63+ $ this ->dispatcher ->{ $ now ? ' dispatchNow ' : ' dispatch ' } (
7664 (new RunTaskJob ($ task ))->delay ($ task ->time_offset )
7765 );
7866 }
0 commit comments