mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 22:46:16 +00:00
#591 - cron jobs
This commit is contained in:
@ -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,
|
||||
|
Reference in New Issue
Block a user