F
Filament6mo ago
shaan

Pdf generation and sending Database notifications takes too much time.

Pdf generation and sending Database notifications takes too much time. like 5 to 6s. I have created a Job too. still its taking same amount of time. Here is my code for Job
php class ProcessLetterApproval implements ShouldQueue
{

protected $letter;

protected $userId;

public function __construct(Letter $letter, $userId)
{
$this->letter = $letter;
$this->userId = $userId;
}

public function handle()
{
$this->letter->update([
'status' => 'approved',
]);

$pdf = PDF::loadView('letter-template', ['letter' => $this->letter]);

$fileName = 'letter_'.$this->letter->id.'_'.time().'.pdf';

Storage::put('public/letters/'.$fileName, $pdf->output());


$this->letter->update(['file_path' => 'letters/'.$fileName]);
}

protected function sendNotification()
{
$recipient = User::find($this->letter->user_id);

Notification::make()
->body("Your {$this->letter->template->name} Letter Request has been changed")
->actions([
Action::make('index')
->url(LetterResource::getUrl('index',
['record' => $this->letter])),
])
->sendToDatabase($recipient);
}
}
php class ProcessLetterApproval implements ShouldQueue
{

protected $letter;

protected $userId;

public function __construct(Letter $letter, $userId)
{
$this->letter = $letter;
$this->userId = $userId;
}

public function handle()
{
$this->letter->update([
'status' => 'approved',
]);

$pdf = PDF::loadView('letter-template', ['letter' => $this->letter]);

$fileName = 'letter_'.$this->letter->id.'_'.time().'.pdf';

Storage::put('public/letters/'.$fileName, $pdf->output());


$this->letter->update(['file_path' => 'letters/'.$fileName]);
}

protected function sendNotification()
{
$recipient = User::find($this->letter->user_id);

Notification::make()
->body("Your {$this->letter->template->name} Letter Request has been changed")
->actions([
Action::make('index')
->url(LetterResource::getUrl('index',
['record' => $this->letter])),
])
->sendToDatabase($recipient);
}
}
And Here is the LetterEdit
php protected function getHeaderActions(): array
{
return [
Action::make('approve')
->action(function (Letter $record) {
$this->record->update([
'status' => 'approved',
'approved_at' => now(),
'approved_by' => auth()->user()->id,
]);
ProcessLetterApproval::dispatch($record, auth()->id());
$this->redirect(static::getResource()::getUrl('index'));
})
];
}
php protected function getHeaderActions(): array
{
return [
Action::make('approve')
->action(function (Letter $record) {
$this->record->update([
'status' => 'approved',
'approved_at' => now(),
'approved_by' => auth()->user()->id,
]);
ProcessLetterApproval::dispatch($record, auth()->id());
$this->redirect(static::getResource()::getUrl('index'));
})
];
}
8 Replies
Dennis Koch
Dennis Koch6mo ago
Is the job running on the queue? Or are you using the sync driver? And what part is taking much time? If it takes some seconds to complete the job there’s nothing filament can do about it
shaan
shaanOP6mo ago
php artisan queue:work around 6 to 10 sec. and sometimes exceed error max 30
Don Vicks
Don Vicks6mo ago
It seems normal to me ...
shaan
shaanOP6mo ago
if its a job, then i can just press approve and get on with other operations right? Job will update the status and generate the letter and then notification will be send?
Dennis Koch
Dennis Koch6mo ago
And the jobs are shown as run? If it’s on the queue why do you mind?
shaan
shaanOP6mo ago
hehe. good point 🙂 . however, i cant go back to other letters when its running. thats the issue php artisan queue:work INFO Processing jobs from the [default] queue. this is whats showing
Dennis Koch
Dennis Koch6mo ago
But no jobs? Again: check the queue driver. Is it sync
shaan
shaanOP6mo ago
yes. it is. changed to database, migrated. now i think its ok INFO Processing jobs from the [default] queue.

2024-06-26 01:05:15 App\Jobs\ProcessLetterApproval ..................................................................................... RUNNING 2024-06-26 01:05:15 App\Jobs\ProcessLetterApproval ............................................................................... 111.99ms DONE 2024-06-26 01:05:15 App\Jobs\GeneratePdfForLetter ...................................................................................... RUNNING 2024-06-26 01:05:22 App\Jobs\GeneratePdfForLetter ...................................................................................... 6s DONE 2024-06-26 01:05:22 App\Jobs\SendLetterNotification .................................................................................... RUNNING 2024-06-26 01:05:22 App\Jobs\SendLetterNotification .............................................................................. 101.98ms DONE 2024-06-26 01:05:22 Filament\Notifications\DatabaseNotification ........................................................................ RUNNING 2024-06-26 01:05:22 Filament\Notifications\DatabaseNotification ................................................................... 55.69ms DONE Thank You
Want results from more Discord servers?
Add your server