mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +00:00
#591 - cron jobs
This commit is contained in:
@ -3,18 +3,23 @@
|
||||
namespace App\Actions\CronJob;
|
||||
|
||||
use App\Enums\CronjobStatus;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
use App\ValidationRules\CronRule;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CreateCronJob
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @param array<string, mixed> $input
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function create(Server $server, array $input): CronJob
|
||||
{
|
||||
Validator::make($input, self::rules($input, $server))->validate();
|
||||
|
||||
$cronJob = new CronJob([
|
||||
'server_id' => $server->id,
|
||||
'user' => $input['user'],
|
||||
@ -33,7 +38,7 @@ public function create(Server $server, array $input): CronJob
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, array<mixed>>
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
public static function rules(array $input, Server $server): array
|
||||
{
|
||||
|
@ -3,11 +3,15 @@
|
||||
namespace App\Actions\CronJob;
|
||||
|
||||
use App\Enums\CronjobStatus;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
|
||||
class DeleteCronJob
|
||||
{
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function delete(Server $server, CronJob $cronJob): void
|
||||
{
|
||||
$user = $cronJob->user;
|
||||
|
@ -3,11 +3,15 @@
|
||||
namespace App\Actions\CronJob;
|
||||
|
||||
use App\Enums\CronjobStatus;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
|
||||
class DisableCronJob
|
||||
{
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function disable(Server $server, CronJob $cronJob): void
|
||||
{
|
||||
$cronJob->status = CronjobStatus::DISABLING;
|
||||
|
68
app/Actions/CronJob/EditCronJob.php
Executable file
68
app/Actions/CronJob/EditCronJob.php
Executable file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\CronJob;
|
||||
|
||||
use App\Enums\CronjobStatus;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
use App\ValidationRules\CronRule;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class EditCronJob
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
*
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function edit(Server $server, CronJob $cronJob, array $input): CronJob
|
||||
{
|
||||
Validator::make($input, self::rules($input, $server))->validate();
|
||||
|
||||
$cronJob->update([
|
||||
'user' => $input['user'],
|
||||
'command' => $input['command'],
|
||||
'frequency' => $input['frequency'] == 'custom' ? $input['custom'] : $input['frequency'],
|
||||
'status' => CronjobStatus::UPDATING,
|
||||
]);
|
||||
$cronJob->save();
|
||||
|
||||
$server->cron()->update($cronJob->user, CronJob::crontab($server, $cronJob->user));
|
||||
$cronJob->status = CronjobStatus::READY;
|
||||
$cronJob->save();
|
||||
|
||||
return $cronJob;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
public static function rules(array $input, Server $server): array
|
||||
{
|
||||
$rules = [
|
||||
'command' => [
|
||||
'required',
|
||||
],
|
||||
'user' => [
|
||||
'required',
|
||||
Rule::in($server->getSshUsers()),
|
||||
],
|
||||
'frequency' => [
|
||||
'required',
|
||||
new CronRule(acceptCustom: true),
|
||||
],
|
||||
];
|
||||
|
||||
if (isset($input['frequency']) && $input['frequency'] == 'custom') {
|
||||
$rules['custom'] = [
|
||||
'required',
|
||||
new CronRule,
|
||||
];
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
@ -3,11 +3,15 @@
|
||||
namespace App\Actions\CronJob;
|
||||
|
||||
use App\Enums\CronjobStatus;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
|
||||
class EnableCronJob
|
||||
{
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function enable(Server $server, CronJob $cronJob): void
|
||||
{
|
||||
$cronJob->status = CronjobStatus::ENABLING;
|
||||
|
@ -14,5 +14,7 @@ final class CronjobStatus
|
||||
|
||||
const DISABLING = 'disabling';
|
||||
|
||||
const UPDATING = 'updating';
|
||||
|
||||
const DISABLED = 'disabled';
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Actions\CronJob\CreateCronJob;
|
||||
use App\Actions\CronJob\DeleteCronJob;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CronJobResource;
|
||||
use App\Models\CronJob;
|
||||
@ -39,6 +40,9 @@ public function index(Project $project, Server $server): ResourceCollection
|
||||
return CronJobResource::collection($server->cronJobs()->simplePaginate(25));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/', name: 'api.projects.servers.cron-jobs.create', middleware: 'ability:write')]
|
||||
#[Endpoint(title: 'create', description: 'Create a new cron job.')]
|
||||
#[BodyParam(name: 'command', required: true)]
|
||||
@ -51,8 +55,6 @@ public function create(Request $request, Project $project, Server $server): Cron
|
||||
|
||||
$this->validateRoute($project, $server);
|
||||
|
||||
$this->validate($request, CreateCronJob::rules($request->all(), $server));
|
||||
|
||||
$cronJob = app(CreateCronJob::class)->create($server, $request->all());
|
||||
|
||||
return new CronJobResource($cronJob);
|
||||
@ -70,6 +72,9 @@ public function show(Project $project, Server $server, CronJob $cronJob): CronJo
|
||||
return new CronJobResource($cronJob);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Delete('{cronJob}', name: 'api.projects.servers.cron-jobs.delete', middleware: 'ability:write')]
|
||||
#[Endpoint(title: 'delete', description: 'Delete cron job.')]
|
||||
#[Response(status: 204)]
|
||||
|
108
app/Http/Controllers/CronJobController.php
Normal file
108
app/Http/Controllers/CronJobController.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\CronJob\CreateCronJob;
|
||||
use App\Actions\CronJob\DeleteCronJob;
|
||||
use App\Actions\CronJob\DisableCronJob;
|
||||
use App\Actions\CronJob\EditCronJob;
|
||||
use App\Actions\CronJob\EnableCronJob;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Http\Resources\CronJobResource;
|
||||
use App\Models\CronJob;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Spatie\RouteAttributes\Attributes\Delete;
|
||||
use Spatie\RouteAttributes\Attributes\Get;
|
||||
use Spatie\RouteAttributes\Attributes\Middleware;
|
||||
use Spatie\RouteAttributes\Attributes\Post;
|
||||
use Spatie\RouteAttributes\Attributes\Prefix;
|
||||
use Spatie\RouteAttributes\Attributes\Put;
|
||||
|
||||
#[Prefix('servers/{server}/cronjobs')]
|
||||
#[Middleware(['auth', 'has-project'])]
|
||||
class CronJobController extends Controller
|
||||
{
|
||||
#[Get('/', name: 'cronjobs')]
|
||||
public function index(Server $server): Response
|
||||
{
|
||||
$this->authorize('viewAny', [CronJob::class, $server]);
|
||||
|
||||
return Inertia::render('cronjobs/index', [
|
||||
'cronjobs' => CronJobResource::collection($server->cronJobs()->latest()->simplePaginate(config('web.pagination_size'))),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/', name: 'cronjobs.store')]
|
||||
public function store(Request $request, Server $server): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', [CronJob::class, $server]);
|
||||
|
||||
app(CreateCronJob::class)->create($server, $request->all());
|
||||
|
||||
return back()
|
||||
->with('success', 'Cron job has been created.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Put('/{cronJob}', name: 'cronjobs.update')]
|
||||
public function update(Request $request, Server $server, CronJob $cronJob): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $cronJob);
|
||||
|
||||
app(EditCronJob::class)->edit($server, $cronJob, $request->all());
|
||||
|
||||
return back()
|
||||
->with('success', 'Cron job has been updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/{cronJob}/enable', name: 'cronjobs.enable')]
|
||||
public function enable(Server $server, CronJob $cronJob): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $cronJob);
|
||||
|
||||
app(EnableCronJob::class)->enable($server, $cronJob);
|
||||
|
||||
return back()
|
||||
->with('success', 'Cron job has been enabled.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/{cronJob}/disable', name: 'cronjobs.disable')]
|
||||
public function disable(Server $server, CronJob $cronJob): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $cronJob);
|
||||
|
||||
app(DisableCronJob::class)->disable($server, $cronJob);
|
||||
|
||||
return back()
|
||||
->with('success', 'Cron job has been disabled.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Delete('/{cronJob}', name: 'cronjobs.destroy')]
|
||||
public function destroy(Server $server, CronJob $cronJob): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $cronJob);
|
||||
|
||||
app(DeleteCronJob::class)->delete($server, $cronJob);
|
||||
|
||||
return back()
|
||||
->with('success', 'Cron job has been deleted.');
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ public function toArray(Request $request): array
|
||||
'user' => $this->user,
|
||||
'frequency' => $this->frequency,
|
||||
'status' => $this->status,
|
||||
'status_color' => CronJob::$statusColors[$this->status] ?? 'gray',
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
|
@ -21,6 +21,7 @@ public function toArray(Request $request): array
|
||||
'provider_id' => $this->provider_id,
|
||||
'name' => $this->name,
|
||||
'ssh_user' => $this->ssh_user,
|
||||
'ssh_users' => $this->getSshUsers(),
|
||||
'ip' => $this->ip,
|
||||
'local_ip' => $this->local_ip,
|
||||
'port' => $this->port,
|
||||
|
@ -63,6 +63,7 @@ public static function crontab(Server $server, string $user): string
|
||||
->whereIn('status', [
|
||||
CronjobStatus::READY,
|
||||
CronjobStatus::CREATING,
|
||||
CronjobStatus::UPDATING,
|
||||
CronjobStatus::ENABLING,
|
||||
])
|
||||
->get();
|
||||
|
Reference in New Issue
Block a user