Skip to content

Commit cafafd9

Browse files
authored
Feat/cron job names (#994)
* Optional cron job naming added * Fix: CronJob model, test and form formatting
1 parent 8160170 commit cafafd9

File tree

11 files changed

+76
-1
lines changed

11 files changed

+76
-1
lines changed

app/Actions/CronJob/CreateCronJob.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function create(Server $server, array $input, ?Site $site = null): CronJo
2929
}
3030

3131
$cronJob = new CronJob([
32+
'name' => $input['name'] ?? null,
3233
'server_id' => $server->id,
3334
'site_id' => $siteId,
3435
'user' => $input['user'],
@@ -59,6 +60,11 @@ private function validate(array $input, Server $server, ?Site $site = null): voi
5960
'required',
6061
new CronRule(acceptCustom: true),
6162
],
63+
'name' => [
64+
'nullable',
65+
'string',
66+
'max:255',
67+
],
6268
];
6369

6470
// Add site_id validation if provided in input

app/Actions/CronJob/EditCronJob.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function edit(Server $server, CronJob $cronJob, array $input, ?Site $site
3838

3939
$cronJob->update([
4040
'site_id' => $siteId,
41+
'name' => $input['name'] ?? null,
4142
'user' => $input['user'],
4243
'command' => $input['command'],
4344
'frequency' => $input['frequency'] == 'custom' ? $input['custom'] : $input['frequency'],
@@ -72,6 +73,11 @@ private function validate(array $input, Server $server, ?Site $site = null): voi
7273
'required',
7374
new CronRule(acceptCustom: true),
7475
],
76+
'name' => [
77+
'nullable',
78+
'string',
79+
'max:255',
80+
],
7581
];
7682

7783
// Add site_id validation if provided in input

app/Http/Resources/CronJobResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function toArray(Request $request): array
1616
{
1717
return [
1818
'id' => $this->id,
19+
'name' => $this->name,
1920
'server_id' => $this->server_id,
2021
'site_id' => $this->site_id,
2122
'command' => $this->command,

app/Models/CronJob.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @property string $user
1515
* @property string $frequency
1616
* @property bool $hidden
17+
* @property string|null $name
1718
* @property CronjobStatus $status
1819
* @property string $crontab
1920
* @property Server $server
@@ -32,6 +33,7 @@ class CronJob extends AbstractModel
3233
'frequency',
3334
'hidden',
3435
'status',
36+
'name',
3537
];
3638

3739
protected $casts = [

database/factories/CronJobFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CronJobFactory extends Factory
1616
public function definition(): array
1717
{
1818
return [
19+
'name' => $this->faker->optional()->sentence(3),
1920
'server_id' => 1,
2021
'site_id' => null,
2122
'command' => 'ls -la',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('cron_jobs', function (Blueprint $table) {
15+
$table->string('name')->nullable()->index();
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('cron_jobs', function (Blueprint $table) {
25+
$table->dropColumn('name');
26+
});
27+
}
28+
};

resources/js/pages/cronjobs/components/columns.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ function Delete({ cronJob, site }: { cronJob: CronJob; site?: Site }) {
9696

9797
function getColumns(site?: Site, sites?: Array<{ id: number; domain: string }>): ColumnDef<CronJob>[] {
9898
return [
99+
{
100+
accessorKey: 'name',
101+
header: 'Name',
102+
enableColumnFilter: true,
103+
enableSorting: true,
104+
cell: ({ row }) => {
105+
return <span>{row.original.name || <i className="text-muted-foreground">No name</i>}</span>;
106+
},
107+
},
99108
{
100109
accessorKey: 'command',
101110
header: 'Command',

resources/js/pages/cronjobs/components/form.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export default function CronJobForm({
3636
const page = usePage<SharedData & { server: Server; sites?: Array<{ id: number; domain: string }> }>();
3737
const [open, setOpen] = useState(false);
3838
const form = useForm<{
39+
name: string;
3940
command: string;
4041
user: string;
4142
frequency: string;
4243
custom: string;
4344
site_id: string;
4445
}>({
46+
name: cronJob?.name || '',
4547
command: cronJob?.command || '',
4648
user: cronJob?.user || '',
4749
frequency: cronJob ? (page.props.configs.cronjob_intervals[cronJob.frequency] ? cronJob.frequency : 'custom') : '',
@@ -84,6 +86,20 @@ export default function CronJobForm({
8486
</DialogHeader>
8587
<Form id="cronjob-form" onSubmit={submit} className="p-4">
8688
<FormFields>
89+
{/* Name */}
90+
<FormField>
91+
<Label htmlFor="name">Name</Label>
92+
<Input
93+
type="text"
94+
id="name"
95+
value={form.data.name}
96+
onChange={(e) => form.setData('name', e.target.value)}
97+
placeholder="Optional name for the cron job"
98+
/>
99+
<InputError message={form.errors.name} />
100+
</FormField>
101+
102+
{/* Command */}
87103
<FormField>
88104
<Label htmlFor="command">Command</Label>
89105
<Input type="text" id="command" value={form.data.command} onChange={(e) => form.setData('command', e.target.value)} />

resources/js/types/cronjob.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface CronJob {
22
id: number;
3+
name: string | null;
34
server_id: number;
45
site_id: number | null;
56
command: string;

tests/Feature/API/SiteCronjobTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function test_create_site_cronjob(): void
7474
'command' => 'ls -la',
7575
'user' => 'vito',
7676
'frequency' => '* * * * *',
77+
'name' => 'My Site Cronjob',
7778
])
7879
->assertSuccessful()
7980
->assertJsonFragment([
@@ -91,6 +92,7 @@ public function test_create_site_cronjob(): void
9192
'user' => 'vito',
9293
'frequency' => '* * * * *',
9394
'status' => CronjobStatus::READY,
95+
'name' => 'My Site Cronjob',
9496
]);
9597
}
9698

0 commit comments

Comments
 (0)