Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DR-12034-housekeeping-eventlogs] Adds Job for cleaning up event_logs table #21

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions app/Jobs/EventLogCleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Jobs;

use DB;
use Log;

use Carbon\Carbon;

class EventLogCleanupJob extends Job
{
public function handle()
{
$keepUntil = Carbon::now()->subDay();

DB::table('event_logs')
->where('created_at', '<', $keepUntil->toDateTimeString())
->delete();

Log::notice(sprintf(
'Deleted event log entries before %s.',
$keepUntil->toRfc2822String()
));
}
}
32 changes: 32 additions & 0 deletions tests/Jobs/EventLogCleanupJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Jobs;

use App\Jobs\EventLogCleanupJob;
use App\Models\EventLog;
use Tests\Traits\DatabaseMigrations;
use Tests\TestCase;
use Carbon\Carbon;
use DB;
use Log;

class EventLogCleanupJobTest extends TestCase
{
use DatabaseMigrations;

public function testHandle()
{
$newCount = rand(1, 10);
$oldEventLogs = factory(EventLog::class, 100)->create([
'created_at' => Carbon::now()->subDay()->subSecond(),
]);
$newEventLogs = factory(EventLog::class, $newCount)->create();

Log::shouldReceive('notice');

$job = new EventLogCleanupJob();
$job->handle();

$this->assertEquals($newCount, DB::table('event_logs')->count());
}
}