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 ;
119
1210class ProcessScheduleService
1311{
@@ -17,62 +15,45 @@ class ProcessScheduleService
1715 private $ dispatcher ;
1816
1917 /**
20- * @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
18+ * @var \Illuminate\Database\ConnectionInterface
2119 */
22- private $ scheduleRepository ;
23-
24- /**
25- * @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
26- */
27- private $ taskRepository ;
20+ private $ connection ;
2821
2922 /**
3023 * ProcessScheduleService constructor.
3124 *
25+ * @param \Illuminate\Database\ConnectionInterface $connection
3226 * @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
33- * @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $scheduleRepository
34- * @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
3527 */
36- public function __construct (
37- Dispatcher $ dispatcher ,
38- ScheduleRepositoryInterface $ scheduleRepository ,
39- TaskRepositoryInterface $ taskRepository
40- ) {
28+ public function __construct (ConnectionInterface $ connection , Dispatcher $ dispatcher )
29+ {
4130 $ this ->dispatcher = $ dispatcher ;
42- $ this ->scheduleRepository = $ scheduleRepository ;
43- $ this ->taskRepository = $ taskRepository ;
31+ $ this ->connection = $ connection ;
4432 }
4533
4634 /**
4735 * Process a schedule and push the first task onto the queue worker.
4836 *
4937 * @param \Pterodactyl\Models\Schedule $schedule
38+ * @param bool $now
5039 *
51- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
52- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
40+ * @throws \Throwable
5341 */
54- public function handle (Schedule $ schedule )
42+ public function handle (Schedule $ schedule, bool $ now = false )
5543 {
56- $ this ->scheduleRepository ->loadTasks ($ schedule );
57-
5844 /** @var \Pterodactyl\Models\Task $task */
59- $ task = $ schedule ->getRelation ('tasks ' )->where ('sequence_id ' , 1 )->first ();
60-
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- );
45+ $ task = $ schedule ->tasks ()->where ('sequence_id ' , 1 )->firstOrFail ();
6746
68- $ this ->scheduleRepository ->update ($ schedule ->id , [
69- 'is_processing ' => true ,
70- 'next_run_at ' => CronExpression::factory ($ formattedCron )->getNextRunDate (),
71- ]);
47+ $ this ->connection ->transaction (function () use ($ schedule , $ task ) {
48+ $ schedule ->forceFill ([
49+ 'is_processing ' => true ,
50+ 'next_run_at ' => $ schedule ->getNextRunDate (),
51+ ])->saveOrFail ();
7252
73- $ this ->taskRepository ->update ($ task ->id , ['is_queued ' => true ]);
53+ $ task ->update (['is_queued ' => true ]);
54+ });
7455
75- $ this ->dispatcher ->dispatch (
56+ $ this ->dispatcher ->{ $ now ? ' dispatchNow ' : ' dispatch ' } (
7657 (new RunTaskJob ($ task ))->delay ($ task ->time_offset )
7758 );
7859 }
0 commit comments