DennisEilander's avatar

Setting up right Jobs per function

Hi,

I've a question about setting up some Jobs for my functions.

The case

  1. A customer uploads images to an FTP environment
  2. A crontask will check if there are any images in the folder the client has to upload to
  3. Check if image is older than 5 minutes (to be sure there is no image still in "upload" process
  4. Check if image is greater than 0kb (yes, it happens that the client uploads 0kb images)
  5. Reduce image with "intervention/image"
  6. Copy image to local website
  7. Move image to "uploaded" folder as backup

So this are all single tasks that has to be done. My question is, do I have to make a single job per function/task or can I put all functions in one job?

Thanks!

0 likes
5 replies
andresayej's avatar
Level 13

It really depends what do you prefer more.

I would go with the all in one job approach because it seems easier to handle and on a first look it would help if for some reason i have to use result(s) from point 4. in 7. for example.

Also worth noting:

For every of the points listed set-up a proper exception handling as to have more information if something goes wrong on a job execution and potentially decrease failed jobs in your queue flow.

DennisEilander's avatar

Thanks @andresayej, I will do that. So for all the functions I can create one Class and inject it into the Job handler. And important to know, all images will be processed in their own job right? Somewhere I already know the answer, but still to be sure.

andresayej's avatar

@dennis1502

To achieve so that every image is processed in it's own job i would structure it like this:

Using: https://laravel.com/docs/5.8/scheduling#scheduling-queued-jobs

i would schedule a queued job in App\Console\Kernel class, like so:

<?php

namespace App\Console;

use Jobs\CheckForImagesToConvert;
use Illuminate\Support\Facades\DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->job(new CheckForImagesToConvert)->everyFiveMinutes();
    }
}

Then create a Job class in which i would write my logic in the handle() method for the following:

  • Check if there are any images in the folder the client has to upload to
  • Check if image is older than 5 minutes (to be sure there is no image still in "upload" process
  • Check if image is greater than 0kb (yes, it happens that the client uploads 0kb images)

Then if all of the 3 steps are passing i would dispatch another job for every image that matches the 3 things above while referencing the image somehow.

An dispatch for each validated image (if any), would look something like this:

<?php

namespace App\Jobs;

use Exception;
use Jobs\ProcessImage;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class CheckForImagesToConvert implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 1. Check if there are any images in the folder the client has to upload to
        foreach($images as $image) {
            if (//Check if image is older than 5 minutes AND more then 0KB) {
                ProcessImage::dispatch($image);
            }
        }
    }

}

Then i would create the job ProccessImage in which i would handle the remaining steps for the image itself:

  • Reduce image with "intervention/image"
  • Copy image to local website
  • Move image to "uploaded" folder as backup

Hope this helps in clarifying the general structure of things, or at least my point of view on it.

Happy coding :).

DennisEilander's avatar

@andresayej Many thanks for your clear explanation. I never thought about scheduling a job first and schedule another job to do the real heavy stuff like processing.

Thanks!

Please or to participate in this conversation.