Skip to content

Commit

Permalink
Install cron to cleanup working directories
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Jul 11, 2023
1 parent 0d4e699 commit 3141e15
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
41 changes: 41 additions & 0 deletions app/Jobs/InstallTaskCleanupCron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Jobs;

use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class InstallTaskCleanupCron implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct(public Server $server)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$workingDirectory = $this->server->working_directory;

$command = implode(' ; ', [
"find /root/{$workingDirectory} -name \"task-*\" -type f -mtime +7 -exec rm {} \;",
"find /home/eddy/{$workingDirectory} -name \"task-*\" -type f -mtime +7 -exec rm {} \;",
]);

$this->server->uploadAsRoot(
'/etc/cron.d/eddy-tasks-cleanup',
view('tasks.eddy-tasks-cleanup', ['command' => $command])->render()
);
}
}
2 changes: 2 additions & 0 deletions app/Tasks/ProvisionFreshServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enum;
use App\Infrastructure\Entities\ServerStatus;
use App\Jobs\CleanupFailedServerProvisioning;
use App\Jobs\InstallTaskCleanupCron;
use App\Jobs\UpdateUserPublicKey;
use App\Mail\ServerProvisioned;
use App\Models\FirewallRule;
Expand Down Expand Up @@ -48,6 +49,7 @@ protected function onFinished(TaskModel $task, Request $request)
'status' => ServerStatus::Running,
])->save();

dispatch(new InstallTaskCleanupCron($this->server));
dispatch(new UpdateUserPublicKey($this->server));

Mail::to($this->server->createdByUser)->queue(new ServerProvisioned($this->server));
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/server/cron.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

{{ $cron->expression }} {{ $cron->user }} {{ $cron->command }} > {{ $cron->logPath() }} 2>&1
{{ $cron->expression }} {{ $cron->user }} {!! $cron->command !!} > {{ $cron->logPath() }} 2>&1
4 changes: 4 additions & 0 deletions resources/views/tasks/eddy-tasks-cleanup.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 0 * * * root ({!! $command !!}) 2>&1
34 changes: 34 additions & 0 deletions tests/Unit/Jobs/InstallTaskCleanupCronTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Unit\Jobs;

use App\Jobs\InstallTaskCleanupCron;
use App\Models\Server;
use App\Tasks\UploadFile;
use Database\Factories\ServerFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use ProtoneMedia\LaravelTaskRunner\Facades\TaskRunner;
use Tests\TestCase;

class InstallTaskCleanupCronTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_uploads_the_cron_file()
{
TaskRunner::fake();

/** @var Server */
$server = ServerFactory::new()->create();

$job = new InstallTaskCleanupCron($server);
$job->handle();

TaskRunner::assertDispatched(function (UploadFile $task) {
$this->assertMatchesSnapshot($task->contents);

return $task->path === '/etc/cron.d/eddy-tasks-cleanup';
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 0 * * * root (find /root/.eddy -name "task-*" -type f -mtime +7 -exec rm {} \; ; find /home/eddy/.eddy -name "task-*" -type f -mtime +7 -exec rm {} \;) 2>&1
5 changes: 5 additions & 0 deletions tests/Unit/Tasks/ProvisionFreshServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\Tasks;

use App\Infrastructure\Entities\ServerStatus;
use App\Jobs\InstallTaskCleanupCron;
use App\Jobs\UpdateUserPublicKey;
use App\Mail\ServerProvisioned;
use App\Models\Server;
Expand Down Expand Up @@ -36,6 +37,10 @@ public function it_adds_the_default_firewall_rules_and_status_when_finished_and_
$this->assertEquals(ServerStatus::Running, $server->fresh()->status);
$this->assertEquals([22, 80, 443], $server->firewallRules->map->port->all());

Bus::assertDispatched(function (InstallTaskCleanupCron $job) use ($server) {
return $job->server->is($server);
});

Bus::assertDispatched(function (UpdateUserPublicKey $job) use ($server) {
return $job->server->is($server);
});
Expand Down

0 comments on commit 3141e15

Please sign in to comment.