PHP code example of xmlshop / queue-monitor

1. Go to this page and download the library: Download xmlshop/queue-monitor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xmlshop / queue-monitor example snippets



final class Handler extends \Illuminate\Foundation\Exceptions\Handler implements \Illuminate\Contracts\Debug\ExceptionHandler
{
    /**
     * Render an exception to the console.
     *
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     * @param \Throwable $e
     * @return void
     */
    final public function renderForConsole($output, Throwable $e)
    {
        $command_name = null;
        foreach (request()->server('argv') as $arg) {
            if (Str::contains($arg, ':')) {
                $command_name = $arg;
                break;
            }
        }

        if (null !== $command_name && class_exists('xmlshop\QueueMonitor\Services\CLIFailureHandler')) {
            app('xmlshop\QueueMonitor\Services\CLIFailureHandler')->handle($command_name, $e);
        }

        parent::renderForConsole($output, $e);
    }
}

$this->app->bind(\Illuminate\Contracts\Debug\ExceptionHandler::class, \App\Exceptions\Handler::class);

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use xmlshop\QueueMonitor\Traits\IsMonitored; // <---

class ExampleJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;
    use IsMonitored; // <---
}

Route::prefix('monitor')->group(function () {
    Route::queueMonitor();
});

use Illuminate\Contracts\Queue\ShouldQueue;
use xmlshop\QueueMonitor\Traits\IsMonitored;

class ExampleJob implements ShouldQueue
{
    use IsMonitored;

    public function handle()
    {
        $this->queueProgress(0);

        // Do something...

        $this->queueProgress(50);

        // Do something...

        $this->queueProgress(100);
    }
}

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use xmlshop\QueueMonitor\Traits\IsMonitored;

class ChunkJob implements ShouldQueue
{
    use IsMonitored;

    public function handle()
    {
        $usersCount = User::count();

        $perChunk = 50;

        User::query()
            ->chunk($perChunk, function (Collection $users) use ($perChunk, $usersCount) {

                $this->queueProgressChunk($usersCount‚ $perChunk);

                foreach ($users as $user) {
                    // ...
                }
            });
    }
}

use Illuminate\Contracts\Queue\ShouldQueue;
use xmlshop\QueueMonitor\Traits\IsMonitored;

class LazyJob implements ShouldQueue
{
    use IsMonitored;

    public function progressCooldown(): int
    {
        return 10; // Wait 10 seconds between each progress update
    }
}

use Illuminate\Contracts\Queue\ShouldQueue;
use xmlshop\QueueMonitor\Traits\IsMonitored;

class CustomDataJob implements ShouldQueue
{
    use IsMonitored;

    public function handle()
    {
        $this->queueData(['foo' => 'Bar']);
        
        // WARNING! This is overriding the monitoring data
        $this->queueData(['bar' => 'Foo']);

        // To preserve previous data and merge the given payload, set the $merge parameter true
        $this->queueData(['bar' => 'Foo'], true);
    }
}

'ui' => [
    ...

    'show_custom_data' => true,

    ...
]

use Illuminate\Contracts\Queue\ShouldQueue;
use xmlshop\QueueMonitor\Traits\IsMonitored;

class FrequentSucceedingJob implements ShouldQueue
{
    use IsMonitored;

    public static function keepMonitorOnSuccess(): bool
    {
        return false;
    }
}

use xmlshop\QueueMonitor\Models\MonitorQueue;

$job = MonitorQueue::query()->first();

// Check the current state of a job
$job->isFinished();
$job->hasFailed();
$job->hasSucceeded();

// If the job is still running, get the estimated seconds remaining
// Notice: This 

use xmlshop\QueueMonitor\Models\MonitorQueue;

// Filter by Status
MonitorQueue::failed();
MonitorQueue::succeeded();

// Filter by Date
MonitorQueue::lastHour();
MonitorQueue::today();

// Chain Scopes
MonitorQueue::today()->failed();

php artisan vendor:publish --provider="xmlshop\QueueMonitor\Providers\MonitorProvider"  --tag=config --tag=migrations

php artisan migrate

class Kernel extends ConsoleKernel
{
    #...
    protected function schedule(Schedule $schedule)
    {
        #...
        $schedule->command('queue-monitor:aggregate-queues-sizes')->everyMinute();
        $schedule->command('queue-monitor:clean-up')->dailyAt('01:23');
        $schedule->command('queue-monitor:listener')->everyMinute();
        $schedule->command('monitor:sync-scheduler')->daily();
        $schedule->command('monitor:pid-checker')->everyMinute(); #for each node, if >1
        #...
    }
}
bash
#php artisan queue-monitor:listener disable {hours}
php artisan queue-monitor:listener disable 24 #disables alert-launcher for a day. By default 1 hour
php artisan queue-monitor:listener enable #enables that back