-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Send 'new pool' emails overnight (#11804)
* remove old observer and event * new artisan command * no publishing group other * add schedule * don't send closed pools * fix linting * update tests * fix comment
- Loading branch information
1 parent
b9911a5
commit 3cbb385
Showing
8 changed files
with
141 additions
and
155 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
api/app/Console/Commands/SendNotificationsPoolPublished.php
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,108 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Enums\NotificationFamily; | ||
use App\Enums\PublishingGroup; | ||
use App\Models\Pool; | ||
use App\Models\User; | ||
use App\Notifications\NewJobPosted; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Log; | ||
use Throwable; | ||
|
||
class SendNotificationsPoolPublished extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'send-notifications:pool-published'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send notifications to users about a new pool being published.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
// workaround until we get better logging in prod #11289 | ||
$onDemandLog = Log::build([ | ||
'driver' => 'single', | ||
'path' => App::isProduction() // workaround for storage_path misconfigured in prod #11471 | ||
? '/tmp/api/storage/logs/jobs.log' | ||
: storage_path('logs/jobs.log'), | ||
]); | ||
|
||
$this->info('SendNotificationsPoolPublished running at '.Carbon::now()->toDateTimeString().'.'); | ||
|
||
$successCount = 0; | ||
$failureCount = 0; | ||
|
||
$endOfSpan = Carbon::now(); | ||
$startOfSpan = Carbon::now()->subHours(24); // assuming the last reporting job ran about 24 hours ago | ||
$this->info("Finding pools published between $startOfSpan and $endOfSpan."); | ||
|
||
$poolsPublishedRecently = Pool::query() | ||
->where('published_at', '>=', $startOfSpan) | ||
->where('published_at', '<', $endOfSpan) | ||
->whereNotClosed() // don't notify of pools that have already been closed | ||
->where('publishing_group', '<>', PublishingGroup::OTHER->name) // don't notify of testing pools | ||
->get(); | ||
|
||
$this->info('Found '.$poolsPublishedRecently->count().' pools.'); | ||
|
||
$notifications = $poolsPublishedRecently | ||
->map(fn ($model) => get_class($model) == Pool::class | ||
? new NewJobPosted( | ||
$model->name['en'], | ||
$model->name['fr'], | ||
$model->id | ||
) | ||
: null | ||
) | ||
->whereNotNull(); | ||
|
||
$successCount = 0; | ||
$failureCount = 0; | ||
|
||
if ($notifications->count() > 0) { | ||
User::whereJsonContains('enabled_email_notifications', NotificationFamily::JOB_ALERT->name) | ||
->orWhereJsonContains('enabled_in_app_notifications', NotificationFamily::JOB_ALERT->name) | ||
->chunk(200, function (Collection $users) use ($notifications, &$successCount, &$failureCount, $onDemandLog) { | ||
foreach ($users as $user) { | ||
foreach ($notifications as $notification) { | ||
try { | ||
$user->notify($notification); | ||
$successCount++; | ||
} catch (Throwable $e) { | ||
// best-effort: log and continue | ||
$onDemandLog->error('Failed to send "new job posted" notification for "'.$notification->poolNameEn.'" ('.$notification->poolId.') to user " '.$user->first_name.' '.$user->last_name.' ('.$user->id.'). '.$e->getMessage()); | ||
$failureCount++; | ||
} | ||
} | ||
} | ||
}); | ||
} else { | ||
$this->info('No notifications to send.'); | ||
} | ||
|
||
$this->info("Success: $successCount Failure: $failureCount"); | ||
if ($failureCount > 0) { | ||
return Command::FAILURE; | ||
} else { | ||
return Command::SUCCESS; | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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