Laravel notify user with results when batched jobs have finished - laravel

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?

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.

Related

How To Check If Notification Is Sent Successfully In Laravel

I am unable to find in the documentation of how to handle the notification (what to do if notification is sent successfully or not)
NotificationController
public function AskForLevelUp()
{
$admin = User::where('is_admin', 1)->first();
$sendNotification = Notification::send($admin, new LevelUpNotification(Auth::user()->id));
if ($sendNotification->success()) // **HOW TO DO THIS CORRECTLY**
{
return back()->with('success', 'Your request has been submitted, please wait for admin confirmation.');
}
else
{
return back()->with('failure', 'Something went wrong, your request is not submitted. Please try again later.');
}
}
I've also read that send() has void return value here. Does anyone know how to handle this?

Laravel Console Command Failing to Complete Execution

I have the script below which is used to send messages (sms, email & whatsapp) to some 300k customers. Things where working fine before but for some days now the script is bugging (it freezes at some point in the loop. I can see this with the progress bar which doesn't move further). I have checked laravel.log and there is no particular error message. The script itself does not output any error message.
Below is the code:
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
AccountsCstm::where('authentification_c', 1)->chunk(10000, function ($customer_details) {
$bar = $this->output->createProgressBar(count($customer_details));
$bar->start();
foreach ($customer_details as $customer_detail) {
// Get customer bills and send through the appropriate channel. He could have multiple bills
if (isset($customer_detail->account->sic_code)) {
$bills = TbBillsInfos::where('contract_number', $customer_detail->account->sic_code)->where('bill_status', 'UNPAID')->get();
if (isset($bills)){
foreach ($bills as $bill) {
// Send messages.
if ($customer_detail->is_whatsapp_c) {
$message = $this->bill_message($customer_detail, $bill, 'WhatsApp');
// Send the message
$this->send_whatsapp_message($customer_detail->phone_4_c, $message, $customer_detail->contract_number_c);
// Record the message in database
$this->save_message('WhatsApp', $customer_detail->phone_4_c, null, $message, $customer_detail->account->id);
} elseif ($customer_detail->is_sms_c) {
$message = $this->bill_message($customer_detail, $bill, 'SMS');
// Send the message
$this->send_sms($customer_detail->phone_4_c, $message);
// Record the message in database
$this->save_message('SMS', $customer_detail->phone_4_c, null, $message, $customer_detail->account->id);
} elseif ($customer_detail->is_mail_c) {
$message = $this->bill_message($customer_detail, $bill, 'Email');
// Send the message
$this->send_email($customer_detail, $bill, $message);
// Record the message in database
$this->save_message('Email', null, $customer_detail->mail_c, $message, $customer_detail->account->id);
}
}
}
}
$bar->advance();
}
$bar->finish();
});
// Delete all record from the tb_bills_infos table
// Set the auto-increment value to 0
TbBillsInfos::truncate();
Log::info('send:unpaid-bill-messages: Command executed successfully');
}
Please can someone point out what can be the issue with the above script? Why is it bugging and not completing execution ? Why is it freezing at some point in the loop?
See for example the command since April 21, where as before it will complete execution in about 15 minutes or less
I have equally checked RAM usage on the server and only 1GB out of 8GB is being used. So the server is stable

Successful request/response from saga leaves Canceled message in saga skipped queue

