Queue

pdf-optimizer introduces a powerful feature that allows you to queue the optimization of your files seamlessly within the Laravel framework. Queueing files provides a convenient and efficient way to handle optimization tasks asynchronously, improving the responsiveness and performance of your application.

Key Benefits

  • Asynchronous Processing: Queueing enables the optimization of files in the background, preventing long processing times from affecting the user experience.

  • Scalability: Easily handle large batches of files by distributing optimization tasks across multiple workers, ensuring optimal resource utilization.

  • Improved Performance: By offloading optimization to the queue, your application remains responsive, providing a smoother user experience.

There are two ways to enable the queue for optimizing processes. You can enable it globally through the configuration file or selectively using onQueue wherever you want to optimize a file.


Usage

use Mostafaznv\PdfOptimizer\Laravel\Facade\PdfOptimizer;

$result = PdfOptimizer::fromDisk('minio')
    ->open('input.pdf')
    ->toDisk('files')
    ->onQueue()
    ->optimize('output.pdf');

The onQueue method accepts four optional arguments:

  • enabled (boolean): Responsible for enabling or disabling the queue. It is set to true by default.

  • name (string): Customizes the queue name, which is set to the default value by default.

  • connection (string): Customizes the connection of the queue process. It defaults to null.

  • timeout (int): Sets the timeout for processes during the queue job.

The PdfOptimizer class returns an OptimizeResult object that contains two properties:

  • isQueued (boolean): Indicates whether the optimization process has been queued. Defaults to false.

  • queueId (string): Provides an orderedUuid for the queued optimization job.

You can store the queueId in your database and monitor the optimization's result by listening to the PdfOptimizerJobFinished event.


Job Completion Event

pdf-optimizer provides an event that fires when an optimization job finishes processing in the queue. This event can be used to perform additional actions once the job is complete, such as sending a notification to a user or updating a database record. The event can be listened to using Laravel's built-in event system, and it provides access to the ID of optimization job, status of the optimization process, and the message of executed command. To utilize this feature, you need to create an event listener and register it. Once the job is completed, pdf-optimizer will notify your listener and provide the necessary information.

  1. Craete a Listener

php artisan make:listener PdfOptimizerJobNotification
  1. Register Listener

App\Providers\EventServiceProvider
use Mostafaznv\PdfOptimizer\Events\PdfOptimizerJobFinished;
use App\Listeners\PdfOptimizerJobNotification;
 

protected $listen = [
    PdfOptimizerJobFinished::class => [
        PdfOptimizerJobNotification::class,
    ],
];
  1. Listen to Notification

<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Log;
use Mostafaznv\PdfOptimizer\Events\PdfOptimizerJobFinished;


class PdfOptimizerJobNotification
{
    public function handle(PdfOptimizerJobFinished $event)
    {
        Log::info("pdf optimization finished:");
        Log::info("id: $event->id");
        Log::info("status: $event->status");
        Log::info("message: $event->message");
    }
}

Last updated