88use Pterodactyl \Jobs \Schedule \RunTaskJob ;
99use Illuminate \Database \ConnectionInterface ;
1010use Pterodactyl \Exceptions \DisplayException ;
11+ use Pterodactyl \Repositories \Wings \DaemonServerRepository ;
12+ use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
1113
1214class ProcessScheduleService
1315{
@@ -21,13 +23,19 @@ class ProcessScheduleService
2123 */
2224 private $ connection ;
2325
26+ /**
27+ * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
28+ */
29+ private $ serverRepository ;
30+
2431 /**
2532 * ProcessScheduleService constructor.
2633 */
27- public function __construct (ConnectionInterface $ connection , Dispatcher $ dispatcher )
34+ public function __construct (ConnectionInterface $ connection , DaemonServerRepository $ serverRepository , Dispatcher $ dispatcher )
2835 {
2936 $ this ->dispatcher = $ dispatcher ;
3037 $ this ->connection = $ connection ;
38+ $ this ->serverRepository = $ serverRepository ;
3139 }
3240
3341 /**
@@ -38,7 +46,7 @@ public function __construct(ConnectionInterface $connection, Dispatcher $dispatc
3846 public function handle (Schedule $ schedule , bool $ now = false )
3947 {
4048 /** @var \Pterodactyl\Models\Task $task */
41- $ task = $ schedule ->tasks ()->orderBy ('sequence_id ' , ' asc ' )->first ();
49+ $ task = $ schedule ->tasks ()->orderBy ('sequence_id ' )->first ();
4250
4351 if (is_null ($ task )) {
4452 throw new DisplayException ('Cannot process schedule for task execution: no tasks are registered. ' );
@@ -54,6 +62,30 @@ public function handle(Schedule $schedule, bool $now = false)
5462 });
5563
5664 $ job = new RunTaskJob ($ task , $ now );
65+ if ($ schedule ->only_when_online ) {
66+ // Check that the server is currently in a starting or running state before executing
67+ // this schedule if this option has been set.
68+ try {
69+ $ details = $ this ->serverRepository ->setServer ($ schedule ->server )->getDetails ();
70+ $ state = $ details ['state ' ] ?? 'offline ' ;
71+ // If the server is stopping or offline just do nothing with this task.
72+ if (in_array ($ state , ['offline ' , 'stopping ' ])) {
73+ $ job ->failed ();
74+
75+ return ;
76+ }
77+ } catch (Exception $ exception ) {
78+ if (!$ exception instanceof DaemonConnectionException) {
79+ // If we encountered some exception during this process that wasn't just an
80+ // issue connecting to Wings run the failed sequence for a job. Otherwise we
81+ // can just quietly mark the task as completed without actually running anything.
82+ $ job ->failed ($ exception );
83+ }
84+ $ job ->failed ();
85+
86+ return ;
87+ }
88+ }
5789
5890 if (!$ now ) {
5991 $ this ->dispatcher ->dispatch ($ job ->delay ($ task ->time_offset ));
0 commit comments