romanzipp / Laravel-Queue-Monitor

Monitoring Laravel Jobs with your Database
https://packagist.org/packages/romanzipp/laravel-queue-monitor
MIT License
648 stars 89 forks source link

does this package still work, and is it relevant to laravel 8.x #154

Open halimkun opened 1 week ago

halimkun commented 1 week ago

I needed a package to monitor my job queue and I found this cool package, I tried it and found that the monitor doesn't show any data after trying to run the job. I have added traits to my job class.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use romanzipp\QueueMonitor\Traits\IsMonitored;

class SendNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, IsMonitored;

    protected $topics;

    protected $notification;

    public $tries = 5;

    public $backoff = 3;

    public function __construct(array $topics, $notification = "")
    {
        $this->topics = $topics;
        $this->notification = $notification;
    }

    public function handle()
    {
        Log::info('Job started', ['time' => now()->toDateTimeString()]);

        foreach ($this->topics as $i => $topic) {

            if ($i > 0) {
                sleep(3);
            }

            try {
                Log::info('Notifikasi berhasil dikirim', ['topic' => $topic, 'time' => now()->toDateTimeString()]);
            } catch (\Exception $e) {
                Log::error('Gagal mengirim notifikasi', ['topic' => $topic, 'error' => $e->getMessage(), 'time' => now()->toDateTimeString()]);
            }
        }

        Log::info('Job finished', ['time' => now()->toDateTimeString()]);
    }
}
romanzipp commented 1 week ago

Hey! So your problem as far as I understood is that no data is being logged with the Monitor model? For this, you can call the queueData() method on the job class after applying the trait