From 446ca3a87877f3ead443812c7dc7b1eeae225d04 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 13 Nov 2023 01:59:38 -0700 Subject: [PATCH] Update notification epoch generation --- .../Commands/NotificationEpochUpdate.php | 31 ++++++++ app/Console/Kernel.php | 1 + .../NotificationEpochUpdatePipeline.php | 71 +++++++++++++++++++ app/Services/NotificationService.php | 13 ++-- 4 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 app/Console/Commands/NotificationEpochUpdate.php create mode 100644 app/Jobs/InternalPipeline/NotificationEpochUpdatePipeline.php diff --git a/app/Console/Commands/NotificationEpochUpdate.php b/app/Console/Commands/NotificationEpochUpdate.php new file mode 100644 index 000000000..e606b47ad --- /dev/null +++ b/app/Console/Commands/NotificationEpochUpdate.php @@ -0,0 +1,31 @@ +command('app:import-remove-deleted-accounts')->hourlyAt(37); $schedule->command('app:import-upload-clean-storage')->twiceDailyAt(1, 13, 32); } + $schedule->command('app:notification-epoch-update')->weeklyOn(1, '2:21'); } /** diff --git a/app/Jobs/InternalPipeline/NotificationEpochUpdatePipeline.php b/app/Jobs/InternalPipeline/NotificationEpochUpdatePipeline.php new file mode 100644 index 000000000..477b1f9b3 --- /dev/null +++ b/app/Jobs/InternalPipeline/NotificationEpochUpdatePipeline.php @@ -0,0 +1,71 @@ + + */ + public function middleware(): array + { + return [(new WithoutOverlapping('ip:notification-epoch-update'))->shared()->dontRelease()]; + } + + /** + * Create a new job instance. + */ + public function __construct() + { + // + } + + /** + * Execute the job. + */ + public function handle(): void + { + $rec = Notification::where('created_at', '>', now()->subMonths(6))->first(); + $id = 1; + if($rec) { + $id = $rec->id; + } + Cache::put(NotificationService::EPOCH_CACHE_KEY . '6', $id, 1209600); + } +} diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index d088c2015..634038baf 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -12,6 +12,7 @@ use App\Transformer\Api\NotificationTransformer; use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use App\Jobs\InternalPipeline\NotificationEpochUpdatePipeline; class NotificationService { @@ -48,12 +49,12 @@ class NotificationService { public static function getEpochId($months = 6) { - return Cache::remember(self::EPOCH_CACHE_KEY . $months, 1209600, function() use($months) { - if(Notification::count() === 0) { - return 0; - } - return Notification::where('created_at', '>', now()->subMonths($months))->first()->id; - }); + $epoch = Cache::get(self::EPOCH_CACHE_KEY . $months); + if(!$epoch) { + NotificationEpochUpdatePipeline::dispatch(); + return 1; + } + return $epoch; } public static function coldGet($id, $start = 0, $stop = 400)