-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule periodic tasks with Laravel (#2222)
NOTE: This PR moves cleanup logic out of the request-triggered daily update script. To continue having these cleanup tasks performed automatically, administrators of non-Docker systems must run `php artisan schedule:run` via cron or similar. The current "daily update" process is clunky, and in dire need of a refactor. This PR takes the first step towards modernizing the daily update process by separating out a few small portions of the process into independent "tasks" managed by Laravel. This PR also develops the infrastructure needed to eventually perform LDAP syncing for #1983. Laravel overhauled the task system in Laravel 11, but we can't upgrade to Laravel 11 until Red Hat releases a UBI image with PHP 8.2+.
- Loading branch information
1 parent
2f02587
commit d1bf379
Showing
12 changed files
with
254 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\AuthToken; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
/** | ||
* Removes expired auth tokens. | ||
*/ | ||
class PruneAuthTokens implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
AuthToken::expired()->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\SuccessfulJob; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Artisan; | ||
|
||
/** | ||
* Removes job results which are expired. Job lifetime is controlled by the BACKUP_TIMEFRAME | ||
* configuration option. | ||
*/ | ||
class PruneJobs implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$lifetime = config('cdash.backup_timeframe'); | ||
|
||
Artisan::call("queue:prune-failed --hours=$lifetime"); | ||
|
||
// The successful_jobs table is a CDash specific table, so we have to prune it manually | ||
SuccessfulJob::where('finished_at', '<', Carbon::now()->subHours($lifetime))->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Jobs; | ||
|
||
use App\Jobs\PruneAuthTokens; | ||
use App\Models\AuthToken; | ||
use App\Models\User; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Str; | ||
use Tests\TestCase; | ||
use Tests\Traits\CreatesUsers; | ||
|
||
class PruneAuthTokensTest extends TestCase | ||
{ | ||
use CreatesUsers; | ||
|
||
protected User $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = $this->makeNormalUser(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->user->delete(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testExpiredAuthTokenDeleted(): void | ||
{ | ||
$hash = Str::uuid()->toString(); | ||
AuthToken::create([ | ||
'hash' => $hash, | ||
'expires' => Carbon::now()->subMinute(), | ||
'scope' => 'test', | ||
'userid' => $this->user->id, | ||
]); | ||
|
||
self::assertNotNull(Authtoken::find($hash)); | ||
|
||
PruneAuthTokens::dispatch(); | ||
|
||
self::assertNull(Authtoken::find($hash)); | ||
} | ||
|
||
public function testValidAuthTokenNotDeleted(): void | ||
{ | ||
$hash = Str::uuid()->toString(); | ||
AuthToken::create([ | ||
'hash' => $hash, | ||
'expires' => Carbon::now()->addMinute(), | ||
'scope' => 'test', | ||
'userid' => $this->user->id, | ||
]); | ||
|
||
self::assertNotNull(Authtoken::find($hash)); | ||
|
||
PruneAuthTokens::dispatch(); | ||
|
||
self::assertNotNull(Authtoken::find($hash)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Jobs; | ||
|
||
use App\Jobs\PruneJobs; | ||
use App\Models\SuccessfulJob; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Str; | ||
use Tests\TestCase; | ||
|
||
class PruneJobsTest extends TestCase | ||
{ | ||
/** | ||
* Changing the config is difficult since multiple processes are involved. | ||
* Instead, we just rely upon the default value of 48 hours. | ||
*/ | ||
public function testExpiredSuccessfulJobDeleted(): void | ||
{ | ||
$filename = 'test-filename' . Str::uuid()->toString(); | ||
$job = new SuccessfulJob([ | ||
'filename' => $filename, | ||
]); | ||
$job->finished_at = Carbon::now()->subHours(1000); // finished_at isn't fillable... | ||
$job->save(); | ||
|
||
self::assertEquals(1, SuccessfulJob::where('filename', $filename)->count()); | ||
|
||
PruneJobs::dispatch(); | ||
|
||
self::assertEquals(0, SuccessfulJob::where('filename', $filename)->count()); | ||
} | ||
|
||
public function testRecentSuccessfulJobNotDeleted(): void | ||
{ | ||
$filename = 'test-filename' . Str::uuid()->toString(); | ||
// The timestamp defaults to NOW(). | ||
SuccessfulJob::create([ | ||
'filename' => $filename, | ||
]); | ||
|
||
self::assertEquals(1, SuccessfulJob::where('filename', $filename)->count()); | ||
|
||
PruneJobs::dispatch(); | ||
|
||
self::assertEquals(1, SuccessfulJob::where('filename', $filename)->count()); | ||
} | ||
} |