2020-02-08 23:23:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
use Exception;
|
|
|
|
use Carbon\Carbon;
|
2020-02-08 23:23:08 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-22 20:56:15 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-02-08 23:23:08 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-03-18 06:33:53 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2020-03-22 20:56:15 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 23:26:28 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2020-03-22 20:56:15 +00:00
|
|
|
use Pterodactyl\Helpers\Utilities;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2022-10-22 23:49:18 +00:00
|
|
|
use Pterodactyl\Exceptions\Model\DataValidationException;
|
2020-10-15 03:38:59 +00:00
|
|
|
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
2020-02-08 23:23:08 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\ScheduleTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2020-03-18 06:33:53 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2020-03-22 20:56:15 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\ViewScheduleRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\StoreScheduleRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\DeleteScheduleRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\UpdateScheduleRequest;
|
2020-10-15 03:38:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\TriggerScheduleRequest;
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
class ScheduleController extends ClientApiController
|
|
|
|
{
|
2020-03-22 20:56:15 +00:00
|
|
|
/**
|
|
|
|
* ScheduleController constructor.
|
|
|
|
*/
|
2022-10-22 23:49:18 +00:00
|
|
|
public function __construct(private ProcessScheduleService $service)
|
2020-03-22 20:56:15 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2020-02-08 23:23:08 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Returns all the schedules belonging to a given server.
|
2020-02-08 23:23:08 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function index(ViewScheduleRequest $request, Server $server): array
|
2020-02-08 23:23:08 +00:00
|
|
|
{
|
2022-05-22 18:10:01 +00:00
|
|
|
$schedules = $server->schedules->loadMissing('tasks');
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($schedules)
|
|
|
|
->transformWith($this->getTransformer(ScheduleTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2020-03-18 06:33:53 +00:00
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
/**
|
|
|
|
* Store a new schedule for a server.
|
|
|
|
*
|
2022-10-22 23:49:18 +00:00
|
|
|
* @throws DisplayException
|
|
|
|
* @throws DataValidationException
|
2020-03-22 20:56:15 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function store(StoreScheduleRequest $request, Server $server): array
|
2020-03-22 20:56:15 +00:00
|
|
|
{
|
2022-10-22 23:49:18 +00:00
|
|
|
/** @var Schedule $model */
|
|
|
|
$model = Schedule::query()->create([
|
2020-03-22 20:56:15 +00:00
|
|
|
'server_id' => $server->id,
|
|
|
|
'name' => $request->input('name'),
|
|
|
|
'cron_day_of_week' => $request->input('day_of_week'),
|
2021-01-17 03:07:39 +00:00
|
|
|
'cron_month' => $request->input('month'),
|
2020-03-22 20:56:15 +00:00
|
|
|
'cron_day_of_month' => $request->input('day_of_month'),
|
|
|
|
'cron_hour' => $request->input('hour'),
|
|
|
|
'cron_minute' => $request->input('minute'),
|
|
|
|
'is_active' => (bool) $request->input('is_active'),
|
2021-05-01 17:44:40 +00:00
|
|
|
'only_when_online' => (bool) $request->input('only_when_online'),
|
2020-03-22 20:56:15 +00:00
|
|
|
'next_run_at' => $this->getNextRunAt($request),
|
|
|
|
]);
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:schedule.create')
|
|
|
|
->subject($model)
|
|
|
|
->property('name', $model->name)
|
|
|
|
->log();
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
return $this->fractal->item($model)
|
|
|
|
->transformWith($this->getTransformer(ScheduleTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-03-18 06:33:53 +00:00
|
|
|
/**
|
|
|
|
* Returns a specific schedule for the server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function view(ViewScheduleRequest $request, Server $server, Schedule $schedule): array
|
2020-03-18 06:33:53 +00:00
|
|
|
{
|
|
|
|
if ($schedule->server_id !== $server->id) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new NotFoundHttpException();
|
2020-03-18 06:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$schedule->loadMissing('tasks');
|
|
|
|
|
|
|
|
return $this->fractal->item($schedule)
|
|
|
|
->transformWith($this->getTransformer(ScheduleTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2020-03-22 20:56:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a given schedule with the new data provided.
|
|
|
|
*
|
2022-10-22 23:49:18 +00:00
|
|
|
* @throws DisplayException
|
|
|
|
* @throws DataValidationException
|
2020-03-22 20:56:15 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function update(UpdateScheduleRequest $request, Server $server, Schedule $schedule): array
|
2020-03-22 20:56:15 +00:00
|
|
|
{
|
2020-10-04 02:47:52 +00:00
|
|
|
$active = (bool) $request->input('is_active');
|
|
|
|
|
|
|
|
$data = [
|
2020-03-22 20:56:15 +00:00
|
|
|
'name' => $request->input('name'),
|
|
|
|
'cron_day_of_week' => $request->input('day_of_week'),
|
2021-01-17 03:07:39 +00:00
|
|
|
'cron_month' => $request->input('month'),
|
2020-03-22 20:56:15 +00:00
|
|
|
'cron_day_of_month' => $request->input('day_of_month'),
|
|
|
|
'cron_hour' => $request->input('hour'),
|
|
|
|
'cron_minute' => $request->input('minute'),
|
2020-10-04 02:47:52 +00:00
|
|
|
'is_active' => $active,
|
2021-05-01 17:44:40 +00:00
|
|
|
'only_when_online' => (bool) $request->input('only_when_online'),
|
2020-03-22 20:56:15 +00:00
|
|
|
'next_run_at' => $this->getNextRunAt($request),
|
2020-10-04 02:47:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Toggle the processing state of the scheduled task when it is enabled or disabled so that an
|
|
|
|
// invalid state can be reset without manual database intervention.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2425
|
|
|
|
if ($schedule->is_active !== $active) {
|
|
|
|
$data['is_processing'] = false;
|
|
|
|
}
|
|
|
|
|
2022-10-22 23:49:18 +00:00
|
|
|
$schedule->update($data);
|
2020-03-22 20:56:15 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:schedule.update')
|
|
|
|
->subject($schedule)
|
|
|
|
->property(['name' => $schedule->name, 'active' => $active])
|
|
|
|
->log();
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
return $this->fractal->item($schedule->refresh())
|
|
|
|
->transformWith($this->getTransformer(ScheduleTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
/**
|
|
|
|
* Executes a given schedule immediately rather than waiting on it's normally scheduled time
|
|
|
|
* to pass. This does not care about the schedule state.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function execute(TriggerScheduleRequest $request, Server $server, Schedule $schedule): JsonResponse
|
2020-10-15 03:38:59 +00:00
|
|
|
{
|
|
|
|
$this->service->handle($schedule, true);
|
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:schedule.execute')->subject($schedule)->property('name', $schedule->name)->log();
|
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_ACCEPTED);
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
/**
|
|
|
|
* Deletes a schedule and it's associated tasks.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function delete(DeleteScheduleRequest $request, Server $server, Schedule $schedule): JsonResponse
|
2020-03-22 20:56:15 +00:00
|
|
|
{
|
2022-10-22 23:49:18 +00:00
|
|
|
$schedule->delete();
|
2020-03-22 20:56:15 +00:00
|
|
|
|
2022-05-29 23:26:28 +00:00
|
|
|
Activity::event('server:schedule.delete')->subject($schedule)->property('name', $schedule->name)->log();
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-03-22 20:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next run timestamp based on the cron data provided.
|
|
|
|
*
|
2022-10-22 23:49:18 +00:00
|
|
|
* @throws DisplayException
|
2020-03-22 20:56:15 +00:00
|
|
|
*/
|
|
|
|
protected function getNextRunAt(Request $request): Carbon
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return Utilities::getScheduleNextRunDate(
|
|
|
|
$request->input('minute'),
|
|
|
|
$request->input('hour'),
|
|
|
|
$request->input('day_of_month'),
|
2021-01-17 03:07:39 +00:00
|
|
|
$request->input('month'),
|
2020-03-22 20:56:15 +00:00
|
|
|
$request->input('day_of_week')
|
|
|
|
);
|
2022-10-22 23:49:18 +00:00
|
|
|
} catch (Exception) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new DisplayException('The cron data provided does not evaluate to a valid expression.');
|
2020-03-22 20:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:23:08 +00:00
|
|
|
}
|