misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/ScheduleController.php

184 lines
6.6 KiB
PHP
Raw Normal View History

2020-02-08 23:23:08 +00:00
<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Exception;
use Carbon\Carbon;
2020-02-08 23:23:08 +00:00
use Illuminate\Http\Request;
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;
use Pterodactyl\Helpers\Utilities;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
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;
2021-07-24 21:01:14 +00:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
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;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\TriggerScheduleRequest;
2020-02-08 23:23:08 +00:00
class ScheduleController extends ClientApiController
{
2021-03-05 17:03:12 +00:00
private ScheduleRepository $repository;
private ProcessScheduleService $service;
/**
* ScheduleController constructor.
*/
public function __construct(ScheduleRepository $repository, ProcessScheduleService $service)
{
parent::__construct();
$this->repository = $repository;
$this->service = $service;
}
2020-02-08 23:23:08 +00:00
/**
* Returns all of the schedules belonging to a given server.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2020-02-08 23:23:08 +00:00
*/
2021-03-05 17:03:12 +00:00
public function index(ViewScheduleRequest $request, Server $server): array
2020-02-08 23:23:08 +00:00
{
$schedules = $server->schedule;
$schedules->loadMissing('tasks');
return $this->fractal->collection($schedules)
2021-08-07 21:32:40 +00:00
->transformWith(ScheduleTransformer::class)
2020-02-08 23:23:08 +00:00
->toArray();
}
2020-03-18 06:33:53 +00:00
/**
* Store a new schedule for a server.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
2021-03-05 17:03:12 +00:00
public function store(StoreScheduleRequest $request, Server $server): array
{
/** @var \Pterodactyl\Models\Schedule $model */
$model = $this->repository->create([
'server_id' => $server->id,
'name' => $request->input('name'),
'cron_day_of_week' => $request->input('day_of_week'),
'cron_month' => $request->input('month'),
'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'),
'only_when_online' => (bool) $request->input('only_when_online'),
'next_run_at' => $this->getNextRunAt($request),
]);
return $this->fractal->item($model)
2021-08-07 21:32:40 +00:00
->transformWith(ScheduleTransformer::class)
->toArray();
}
2020-03-18 06:33:53 +00:00
/**
* Returns a specific schedule for the server.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2020-03-18 06:33:53 +00:00
*/
2021-03-05 17:03:12 +00:00
public function view(ViewScheduleRequest $request, Server $server, Schedule $schedule): array
2020-03-18 06:33:53 +00:00
{
$schedule->loadMissing('tasks');
return $this->fractal->item($schedule)
2021-08-07 21:32:40 +00:00
->transformWith(ScheduleTransformer::class)
2020-03-18 06:33:53 +00:00
->toArray();
}
/**
* Updates a given schedule with the new data provided.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
2021-03-05 17:03:12 +00:00
public function update(UpdateScheduleRequest $request, Server $server, Schedule $schedule): array
{
$active = (bool) $request->input('is_active');
$data = [
'name' => $request->input('name'),
'cron_day_of_week' => $request->input('day_of_week'),
'cron_month' => $request->input('month'),
'cron_day_of_month' => $request->input('day_of_month'),
'cron_hour' => $request->input('hour'),
'cron_minute' => $request->input('minute'),
'is_active' => $active,
'only_when_online' => (bool) $request->input('only_when_online'),
'next_run_at' => $this->getNextRunAt($request),
];
// 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;
}
$this->repository->update($schedule->id, $data);
return $this->fractal->item($schedule->refresh())
2021-08-07 21:32:40 +00:00
->transformWith(ScheduleTransformer::class)
->toArray();
}
/**
* 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
*/
2021-03-05 17:03:12 +00:00
public function execute(TriggerScheduleRequest $request, Server $server, Schedule $schedule): Response
{
2021-01-23 20:33:34 +00:00
if (!$schedule->is_active) {
2021-03-05 17:03:12 +00:00
throw new BadRequestHttpException('Cannot trigger schedule exception for a schedule that is not currently active.');
}
$this->service->handle($schedule, true);
2021-03-05 17:03:12 +00:00
return $this->returnAccepted();
}
/**
* Deletes a schedule and it's associated tasks.
*/
2021-03-05 17:03:12 +00:00
public function delete(DeleteScheduleRequest $request, Server $server, Schedule $schedule): Response
{
$this->repository->delete($schedule->id);
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
* Get the next run timestamp based on the cron data provided.
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
protected function getNextRunAt(Request $request): Carbon
{
try {
return Utilities::getScheduleNextRunDate(
$request->input('minute'),
$request->input('hour'),
$request->input('day_of_month'),
$request->input('month'),
$request->input('day_of_week')
);
} catch (Exception $exception) {
2021-01-23 20:33:34 +00:00
throw new DisplayException('The cron data provided does not evaluate to a valid expression.');
}
}
2020-02-08 23:23:08 +00:00
}