Search code examples
laraveltaskjobs

Laravel - Setting up right Jobs per function


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 function 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!


Solution

  • You don't need to use cronjobs, you can dispach a job from the controller:

    ProcessImage::dispatch($image);
    

    I think about approaching this using a class that contains all of those tasks while each task represented by a public function (additional private helper function can be added according to your needs).

    Afterwards, create a Laravel Job which gets the class you've created earlier by dependency injection using the Service Container. In the handle method of you Job, call the functions from your class in any order you desire.