forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessScheduleService.php
More file actions
60 lines (51 loc) · 1.63 KB
/
ProcessScheduleService.php
File metadata and controls
60 lines (51 loc) · 1.63 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
<?php
namespace Pterodactyl\Services\Schedules;
use Pterodactyl\Models\Schedule;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Illuminate\Database\ConnectionInterface;
class ProcessScheduleService
{
/**
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
private $dispatcher;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* ProcessScheduleService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
*/
public function __construct(ConnectionInterface $connection, Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
$this->connection = $connection;
}
/**
* Process a schedule and push the first task onto the queue worker.
*
* @param \Pterodactyl\Models\Schedule $schedule
* @param bool $now
*
* @throws \Throwable
*/
public function handle(Schedule $schedule, bool $now = false)
{
/** @var \Pterodactyl\Models\Task $task */
$task = $schedule->tasks()->where('sequence_id', 1)->firstOrFail();
$this->connection->transaction(function () use ($schedule, $task) {
$schedule->forceFill([
'is_processing' => true,
'next_run_at' => $schedule->getNextRunDate(),
])->saveOrFail();
$task->update(['is_queued' => true]);
});
$this->dispatcher->{$now ? 'dispatchNow' : 'dispatch'}(
(new RunTaskJob($task))->delay($task->time_offset)
);
}
}