DispatchDueEmails.php 752 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\EmailMessage;
  5. use App\Jobs\SendEmailMessage;
  6. class DispatchDueEmails extends Command
  7. {
  8. protected $signature = 'emails:dispatch-due';
  9. protected $description = 'Invia le email programmate giunte a scadenza';
  10. public function handle()
  11. {
  12. app(\App\Http\Middleware\TenantMiddleware::class)->setupTenantConnection();
  13. EmailMessage::where('status', 'scheduled')
  14. ->where('schedule_at', '<=', now())
  15. ->chunkById(100, function ($chunk) {
  16. foreach ($chunk as $msg) {
  17. dispatch(new SendEmailMessage($msg->id));
  18. }
  19. });
  20. return Command::SUCCESS;
  21. }
  22. }