-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prune sent/cancelled notifications (#37)
* feat: prune sent/cancelled notifications * docs: update readme + typos * fix: turn off by default
- Loading branch information
Showing
5 changed files
with
147 additions
and
2 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
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,59 @@ | ||
<?php | ||
|
||
namespace Thomasjohnkane\Snooze\Console\Commands; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Thomasjohnkane\Snooze\Models\ScheduledNotification; | ||
|
||
class PruneScheduledNotifications extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'snooze:prune'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Prune scheduled notifications that have been sent or cancelled'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle(): void | ||
{ | ||
$pruneDays = config('snooze.pruneAge'); | ||
|
||
if ($pruneDays === null) { | ||
$this->error('Pruning of scheduled notifications is disabled'); | ||
|
||
return; | ||
} | ||
|
||
$pruneBeforeDate = Carbon::now()->subDays($pruneDays); | ||
|
||
$notifications = ScheduledNotification::where(function ($query) { | ||
$query->where('sent_at', '!=', null); | ||
$query->orWhere('cancelled_at', '!=', null); | ||
})->where(function ($query) use ($pruneBeforeDate) { | ||
$query->where('sent_at', '<=', $pruneBeforeDate); | ||
$query->orWhere('cancelled_at', '<=', $pruneBeforeDate); | ||
}); | ||
|
||
$totalDeleted = 0; | ||
|
||
do { | ||
$deleted = $notifications->take(1000)->delete(); | ||
$totalDeleted += $deleted; | ||
} while ($deleted !== 0); | ||
|
||
$this->info(sprintf('Pruned %d scheduled notifications', $totalDeleted)); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
namespace Thomasjohnkane\Snooze\Tests; | ||
|
||
use Carbon\Carbon; | ||
use Thomasjohnkane\Snooze\Models\ScheduledNotification as ScheduledNotificationModel; | ||
use Thomasjohnkane\Snooze\Tests\Models\User; | ||
use Thomasjohnkane\Snooze\Tests\Notifications\TestNotification; | ||
|
||
class PruneCommandTest extends TestCase | ||
{ | ||
public function testItCanBeDisabled() | ||
{ | ||
$this->artisan('snooze:prune') | ||
->expectsOutput('Pruning of scheduled notifications is disabled'); | ||
} | ||
|
||
public function testItDoesNotPruneRecentNotifications() | ||
{ | ||
$this->app->config->set('snooze.pruneAge', 30); | ||
|
||
$target = User::find(1); | ||
|
||
$target->notifyAt(new TestNotification(User::find(2)), Carbon::now()); | ||
$target->notifyAt(new TestNotification(User::find(2)), Carbon::now()); | ||
$target->notifyAt(new TestNotification(User::find(2)), Carbon::now()); | ||
|
||
$this->artisan('snooze:prune') | ||
->expectsOutput('Pruned 0 scheduled notifications') | ||
->assertExitCode(0); | ||
} | ||
|
||
public function testPrunesCorrectNotifications() | ||
{ | ||
$this->app->config->set('snooze.pruneAge', 30); | ||
|
||
$target = User::find(1); | ||
|
||
$notification = $target->notifyAt(new TestNotification(User::find(2)), Carbon::now()); | ||
$base = ScheduledNotificationModel::find($notification->getId()); | ||
|
||
$sent2MonthsAgo = $base->replicate(); | ||
$sent2MonthsAgo->sent_at = Carbon::now()->subMonths(2); | ||
$sent2MonthsAgo->save(); | ||
|
||
$cancelled2MonthsAgo = $base->replicate(); | ||
$cancelled2MonthsAgo->cancelled_at = Carbon::now()->subMonths(2); | ||
$cancelled2MonthsAgo->save(); | ||
|
||
$sent1WeekAgo = $base->replicate(); | ||
$sent1WeekAgo->sent_at = Carbon::now()->subWeek(); | ||
$sent1WeekAgo->save(); | ||
|
||
$cancelled1WeekAgo = $base->replicate(); | ||
$cancelled1WeekAgo->cancelled_at = Carbon::now()->subWeek(); | ||
$cancelled1WeekAgo->save(); | ||
|
||
$this->artisan('snooze:prune') | ||
->expectsOutput('Pruned 2 scheduled notifications') | ||
->assertExitCode(0); | ||
|
||
$this->assertDatabaseMissing('scheduled_notifications', ['id' => $sent2MonthsAgo->id]); | ||
$this->assertDatabaseMissing('scheduled_notifications', ['id' => $cancelled2MonthsAgo->id]); | ||
|
||
$this->assertDatabaseHas('scheduled_notifications', ['id' => $base->id]); | ||
$this->assertDatabaseHas('scheduled_notifications', ['id' => $sent1WeekAgo->id]); | ||
$this->assertDatabaseHas('scheduled_notifications', ['id' => $cancelled1WeekAgo->id]); | ||
} | ||
} |