2020-10-15 06:17:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Services\Schedules;
|
|
|
|
|
2020-10-27 04:54:15 +01:00
|
|
|
use Exception;
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-10-15 06:17:57 +02:00
|
|
|
use Pterodactyl\Models\Task;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Illuminate\Support\Facades\Bus;
|
|
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
|
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
|
|
|
|
|
|
|
class ProcessScheduleServiceTest extends IntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test that a schedule with no tasks registered returns an error.
|
|
|
|
*/
|
|
|
|
public function testScheduleWithNoTasksReturnsException()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
2021-01-23 21:09:16 +01:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
2020-10-15 06:17:57 +02:00
|
|
|
|
|
|
|
$this->expectException(DisplayException::class);
|
|
|
|
$this->expectExceptionMessage('Cannot process schedule for task execution: no tasks are registered.');
|
|
|
|
|
|
|
|
$this->getService()->handle($schedule);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error during the schedule update is not persisted to the database.
|
|
|
|
*/
|
|
|
|
public function testErrorDuringScheduleDataUpdateDoesNotPersistChanges()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 21:09:16 +01:00
|
|
|
$schedule = Schedule::factory()->create([
|
2020-10-15 06:17:57 +02:00
|
|
|
'server_id' => $server->id,
|
|
|
|
'cron_minute' => 'hodor', // this will break the getNextRunDate() function.
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-01-23 21:09:16 +01:00
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 1]);
|
2020-10-15 06:17:57 +02:00
|
|
|
|
2023-02-23 20:30:16 +01:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2020-10-15 06:17:57 +02:00
|
|
|
|
|
|
|
$this->getService()->handle($schedule);
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('schedules', ['id' => $schedule->id, 'is_processing' => true]);
|
|
|
|
$this->assertDatabaseMissing('tasks', ['id' => $task->id, 'is_queued' => true]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a job is dispatched as expected using the initial delay.
|
|
|
|
*
|
|
|
|
* @dataProvider dispatchNowDataProvider
|
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function testJobCanBeDispatchedWithExpectedInitialDelay(bool $now)
|
2020-10-15 06:17:57 +02:00
|
|
|
{
|
2020-10-27 04:54:15 +01:00
|
|
|
Bus::fake();
|
2020-10-15 06:17:57 +02:00
|
|
|
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 21:09:16 +01:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
2020-10-15 06:17:57 +02:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-01-23 21:09:16 +01:00
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'time_offset' => 10, 'sequence_id' => 1]);
|
2020-10-15 06:17:57 +02:00
|
|
|
|
|
|
|
$this->getService()->handle($schedule, $now);
|
|
|
|
|
2020-10-27 04:54:15 +01:00
|
|
|
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($now, $task) {
|
|
|
|
$this->assertInstanceOf(RunTaskJob::class, $job);
|
|
|
|
$this->assertSame($task->id, $job->task->id);
|
|
|
|
// Jobs using dispatchNow should not have a delay associated with them.
|
|
|
|
$this->assertSame($now ? null : 10, $job->delay);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2020-10-15 06:17:57 +02:00
|
|
|
$this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]);
|
|
|
|
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]);
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:54:58 +02:00
|
|
|
/**
|
|
|
|
* Test that even if a schedule's task sequence gets messed up the first task based on
|
|
|
|
* the ascending order of tasks is used.
|
|
|
|
*
|
|
|
|
* @see https://github.com/pterodactyl/panel/issues/2534
|
|
|
|
*/
|
|
|
|
public function testFirstSequenceTaskIsFound()
|
|
|
|
{
|
2020-10-27 04:54:15 +01:00
|
|
|
Bus::fake();
|
2020-10-23 05:54:58 +02:00
|
|
|
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 21:09:16 +01:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
2020-10-23 05:54:58 +02:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-01-23 21:09:16 +01:00
|
|
|
$task2 = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 4]);
|
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 2]);
|
|
|
|
$task3 = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 3]);
|
2020-10-23 05:54:58 +02:00
|
|
|
|
|
|
|
$this->getService()->handle($schedule);
|
|
|
|
|
2021-01-23 21:09:16 +01:00
|
|
|
Bus::assertDispatched(RunTaskJob::class, function (RunTaskJob $job) use ($task) {
|
2020-10-27 04:54:15 +01:00
|
|
|
return $task->id === $job->task->id;
|
|
|
|
});
|
|
|
|
|
2020-10-23 05:54:58 +02:00
|
|
|
$this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]);
|
|
|
|
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]);
|
|
|
|
$this->assertDatabaseHas('tasks', ['id' => $task2->id, 'is_queued' => false]);
|
|
|
|
$this->assertDatabaseHas('tasks', ['id' => $task3->id, 'is_queued' => false]);
|
|
|
|
}
|
|
|
|
|
2020-10-27 04:54:15 +01:00
|
|
|
/**
|
|
|
|
* Tests that a task's processing state is reset correctly if using "dispatchNow" and there is
|
|
|
|
* an exception encountered while running it.
|
|
|
|
*
|
|
|
|
* @see https://github.com/pterodactyl/panel/issues/2550
|
|
|
|
*/
|
|
|
|
public function testTaskDispatchedNowIsResetProperlyIfErrorIsEncountered()
|
|
|
|
{
|
2023-02-23 20:30:16 +01:00
|
|
|
$this->swap(Dispatcher::class, $dispatcher = \Mockery::mock(Dispatcher::class));
|
2020-10-27 04:54:15 +01:00
|
|
|
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 21:09:16 +01:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id, 'last_run_at' => null]);
|
2020-10-27 04:54:15 +01:00
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-01-23 21:09:16 +01:00
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 1]);
|
2020-10-27 04:54:15 +01:00
|
|
|
|
2023-02-23 20:30:16 +01:00
|
|
|
$dispatcher->expects('dispatchNow')->andThrows(new \Exception('Test thrown exception'));
|
2020-10-27 04:54:15 +01:00
|
|
|
|
2023-02-23 20:30:16 +01:00
|
|
|
$this->expectException(\Exception::class);
|
2020-10-27 04:54:15 +01:00
|
|
|
$this->expectExceptionMessage('Test thrown exception');
|
|
|
|
|
|
|
|
$this->getService()->handle($schedule, true);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('schedules', [
|
|
|
|
'id' => $schedule->id,
|
|
|
|
'is_processing' => false,
|
|
|
|
'last_run_at' => CarbonImmutable::now()->toAtomString(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => false]);
|
|
|
|
}
|
|
|
|
|
2023-02-23 20:30:16 +01:00
|
|
|
public static function dispatchNowDataProvider(): array
|
2020-10-15 06:17:57 +02:00
|
|
|
{
|
|
|
|
return [[true], [false]];
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:59:20 +02:00
|
|
|
private function getService(): ProcessScheduleService
|
2020-10-15 06:17:57 +02:00
|
|
|
{
|
|
|
|
return $this->app->make(ProcessScheduleService::class);
|
|
|
|
}
|
|
|
|
}
|