Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/Actions/CronJob/CreateCronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function create(Server $server, array $input, ?Site $site = null): CronJo
}

$cronJob = new CronJob([
'name' => $input['name'] ?? null,
'server_id' => $server->id,
'site_id' => $siteId,
'user' => $input['user'],
Expand Down Expand Up @@ -59,6 +60,11 @@ private function validate(array $input, Server $server, ?Site $site = null): voi
'required',
new CronRule(acceptCustom: true),
],
'name' => [
'nullable',
'string',
'max:255',
],
];

// Add site_id validation if provided in input
Expand Down
6 changes: 6 additions & 0 deletions app/Actions/CronJob/EditCronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function edit(Server $server, CronJob $cronJob, array $input, ?Site $site

$cronJob->update([
'site_id' => $siteId,
'name' => $input['name'] ?? null,
'user' => $input['user'],
'command' => $input['command'],
'frequency' => $input['frequency'] == 'custom' ? $input['custom'] : $input['frequency'],
Expand Down Expand Up @@ -72,6 +73,11 @@ private function validate(array $input, Server $server, ?Site $site = null): voi
'required',
new CronRule(acceptCustom: true),
],
'name' => [
'nullable',
'string',
'max:255',
],
];

// Add site_id validation if provided in input
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/CronJobResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'server_id' => $this->server_id,
'site_id' => $this->site_id,
'command' => $this->command,
Expand Down
2 changes: 2 additions & 0 deletions app/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @property string $user
* @property string $frequency
* @property bool $hidden
* @property string|null $name
* @property CronjobStatus $status
* @property string $crontab
* @property Server $server
Expand All @@ -32,6 +33,7 @@ class CronJob extends AbstractModel
'frequency',
'hidden',
'status',
'name',
];

protected $casts = [
Expand Down
1 change: 1 addition & 0 deletions database/factories/CronJobFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CronJobFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->optional()->sentence(3),
'server_id' => 1,
'site_id' => null,
'command' => 'ls -la',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('cron_jobs', function (Blueprint $table) {
$table->string('name')->nullable()->index();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('cron_jobs', function (Blueprint $table) {
$table->dropColumn('name');
});
}
};
9 changes: 9 additions & 0 deletions resources/js/pages/cronjobs/components/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ function Delete({ cronJob, site }: { cronJob: CronJob; site?: Site }) {

function getColumns(site?: Site, sites?: Array<{ id: number; domain: string }>): ColumnDef<CronJob>[] {
return [
{
accessorKey: 'name',
header: 'Name',
enableColumnFilter: true,
enableSorting: true,
cell: ({ row }) => {
return <span>{row.original.name || <i className="text-muted-foreground">No name</i>}</span>;
},
},
{
accessorKey: 'command',
header: 'Command',
Expand Down
16 changes: 16 additions & 0 deletions resources/js/pages/cronjobs/components/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ export default function CronJobForm({
const page = usePage<SharedData & { server: Server; sites?: Array<{ id: number; domain: string }> }>();
const [open, setOpen] = useState(false);
const form = useForm<{
name: string;
command: string;
user: string;
frequency: string;
custom: string;
site_id: string;
}>({
name: cronJob?.name || '',
command: cronJob?.command || '',
user: cronJob?.user || '',
frequency: cronJob ? (page.props.configs.cronjob_intervals[cronJob.frequency] ? cronJob.frequency : 'custom') : '',
Expand Down Expand Up @@ -84,6 +86,20 @@ export default function CronJobForm({
</DialogHeader>
<Form id="cronjob-form" onSubmit={submit} className="p-4">
<FormFields>
{/* Name */}
<FormField>
<Label htmlFor="name">Name</Label>
<Input
type="text"
id="name"
value={form.data.name}
onChange={(e) => form.setData('name', e.target.value)}
placeholder="Optional name for the cron job"
/>
<InputError message={form.errors.name} />
</FormField>

{/* Command */}
<FormField>
<Label htmlFor="command">Command</Label>
<Input type="text" id="command" value={form.data.command} onChange={(e) => form.setData('command', e.target.value)} />
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/cronjob.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface CronJob {
id: number;
name: string | null;
server_id: number;
site_id: number | null;
command: string;
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/API/SiteCronjobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function test_create_site_cronjob(): void
'command' => 'ls -la',
'user' => 'vito',
'frequency' => '* * * * *',
'name' => 'My Site Cronjob',
])
->assertSuccessful()
->assertJsonFragment([
Expand All @@ -91,6 +92,7 @@ public function test_create_site_cronjob(): void
'user' => 'vito',
'frequency' => '* * * * *',
'status' => CronjobStatus::READY,
'name' => 'My Site Cronjob',
]);
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/CronjobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function test_see_cronjobs_list(): void
$this->get(route('cronjobs', $this->server))
->assertSuccessful()
->assertInertia(fn (AssertableInertia $page) => $page->component('cronjobs/index'));

}

public function test_delete_cronjob(): void
Expand Down Expand Up @@ -64,6 +63,7 @@ public function test_create_cronjob(): void
'command' => 'ls -la',
'user' => 'vito',
'frequency' => '* * * * *',
'name' => 'My Cronjob',
])
->assertSessionDoesntHaveErrors();

Expand All @@ -73,6 +73,7 @@ public function test_create_cronjob(): void
'user' => 'vito',
'frequency' => '* * * * *',
'status' => CronjobStatus::READY,
'name' => 'My Cronjob',
]);

SSH::assertExecutedContains("echo '* * * * * ls -la' | sudo -u vito crontab -");
Expand Down Expand Up @@ -326,13 +327,15 @@ public function test_edit_cronjob_with_valid_site_id(): void
'user' => 'vito',
'frequency' => '* * * * *',
'site_id' => $site->id,
'name' => 'Updated Cronjob',
])
->assertSessionDoesntHaveErrors();

$cronjob->refresh();

$this->assertEquals($site->id, $cronjob->site_id);
$this->assertEquals('updated command', $cronjob->command);
$this->assertEquals('Updated Cronjob', $cronjob->name);
}

public function test_cannot_edit_cronjob_with_invalid_site_id(): void
Expand Down