Begin refactoring Tasks to be apart of the Scheduler system

This commit is contained in:
Dane Everitt 2017-09-12 23:45:19 -05:00
parent 07965d0ce7
commit 2ac90b50f2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
28 changed files with 902 additions and 468 deletions

View file

@ -25,7 +25,6 @@
namespace Pterodactyl\Console\Commands;
use Illuminate\Console\Command;
use Pterodactyl\Repositories\UserRepository;
use Pterodactyl\Services\Users\UserCreationService;
class MakeUser extends Command
@ -60,8 +59,7 @@ class MakeUser extends Command
*/
public function __construct(
UserCreationService $creationService
)
{
) {
parent::__construct();
$this->creationService = $creationService;
}
@ -87,7 +85,6 @@ class MakeUser extends Command
$data['root_admin'] = is_null($this->option('admin')) ? $this->confirm('Is this user a root administrator?') : $this->option('admin');
try {
$this->creationService->handle($data);
return $this->info('User successfully created.');

View file

@ -22,26 +22,23 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services\Tasks;
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class TaskUpdateService
interface ScheduleRepositoryInterface extends RepositoryInterface
{
protected $repository;
/**
* Return all of the schedules for a given server.
*
* @param int $server
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getServerSchedules($server);
protected $serverRepository;
public function __construct(
ServerRepositoryInterface $serverRepository,
TaskRepositoryInterface $repository
) {
$this->repository = $repository;
$this->serverRepository = $serverRepository;
}
public function handle($server, array $data, array $chain = null)
{
}
/**
* Return a schedule model with all of the associated tasks as a relationship.
*
* @param int $schedule
* @return \Illuminate\Support\Collection
*/
public function getScheduleWithTasks($schedule);
}

View file

@ -26,22 +26,4 @@ namespace Pterodactyl\Contracts\Repository;
interface TaskRepositoryInterface extends RepositoryInterface
{
/**
* Return the parent tasks and the count of children attached to that task.
*
* @param int $server
* @return mixed
*/
public function getParentTasksWithChainCount($server);
/**
* Return a single task for a given server including all of the chained tasks.
*
* @param int $task
* @param int $server
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getTaskForServer($task, $server);
}

View file

@ -0,0 +1,31 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Exceptions\Service\Schedule\Task;
use Pterodactyl\Exceptions\DisplayException;
class TaskIntervalTooLongException extends DisplayException
{
}

View file

@ -24,20 +24,27 @@
namespace Pterodactyl\Http\Controllers\Server\Tasks;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Requests\Request;
use Illuminate\Contracts\Session\Session;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Http\Requests\Server\TaskCreationFormRequest;
use Pterodactyl\Services\Schedules\ScheduleCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest;
class TaskManagementController extends Controller
{
use JavascriptInjection;
/**
* @var \Pterodactyl\Services\Tasks\TaskCreationService
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Services\Schedules\ScheduleCreationService
*/
protected $creationService;
@ -47,7 +54,7 @@ class TaskManagementController extends Controller
protected $hashids;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
@ -59,17 +66,20 @@ class TaskManagementController extends Controller
/**
* TaskManagementController constructor.
*
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
* @param \Illuminate\Contracts\Session\Session $session
* @param \Pterodactyl\Services\Tasks\TaskCreationService $creationService
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
* @param \Illuminate\Contracts\Session\Session $session
* @param \Pterodactyl\Services\Schedules\ScheduleCreationService $creationService
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
*/
public function __construct(
AlertsMessageBag $alert,
HashidsInterface $hashids,
Session $session,
TaskCreationService $creationService,
TaskRepositoryInterface $repository
ScheduleCreationService $creationService,
ScheduleRepositoryInterface $repository
) {
$this->alert = $alert;
$this->creationService = $creationService;
$this->hashids = $hashids;
$this->repository = $repository;
@ -86,11 +96,11 @@ class TaskManagementController extends Controller
public function index()
{
$server = $this->session->get('server_data.model');
$this->authorize('list-tasks', $server);
$this->authorize('list-schedules', $server);
$this->injectJavascript();
return view('server.tasks.index', [
'tasks' => $this->repository->getParentTasksWithChainCount($server->id),
'schedules' => $this->repository->getServerSchedules($server->id),
'actions' => [
'command' => trans('server.tasks.actions.command'),
'power' => trans('server.tasks.actions.power'),
@ -108,64 +118,95 @@ class TaskManagementController extends Controller
public function create()
{
$server = $this->session->get('server_data.model');
$this->authorize('create-task', $server);
$this->authorize('create-schedule', $server);
$this->injectJavascript();
return view('server.tasks.new');
}
/**
* @param \Pterodactyl\Http\Requests\Server\TaskCreationFormRequest $request
*
* @param \Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
*/
public function store(TaskCreationFormRequest $request)
public function store(ScheduleCreationFormRequest $request)
{
$server = $this->session->get('server_data.model');
$this->authorize('create-task', $server);
$this->authorize('create-schedule', $server);
$task = $this->creationService->handle($server, $request->normalize(), $request->getChainedTasks());
$schedule = $this->creationService->handle($server, $request->normalize(), $request->getTasks());
$this->alert->success(trans('server.tasks.task_created'))->flash();
return redirect()->route('server.tasks.view', [
'server' => $server->uuidShort,
'task' => $task->id,
'task' => $schedule->hashid,
]);
}
/**
* Return a view to modify task settings.
* Return a view to modify a schedule.
*
* @param string $uuid
* @param string $task
* @param \Pterodactyl\Http\Requests\Request $request
* @return \Illuminate\View\View
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function view($uuid, $task)
public function view(Request $request)
{
$server = $this->session->get('server_data.model');
$this->authorize('edit-task', $server);
$task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id);
$schedule = $request->attributes->get('schedule');
$this->authorize('view-schedule', $server);
$this->injectJavascript([
'chained' => $task->chained->map(function ($chain) {
return collect($chain->toArray())->only('action', 'chain_delay', 'data')->all();
'tasks' => $schedule->tasks->map(function ($schedule) {
return collect($schedule->toArray())->only('action', 'time_offset', 'payload')->all();
}),
]);
return view('server.tasks.view', ['task' => $task]);
return view('server.tasks.view', ['schedule' => $schedule]);
}
public function update(TaskCreationFormRequest $request, $uuid, $task)
/**
* Update a specific parent task on the system.
*
* @param \Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(ScheduleCreationFormRequest $request)
{
$server = $this->session->get('server_data.model');
$this->authorize('edit-task', $server);
$task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id);
$schedule = $request->attributes->get('schedule');
$this->authorize('edit-schedule', $server);
// $this->updateService->handle($task, $request->normalize(), $request->getChainedTasks());
$this->alert->success(trans('server.tasks.task_updated'))->flash();
return redirect()->route('server.tasks.view', [
'server' => $server->uuidShort,
'task' => $schedule->hashid,
]);
}
/**
* Delete a parent task from the Panel.
*
* @param \Pterodactyl\Http\Requests\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(Request $request)
{
$server = $this->session->get('server_data.model');
$schedule = $request->attributes->get('schedule');
$this->authorize('delete-schedule', $server);
$this->repository->delete($task->id);
return response('', 204);
}
}

View file

@ -0,0 +1,91 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Http\Middleware\Server;
use Closure;
use Illuminate\Contracts\Session\Session;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleAccess
{
/**
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
*/
protected $hashids;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;
/**
* TaskAccess constructor.
*
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
* @param \Illuminate\Contracts\Session\Session $session
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
*/
public function __construct(
HashidsInterface $hashids,
Session $session,
ScheduleRepositoryInterface $repository
) {
$this->hashids = $hashids;
$this->repository = $repository;
$this->session = $session;
}
/**
* Determine if a task is assigned to the active server.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($request, Closure $next)
{
$server = $this->session->get('server_data.model');
$scheduleId = $this->hashids->decodeFirst($request->route()->parameter('task'), 0);
$schedule = $this->repository->getScheduleWithTasks($scheduleId);
if ($schedule->server_id !== $server->id) {
abort(404);
}
$request->attributes->set('schedule', $schedule);
return $next($request);
}
}

View file

@ -26,7 +26,7 @@ namespace Pterodactyl\Http\Requests\Server;
use Pterodactyl\Http\Requests\FrontendUserFormRequest;
class TaskCreationFormRequest extends FrontendUserFormRequest
class ScheduleCreationFormRequest extends FrontendUserFormRequest
{
/**
* Validation rules to apply to the request.
@ -37,21 +37,19 @@ class TaskCreationFormRequest extends FrontendUserFormRequest
{
return [
'name' => 'string|max:255',
'day_of_week' => 'required|string',
'day_of_month' => 'required|string',
'hour' => 'required|string',
'minute' => 'required|string',
'action' => 'required|string|in:power,command',
'data' => 'required|string',
'chain' => 'sometimes|array|size:4',
'chain.time_value' => 'required_with:chain|max:5',
'chain.time_interval' => 'required_with:chain|max:5',
'chain.action' => 'required_with:chain|max:5',
'chain.payload' => 'required_with:chain|max:5',
'chain.time_value.*' => 'numeric|between:1,60',
'chain.time_interval.*' => 'string|in:s,m',
'chain.action.*' => 'string|in:power,command',
'chain.payload.*' => 'string',
'cron_day_of_week' => 'required|string',
'cron_day_of_month' => 'required|string',
'cron_hour' => 'required|string',
'cron_minute' => 'required|string',
'tasks' => 'sometimes|array|size:4',
'tasks.time_value' => 'required_with:chain|max:5',
'tasks.time_interval' => 'required_with:chain|max:5',
'tasks.action' => 'required_with:chain|max:5',
'tasks.payload' => 'required_with:chain|max:5',
'tasks.time_value.*' => 'numeric|between:1,60',
'tasks.time_interval.*' => 'string|in:s,m',
'tasks.action.*' => 'string|in:power,command',
'tasks.payload.*' => 'string',
];
}
@ -62,18 +60,18 @@ class TaskCreationFormRequest extends FrontendUserFormRequest
*/
public function normalize()
{
return $this->only('name', 'day_of_week', 'day_of_month', 'hour', 'minute', 'action', 'data');
return $this->only('name', 'cron_day_of_week', 'cron_day_of_month', 'cron_hour', 'cron_minute');
}
/**
* Return the chained tasks provided in the request.
* Return the tasks provided in the request that are associated with this schedule.
*
* @return array|null
*/
public function getChainedTasks()
public function getTasks()
{
$restructured = [];
foreach (array_get($this->all(), 'chain', []) as $key => $values) {
foreach (array_get($this->all(), 'tasks', []) as $key => $values) {
for ($i = 0; $i < count($values); ++$i) {
$restructured[$i][$key] = $values[$i];
}

153
app/Models/Schedule.php Normal file
View file

@ -0,0 +1,153 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Schedule extends Model implements CleansAttributes, ValidableContract
{
use Eloquence, Validable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'schedules';
/**
* Mass assignable attributes on this model.
*
* @var array
*/
protected $fillable = [
'server_id',
'name',
'cron_day_of_week',
'cron_day_of_month',
'cron_hour',
'cron_minute',
'is_active',
'is_processing',
'last_run_at',
'next_run_at',
];
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'server_id' => 'integer',
'is_active' => 'boolean',
'is_processing' => 'boolean',
];
/**
* Columns to mutate to a date.
*
* @var array
*/
protected $dates = [
self::CREATED_AT,
self::UPDATED_AT,
'last_run_at',
'next_run_at',
];
/**
* @var array
*/
protected $attributes = [
'name' => null,
'cron_day_of_week' => '*',
'cron_day_of_month' => '*',
'cron_hour' => '*',
'cron_minute' => '*',
'is_active' => true,
'is_processing' => false,
];
/**
* @var array
*/
protected static $applicationRules = [
'server_id' => 'required',
'cron_day_of_week' => 'required',
'cron_day_of_month' => 'required',
'cron_hour' => 'required',
'cron_minute' => 'required',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'server_id' => 'exists:servers,id',
'name' => 'nullable|string|max:255',
'cron_day_of_week' => 'string',
'cron_day_of_month' => 'string',
'cron_hour' => 'string',
'cron_minute' => 'string',
'is_active' => 'boolean',
'is_processing' => 'boolean',
'last_run_at' => 'nullable|timestamp',
'next_run_at' => 'nullable|timestamp',
];
/**
* Return a hashid encoded string to represent the ID of the schedule.
*
* @return string
*/
public function getHashidAttribute()
{
return app()->make('hashids')->encode($this->id);
}
/**
* Return tasks belonging to a schedule.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class);
}
/**
* Return the server model that a schedule belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
}

View file

@ -42,11 +42,25 @@ class Task extends Model implements CleansAttributes, ValidableContract
protected $table = 'tasks';
/**
* Fields that are not mass assignable.
* Relationships to be updated when this model is updated.
*
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $touches = ['schedule'];
/**
* Fields that are mass assignable.
*
* @var array
*/
protected $fillable = [
'schedule_id',
'squence_id',
'action',
'payload',
'time_offset',
'is_queued',
];
/**
* Cast values to correct type.
@ -55,10 +69,10 @@ class Task extends Model implements CleansAttributes, ValidableContract
*/
protected $casts = [
'id' => 'integer',
'user_id' => 'integer',
'server_id' => 'integer',
'queued' => 'boolean',
'active' => 'boolean',
'schedule_id' => 'integer',
'squence_id' => 'integer',
'time_offset' => 'integer',
'is_queued' => 'boolean',
];
/**
@ -67,54 +81,32 @@ class Task extends Model implements CleansAttributes, ValidableContract
* @var array
*/
protected $attributes = [
'parent_task_id' => null,
'chain_order' => null,
'active' => true,
'day_of_week' => '*',
'day_of_month' => '*',
'hour' => '*',
'minute' => '*',
'chain_delay' => null,
'queued' => false,
'is_queued' => false,
];
/**
* @var array
*/
protected static $applicationRules = [
'server_id' => 'required',
'schedule_id' => 'required',
'squence_id' => 'required',
'action' => 'required',
'data' => 'required',
'payload' => 'required',
'time_offset' => 'required',
];
/**
* @var array
*/
protected static $dataIntegrityRules = [
'name' => 'nullable|string|max:255',
'parent_task_id' => 'nullable|numeric|exists:tasks,id',
'chain_order' => 'nullable|numeric|min:1',
'server_id' => 'numeric|exists:servers,id',
'active' => 'boolean',
'schedule_id' => 'numeric|exists:schedules,id',
'squence_id' => 'numeric|min:1',
'action' => 'string',
'data' => 'string',
'queued' => 'boolean',
'day_of_month' => 'string',
'day_of_week' => 'string',
'hour' => 'string',
'minute' => 'string',
'chain_delay' => 'nullable|numeric|between:1,900',
'last_run' => 'nullable|timestamp',
'next_run' => 'nullable|timestamp',
'payload' => 'string',
'time_offset' => 'numeric|between:0,900',
'is_queued' => 'boolean',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['last_run', 'next_run', 'created_at', 'updated_at'];
/**
* Return a hashid encoded string to represent the ID of the task.
*
@ -126,32 +118,26 @@ class Task extends Model implements CleansAttributes, ValidableContract
}
/**
* Gets the server associated with a task.
* Return the schedule that a task belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function schedule()
{
return $this->belongsTo(Schedule::class);
}
/**
* Return the server a task is assigned to, acts as a belongsToThrough.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
/**
* Gets the user associated with a task.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Return chained tasks for a parent task.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function chained()
{
return $this->hasMany(self::class, 'parent_task_id')->orderBy('chain_order', 'asc');
if ($schedule = $this->schedule) {
return $schedule->server();
} else {
throw new \InvalidArgumentException('Instance of Task must have an associated Schedule in the database.');
}
}
}

View file

@ -39,6 +39,7 @@ use Pterodactyl\Repositories\Eloquent\SessionRepository;
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Repositories\Eloquent\PermissionRepository;
use Pterodactyl\Repositories\Daemon\ConfigurationRepository;
@ -59,6 +60,7 @@ use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServiceVariableRepository;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
@ -92,6 +94,7 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(OptionVariableRepositoryInterface::class, OptionVariableRepository::class);
$this->app->bind(PackRepositoryInterface::class, PackRepository::class);
$this->app->bind(PermissionRepositoryInterface::class, PermissionRepository::class);
$this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class);
$this->app->bind(ServerRepositoryInterface::class, ServerRepository::class);
$this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class);
$this->app->bind(ServiceRepositoryInterface::class, ServiceRepository::class);

View file

@ -0,0 +1,55 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleRepository extends EloquentRepository implements ScheduleRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return Schedule::class;
}
/**
* {@inheritdoc}
*/
public function getServerSchedules($server)
{
return $this->getBuilder()->withCount('tasks')->where('server_id', '=', $server)->get($this->getColumns());
}
/**
* {@inheritdoc}
*/
public function getScheduleWithTasks($schedule)
{
return $this->getBuilder()->with('tasks')->find($schedule, $this->getColumns());
}
}

View file

@ -0,0 +1,106 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Schedules;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleCreationService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $taskCreationService;
/**
* ScheduleCreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
* @param \Pterodactyl\Services\Schedules\Tasks\TaskCreationService $taskCreationService
*/
public function __construct(
ConnectionInterface $connection,
ScheduleRepositoryInterface $repository,
TaskCreationService $taskCreationService
) {
$this->connection = $connection;
$this->repository = $repository;
$this->taskCreationService = $taskCreationService;
}
/**
* Create a new schedule for a specific server.
*
* @param int|\Pterodactyl\Models\Server $server
* @param array $data
* @param array $tasks
* @return \Pterodactyl\Models\Schedule
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
*/
public function handle($server, array $data, array $tasks = [])
{
Assert::true(($server instanceof Server || is_numeric($server)),
'First argument passed to handle must be numeric or instance of \Pterodactyl\Models\Server, received %s.'
);
$server = ($server instanceof Server) ? $server->id : $server;
$data['server_id'] = $server;
$this->connection->beginTransaction();
$schedule = $this->repository->create($data);
if (! empty($tasks)) {
foreach ($tasks as $index => $task) {
$this->taskCreationService->handle($schedule, [
'time_interval' => array_get($task, 'time_interval'),
'time_value' => array_get($task, 'time_value'),
'sequence_id' => $index + 1,
'action' => array_get($task, 'action'),
'payload' => array_get($task, 'payload'),
], false);
}
}
$this->connection->commit();
return $schedule;
}
}

View file

@ -0,0 +1,84 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Schedules\Tasks;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException;
class TaskCreationService
{
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
protected $repository;
/**
* TaskCreationService constructor.
*
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
*/
public function __construct(TaskRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Create a new task that is assigned to a schedule.
*
* @param int|\Pterodactyl\Models\Schedule $schedule
* @param array $data
* @param bool $returnModel
* @return bool|\Pterodactyl\Models\Task
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
*/
public function handle($schedule, array $data, $returnModel = true)
{
Assert::true(($schedule instanceof Schedule || is_numeric($schedule)),
'First argument passed to handle must be numeric or instance of \Pterodactyl\Models\Schedule, received %s.'
);
$schedule = ($schedule instanceof Schedule) ? $schedule->id : $schedule;
if ($data['time_interval'] === 'm' && $data['time_value'] > 15) {
throw new TaskIntervalTooLongException(trans('exceptions.tasks.chain_interval_too_long'));
}
$delay = $data['time_interval'] === 'm' ? $data['time_value'] * 60 : $data['time_value'];
$repository = ($returnModel) ? $this->repository : $this->repository->withoutFresh();
$task = $repository->create([
'schedule_id' => $schedule,
'sequence_id' => $data['sequence_id'],
'action' => $data['action'],
'payload' => $data['payload'],
'time_offset' => $delay,
]);
return $task;
}
}

View file

@ -1,108 +0,0 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Tasks;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class TaskCreationService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* TaskCreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
*/
public function __construct(
ConnectionInterface $connection,
ServerRepositoryInterface $serverRepository,
TaskRepositoryInterface $repository
) {
$this->connection = $connection;
$this->repository = $repository;
$this->serverRepository = $serverRepository;
}
/**
* @param int|\Pterodactyl\Models\Server $server
* @param array $data
* @param array|null $chain
* @return \Pterodactyl\Models\Task
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($server, array $data, array $chain = null)
{
if (! $server instanceof Server) {
$server = $this->serverRepository->find($server);
}
$this->connection->beginTransaction();
$data['server_id'] = $server->id;
$task = $this->repository->create($data);
if (is_array($chain)) {
foreach ($chain as $index => $values) {
if ($values['time_interval'] === 'm' && $values['time_value'] > 15) {
throw new \Exception('I should fix this.');
}
$delay = $values['time_interval'] === 'm' ? $values['time_value'] * 60 : $values['time_value'];
$this->repository->withoutFresh()->create([
'parent_task_id' => $task->id,
'chain_order' => $index + 1,
'server_id' => $server->id,
'action' => $values['action'],
'data' => $values['payload'],
'chain_delay' => $delay,
]);
}
}
$this->connection->commit();
return $task;
}
}