After much toil and trial and error I managed to issue a "request" from my saga and see it handle the response. My jubilation was cut short however by the appearance of a message in my states' skipped queue. (i'm using azure service bus)
It is of type "urn:message:MassTransit.Scheduling:CancelScheduledMessage".
I am a complete newbie at with mass transit and I'm just trying to get a contrived example going.
My saga calls TaxiToRunway/TaxiingComplete. My bit of saga code
Request(()=>TaxiToRunway, config =>
{
config.Timeout = TimeSpan.FromSeconds(30);
});
...
public Request<PlaneState, TaxiToRunway, TaxiingComplete> TaxiToRunway { get; private set; }
...
Initially(
When(ReadyToDepart)
.Then(context =>
{
context.Saga.Altitude = 0;
context.Saga.Speed = 0;
context.Saga.FlightNo = context.Message.FlightNo;
context.Saga.CorrelationId = context.Message.CorrelationId;
Console.WriteLine($"Flight {context.Message.FlightNo} is ready to depart.");
})
.TransitionTo(Taxiing)
.Request(TaxiToRunway,
(context) => context.Init<TaxiToRunway>(new {CorrelationId = context.Saga.CorrelationId}))
...
During(Taxiing,
Ignore(ReadyToDepart),
When(TaxiToRunway.Completed)
.Then(x =>
{
x.ToString();
})
.TransitionTo(TakingOff),
With a debugger attached I hit the x.ToString() line.
The consumer (in a different host):
public class TaxiToRunwayConsumer: IConsumer<TaxiToRunway>
{
public async Task Consume(ConsumeContext<TaxiToRunway> context)
{
await context.RespondAsync<TaxiingComplete>(new
{
context.Message.CorrelationId
});
}
}
Saga startup config:
cfg.AddSagaStateMachine<PlaneStateMachine, PlaneState>()
.MessageSessionRepository();
cfg.AddServiceBusMessageScheduler();
cfg.UsingAzureServiceBus((context, sbCfg) =>
{
var connectionString = appConfig.ServiceBus.ConnectionString;
sbCfg.Host(connectionString);
EndpointConvention.Map<TaxiToRunway>(new Uri("sb://xxx.servicebus.windows.net/taxi-to-runway"));
sbCfg.UseServiceBusMessageScheduler();
sbCfg.ReceiveEndpoint("plane-state", e =>
{
e.UseInMemoryOutbox();
e.RequiresSession = true;
e.PrefetchCount = 50;
e.MaxConcurrentCalls = 50;
e.ConfigureSaga<PlaneState>(context);
});
sbCfg.ConfigureEndpoints(context);
});
I can see this in the log output:
dbug: MassTransit.Messages[0]
SEND sb://dbpdf-us-dev-sam.servicebus.windows.net/plane-state 80d90000-5d7b-2cf0-7a6b-08da0fd3e7b7 MassTransit.Scheduling.CancelScheduledMessage
Am I supposed to be handling this as an event??
Learning curve on this sure is steep! My question is what do I need to do to not have these messages go to skipped?
So, the reason this doesn't work:
The message session saga repository can only correlate by the SessionId, since it's session-stored data.
The requestId, therefore, MUST equal the saga instance correlationId (aka, the SessionId)
The timeout message, sent by the request, gets a tokenId based upon the sequence number of the scheduled message
Which isn't saved anywhere
So the request timeout isn't canceled
The proper approach, in this scenario, is to use a Request/Response that doesn't have a timeout and use a separate Schedule to schedule the timeout yourself.

Event listener never asserts while testing on Laravel / PHPUnit

I'm trying to send a test email and assert (using events) that it was sent.
The test runs fine and the event in fact happen (I'm logging the event handler method on LogSendingMessage class), but the expectsEvents never assert, neither before, neither after the Mail::send method.
public function testSendSimpleMailOverSMTP()
{
// Send methods
$fromMail = 'my#mail.com';
$fromName = 'Tiago Gouvêa';
$toMail = 'myanother#mail.com';
$toName = 'Tiago Gouvêa';
$subject = 'Mail testing123';
$view = 'SimpleMail';
$data = array('body' => "Olá fulano");
$this->expectsEvents(Illuminate\Mail\Events\MessageSending::class);
$this->expectsEvents(Illuminate\Mail\Events\MessageSent::class);
Mail::send($view, $data, function (Illuminate\Mail\Message $message) use ($toMail, $toName, $fromMail, $fromName, $subject) {
$message->to($toMail, $toName)->subject($subject);
$message->from($fromMail, $fromName);
});
}
Here on stack has another questions some like, but, none of then worked for me. I appreciate any help. :)

How to change Laravel 5.4 notifications Via Manual

How to set via in controller
If one user want notification on mail and second user not want to notification in mail
So how to setup that one user notification send on mail and second user notification not send on mail
$newinvoice = New NewInvoice('create a new invoice',$invoice->id);
$newinvoice->via(['database','broadcast','mail']);
Notification::send($sendToUser, $newinvoice);
when I run this code give me error
InvalidArgumentException in Manager.php line 90: Driver [1] not supported.
Thank's in advance
You could provide the channel as another argument to your notifications constructor
$newinvoice = New NewInvoice('create a new invoice',$invoice->id, 'mail');
You can also provide an array of channels
$newinvoice = New NewInvoice('create a new invoice',$invoice->id, ['mail', 'broadcast']);
Create a property in your notifiable class
protected $_channel = null;
Your constructor can save the channels to this property
public function __construct($description, $invoiceId, $notificationChannel)
{
$this->_channel = $notificationChannel;
}
Your via() method could do things like this
public function via($notifiable)
{
if (!$this->_channel)
{
throw new \Exception('Sending a message failed. No channel provided.');
}
return is_array($this->_channel) ? $this->_channel : [$this->_channel];
}

Resources