Un-butcher task system.

This commit is contained in:
Dane Everitt 2017-04-15 13:52:43 -04:00
parent 0fe9a4566e
commit 7f1a97184b
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 154 additions and 77 deletions

View file

@ -25,7 +25,7 @@
namespace Pterodactyl\Console\Commands; namespace Pterodactyl\Console\Commands;
use Carbon; use Carbon;
use Pterodactyl\Models; use Pterodactyl\Models\Task;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Pterodactyl\Jobs\SendScheduledTask; use Pterodactyl\Jobs\SendScheduledTask;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
@ -65,14 +65,14 @@ class RunTasks extends Command
*/ */
public function handle() public function handle()
{ {
$tasks = Models\Task::where('queued', 0)->where('active', 1)->where('next_run', '<=', Carbon::now()->toAtomString())->get(); $tasks = Task::where('queued', false)->where('active', true)->where('next_run', '<=', Carbon::now()->toAtomString())->get();
$this->info(sprintf('Preparing to queue %d tasks.', count($tasks))); $this->info(sprintf('Preparing to queue %d tasks.', count($tasks)));
$bar = $this->output->createProgressBar(count($tasks)); $bar = $this->output->createProgressBar(count($tasks));
foreach ($tasks as &$task) { foreach ($tasks as &$task) {
$bar->advance(); $bar->advance();
$this->dispatch((new SendScheduledTask(Models\Server::findOrFail($task->server), $task))->onQueue(config('pterodactyl.queues.low'))); $this->dispatch((new SendScheduledTask($task))->onQueue(config('pterodactyl.queues.low')));
} }
$bar->finish(); $bar->finish();

View file

@ -70,7 +70,7 @@ class ServerController extends Controller
$server = Server::byUuid($uuid); $server = Server::byUuid($uuid);
$request->user()->can('power-' . $request->input('action'), $server); $request->user()->can('power-' . $request->input('action'), $server);
$repo = new PowerRepository($server); $repo = new PowerRepository($server, $request->user());
$repo->do($request->input('action')); $repo->do($request->input('action'));
return response('', 204)->header('Content-Type', 'application/json'); return response('', 204)->header('Content-Type', 'application/json');
@ -90,7 +90,7 @@ class ServerController extends Controller
$server = Server::byUuid($uuid); $server = Server::byUuid($uuid);
$request->user()->can('send-command', $server); $request->user()->can('send-command', $server);
$repo = new CommandRepository($server); $repo = new CommandRepository($server, $request->user());
$repo->send($request->input('command')); $repo->send($request->input('command'));
return response('', 204)->header('Content-Type', 'application/json'); return response('', 204)->header('Content-Type', 'application/json');

View file

@ -26,9 +26,9 @@ namespace Pterodactyl\Http\Controllers\Server;
use Log; use Log;
use Alert; use Alert;
use Pterodactyl\Models;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Repositories; use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\TaskRepository;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Exceptions\DisplayValidationException; use Pterodactyl\Exceptions\DisplayValidationException;
@ -44,7 +44,7 @@ class TaskController extends Controller
*/ */
public function index(Request $request, $uuid) public function index(Request $request, $uuid)
{ {
$server = Models\Server::byUuid($uuid)->load('tasks'); $server = Server::byUuid($uuid)->load('tasks');
$this->authorize('list-tasks', $server); $this->authorize('list-tasks', $server);
$server->js(); $server->js();
@ -68,7 +68,7 @@ class TaskController extends Controller
*/ */
public function create(Request $request, $uuid) public function create(Request $request, $uuid)
{ {
$server = Models\Server::byUuid($uuid); $server = Server::byUuid($uuid);
$this->authorize('create-task', $server); $this->authorize('create-task', $server);
$server->js(); $server->js();
@ -87,12 +87,12 @@ class TaskController extends Controller
*/ */
public function store(Request $request, $uuid) public function store(Request $request, $uuid)
{ {
$server = Models\Server::byUuid($uuid); $server = Server::byUuid($uuid);
$this->authorize('create-task', $server); $this->authorize('create-task', $server);
$repo = new TaskRepository;
try { try {
$repo = new Repositories\TaskRepository; $repo->create($server->id, $request->user()->id, $request->except([
$repo->create($server->id, $request->except([
'_token', '_token',
])); ]));
@ -119,7 +119,7 @@ class TaskController extends Controller
*/ */
public function delete(Request $request, $uuid, $id) public function delete(Request $request, $uuid, $id)
{ {
$server = Models\Server::byUuid($uuid)->load('tasks'); $server = Server::byUuid($uuid)->load('tasks');
$this->authorize('delete-task', $server); $this->authorize('delete-task', $server);
$task = $server->tasks->where('id', $id)->first(); $task = $server->tasks->where('id', $id)->first();
@ -129,8 +129,8 @@ class TaskController extends Controller
], 404); ], 404);
} }
$repo = new TaskRepository;
try { try {
$repo = new Repositories\TaskRepository;
$repo->delete($id); $repo->delete($id);
return response()->json([], 204); return response()->json([], 204);
@ -153,7 +153,7 @@ class TaskController extends Controller
*/ */
public function toggle(Request $request, $uuid, $id) public function toggle(Request $request, $uuid, $id)
{ {
$server = Models\Server::byUuid($uuid)->load('tasks'); $server = Server::byUuid($uuid)->load('tasks');
$this->authorize('toggle-task', $server); $this->authorize('toggle-task', $server);
$task = $server->tasks->where('id', $id)->first(); $task = $server->tasks->where('id', $id)->first();
@ -163,8 +163,8 @@ class TaskController extends Controller
], 404); ], 404);
} }
$repo = new TaskRepository;
try { try {
$repo = new Repositories\TaskRepository;
$resp = $repo->toggle($id); $resp = $repo->toggle($id);
return response()->json([ return response()->json([

View file

@ -26,7 +26,10 @@ namespace Pterodactyl\Jobs;
use Cron; use Cron;
use Carbon; use Carbon;
use Pterodactyl\Models; use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\TaskLog;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -37,11 +40,6 @@ class SendScheduledTask extends Job implements ShouldQueue
{ {
use InteractsWithQueue, SerializesModels; use InteractsWithQueue, SerializesModels;
/**
* @var \Pterodactyl\Models\Server
*/
protected $server;
/** /**
* @var \Pterodactyl\Models\Task * @var \Pterodactyl\Models\Task
*/ */
@ -52,13 +50,12 @@ class SendScheduledTask extends Job implements ShouldQueue
* *
* @return void * @return void
*/ */
public function __construct(Models\Server $server, Models\Task $task) public function __construct(Task $task)
{ {
$this->server = $server;
$this->task = $task; $this->task = $task;
$task->queued = 1; $this->task->queued = true;
$task->save(); $this->task->save();
} }
/** /**
@ -69,7 +66,7 @@ class SendScheduledTask extends Job implements ShouldQueue
public function handle() public function handle()
{ {
$time = Carbon::now(); $time = Carbon::now();
$log = new Models\TaskLog; $log = new TaskLog;
if ($this->attempts() >= 1) { if ($this->attempts() >= 1) {
// Just delete the job, we will attempt it again later anyways. // Just delete the job, we will attempt it again later anyways.
@ -78,12 +75,15 @@ class SendScheduledTask extends Job implements ShouldQueue
try { try {
if ($this->task->action === 'command') { if ($this->task->action === 'command') {
$repo = new CommandRepository($this->server); $repo = new CommandRepository($this->task->server, $this->task->user);
$response = $repo->send($this->task->data); $response = $repo->send($this->task->data);
} elseif ($this->task->action === 'power') { } else if ($this->task->action === 'power') {
$repo = new PowerRepository($this->server); $repo = new PowerRepository($this->task->server, $this->task->user);
$response = $repo->do($this->task->data); $response = $repo->do($this->task->data);
} else {
throw new \Exception('Task type provided was not valid.');
} }
$log->fill([ $log->fill([
'task_id' => $this->task->id, 'task_id' => $this->task->id,
'run_time' => $time, 'run_time' => $time,
@ -109,7 +109,7 @@ class SendScheduledTask extends Job implements ShouldQueue
$this->task->fill([ $this->task->fill([
'last_run' => $time, 'last_run' => $time,
'next_run' => $cron->getNextRunDate(), 'next_run' => $cron->getNextRunDate(),
'queued' => 0, 'queued' => false,
]); ]);
$this->task->save(); $this->task->save();
$log->save(); $log->save();

View file

@ -320,7 +320,7 @@ class Server extends Model
*/ */
public function tasks() public function tasks()
{ {
return $this->hasMany(Task::class, 'server', 'id'); return $this->hasMany(Task::class);
} }
/** /**

View file

@ -49,8 +49,10 @@ class Task extends Model
*/ */
protected $casts = [ protected $casts = [
'id' => 'integer', 'id' => 'integer',
'server' => 'integer', 'user_id' => 'integer',
'queued' => 'integer', 'server_id' => 'integer',
'queued' => 'boolean',
'active' => 'boolean',
]; ];
/** /**
@ -59,4 +61,24 @@ class Task extends Model
* @var array * @var array
*/ */
protected $dates = ['last_run', 'next_run', 'created_at', 'updated_at']; protected $dates = ['last_run', 'next_run', 'created_at', 'updated_at'];
/**
* Gets the server associated with a task.
*
* @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);
}
} }

View file

@ -24,7 +24,8 @@
namespace Pterodactyl\Repositories\Daemon; namespace Pterodactyl\Repositories\Daemon;
use Pterodactyl\Models; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
@ -37,15 +38,24 @@ class CommandRepository
*/ */
protected $server; protected $server;
/**
* The Eloquent Model associated with the user to run the request as.
*
* @var \Pterodactyl\Models\User|null
*/
protected $user;
/** /**
* Constuctor for repository. * Constuctor for repository.
* *
* @param int|\Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\User|null $user
* @return void * @return void
*/ */
public function __construct($server) public function __construct(Server $server, User $user = null)
{ {
$this->server = ($server instanceof Models\Server) ? $server : Models\Server::findOrFail($server); $this->server = $server;
$this->user = $user;
} }
/** /**
@ -62,7 +72,7 @@ class CommandRepository
// We don't use the user's specific daemon secret here since we // We don't use the user's specific daemon secret here since we
// are assuming that a call to this function has been validated. // are assuming that a call to this function has been validated.
try { try {
$response = $this->server->guzzleClient()->request('PUT', '/server/command', [ $response = $this->server->guzzleClient($this->user)->request('POST', '/server/command', [
'http_errors' => false, 'http_errors' => false,
'json' => [ 'json' => [
'command' => $command, 'command' => $command,

View file

@ -24,7 +24,8 @@
namespace Pterodactyl\Repositories\Daemon; namespace Pterodactyl\Repositories\Daemon;
use Pterodactyl\Models; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
@ -37,21 +38,30 @@ class PowerRepository
*/ */
protected $server; protected $server;
/**
* The Eloquent Model associated with the user to run the request as.
*
* @var \Pterodactyl\Models\User|null
*/
protected $user;
/** /**
* Constuctor for repository. * Constuctor for repository.
* *
* @param int|\Pterodactyl\Models\Server $server * @param \Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\User|null $user
* @return void * @return void
*/ */
public function __construct($server) public function __construct(Server $server, User $user = null)
{ {
$this->server = ($server instanceof Models\Server) ? $server : Models\Server::findOrFail($server); $this->server = $server;
$this->user = $user;
} }
/** /**
* Sends a power option to the daemon. * Sends a power option to the daemon.
* *
* @param string $action * @param string $action
* @return string * @return string
* *
* @throws \GuzzleHttp\Exception\RequestException * @throws \GuzzleHttp\Exception\RequestException
@ -59,10 +69,8 @@ class PowerRepository
*/ */
public function do($action) public function do($action)
{ {
// We don't use the user's specific daemon secret here since we
// are assuming that a call to this function has been validated.
try { try {
$response = $this->server->guzzleClient()->request('PUT', '/server/power', [ $response = $this->server->guzzleClient($this->user)->request('PUT', '/server/power', [
'http_errors' => false, 'http_errors' => false,
'json' => [ 'json' => [
'action' => $action, 'action' => $action,

View file

@ -24,9 +24,12 @@
namespace Pterodactyl\Repositories; namespace Pterodactyl\Repositories;
use DB;
use Cron; use Cron;
use Validator; use Validator;
use Pterodactyl\Models; use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException; use Pterodactyl\Exceptions\DisplayValidationException;
@ -64,49 +67,42 @@ class TaskRepository
*/ */
public function delete($id) public function delete($id)
{ {
$task = Models\Task::findOrFail($id); $task = Task::findOrFail($id);
try { $task->delete();
$task->delete();
return true;
} catch (\Exception $ex) {
throw $ex;
}
} }
/** /**
* Toggles a task active or inactive. * Toggles a task active or inactive.
* *
* @param int $id * @param int $id
* @return int * @return bool
*/ */
public function toggle($id) public function toggle($id)
{ {
$task = Models\Task::findOrFail($id); $task = Task::findOrFail($id);
try {
$task->active = ($task->active === 1) ? 0 : 1;
$task->queued = 0;
$task->save();
return $task->active; $task->active = ! $task->active;
} catch (\Exception $ex) { $task->queued = false;
throw $ex; $task->save();
}
return $task->active;
} }
/** /**
* Create a new scheduled task for a given server. * Create a new scheduled task for a given server.
* *
* @param int $id * @param int $server
* @param int $user
* @param array $data * @param array $data
* @return bool * @return \Pterodactyl\Models\Task
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException * @throws \Pterodactyl\Exceptions\DisplayValidationException
*/ */
public function create($id, $data) public function create($server, $user, $data)
{ {
$server = Models\Server::findOrFail($id); $server = Server::findOrFail($server);
$user = User::findOrFail($user);
$validator = Validator::make($data, [ $validator = Validator::make($data, [
'action' => 'string|required', 'action' => 'string|required',
@ -148,9 +144,9 @@ class TaskRepository
throw $ex; throw $ex;
} }
$task = new Models\Task; return Task::create([
$task->fill([ 'user_id' => $user->id,
'server' => $server->id, 'server_id' => $server->id,
'active' => 1, 'active' => 1,
'action' => $data['action'], 'action' => $data['action'],
'data' => $data['data'], 'data' => $data['data'],
@ -164,7 +160,5 @@ class TaskRepository
'last_run' => null, 'last_run' => null,
'next_run' => $buildCron->getNextRunDate(), 'next_run' => $buildCron->getNextRunDate(),
]); ]);
return $task->save();
} }
} }

View file

@ -0,0 +1,43 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpgradeTaskSystem extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropForeign(['server']);
$table->renameColumn('server', 'server_id');
$table->unsignedInteger('user_id')->after('id');
$table->foreign('server_id')->references('id')->on('servers');
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropForeign(['server_id', 'user_id']);
$table->renameColumn('server_id', 'server');
$table->dropColumn('user_id');
$table->foreign('server')->references('id')->on('servers');
});
}
}

View file

@ -159,7 +159,7 @@
<a href="{{ route('server.tasks', $server->uuidShort)}}"> <a href="{{ route('server.tasks', $server->uuidShort)}}">
<i class="fa fa-clock-o"></i> <span>@lang('navigation.server.task_management')</span> <i class="fa fa-clock-o"></i> <span>@lang('navigation.server.task_management')</span>
<span class="pull-right-container"> <span class="pull-right-container">
<span class="label label-primary pull-right">{{ \Pterodactyl\Models\Task::select('id')->where('server', $server->id)->where('active', 1)->count() }}</span> <span class="label label-primary pull-right">{{ \Pterodactyl\Models\Task::select('id')->where('server_id', $server->id)->where('active', 1)->count() }}</span>
</span> </span>
</a> </a>
</li> </li>

View file

@ -55,7 +55,7 @@
<th></th> <th></th>
</tr> </tr>
@foreach($tasks as $task) @foreach($tasks as $task)
<tr @if($task->active === 0)class="muted muted-hover"@endif> <tr @if(! $task->active)class="muted muted-hover"@endif>
<td class="middle">{{ $actions[$task->action] }}</td> <td class="middle">{{ $actions[$task->action] }}</td>
<td class="middle"><code>{{ $task->data }}</code></td> <td class="middle"><code>{{ $task->data }}</code></td>
<td class="middle"> <td class="middle">