0

I want to email a potentially large number of clients, so I am using Batches and pushing each email send as a batched job, like this:

public function __construct(RunReport $runReport, User $run_by) {
        $this->runReport = $runReport;
        $this->run_by = $run_by;
    }

    public function handle()
    {
        $company_detail = CompanyDetail::first();

        $jobs = $this->runReport->runReportReportees
                ->map(fn(RunReportReportee $runReportReportee) => new EmailStatementJob($runReportReportee, $company_detail))
                ->toArray();

        $batch = Bus
            ::batch($jobs)
            ->then(function(Batch $batch) {
                // All completed
                $completed = ($batch->totalJobs - $batch->failedJobs);
                
                $message = "foo";
                $type = "bar";

                $this->run_by->notify(new GenericNotification($message, $type, 'envelope'));
            })
            ->allowFailures()
            ->name('Batch name here trust me')
            ->dispatch();

        return $batch->id;
    }

However the notify line causes an error Serialization of 'Doctrine\DBAL\Driver\PDOConnection' is not allowed.

How can I notify the user that initiated the batch when the batch is finished, with the results of the included email send attempts? Alternatively, how else should I email a few hundred clients and notify the user of the results?

Healyhatman
  • 1,482
  • 1
  • 14
  • 23
  • Searching for your error msg turns up many explanations and suggestions here on SO, have you been through all of those? They suggest you have something which cannot be serialised in your `GenericNotification`: https://stackoverflow.com/questions/43643626/laravel-queueable-notification-errors-serialization-of-closure-is-not-allowed, https://stackoverflow.com/questions/47475892/laravel-queue-serialization-of-closure-is-not-allowed, https://stackoverflow.com/questions/66420024/serialization-of-doctrine-dbal-driver-pdoconnection-is-not-allowed ... – Don't Panic Sep 23 '21 at 08:16
  • Yeah but I couldn't understand what was being serialised. All sorted. – Healyhatman Sep 23 '21 at 23:04

1 Answers1

2

I figured it out eventually with help - I was using $this in the ->then() callback which was my first mistake. Then instead of the notification in the job, I have instead stored the user ID and passed it to the then() callback like this

->then(function(Batch $batch) use ($run_by_id) { ... }

In the callback instead of notifying the user, I call an event

event(new HcpStatementsBatchFinishedEvent($batch, $id));

The event simply stores the information

public function __construct(Batch $batch, int $run_by_sysid)
{
    $this->run_by = User::find($run_by_id);
    $this->batch = $batch;
}

And the listener for the event builds the message and notifies the user.

Healyhatman
  • 1,482
  • 1
  • 14
  • 23