php - How can I schedule a function to run, after one of my function is completed, after a specific time period in laravel?

172

I have a ftp function which moves file from one folder to other(say folder a to folder b) when a button is clicked. It works perfectly. I want a function that will run 30 minutes after the above function is called(i.e. after the file is moved from a to b). How can I schedule such a task in laravel?

I want to do this, because after that file is moved few functions are executed and then if the person forgets to remove the file then that can be dangerous. So I need to check, 30 mins after the file is moved from a to b, whether the file has been moved back or no.

228

Answer

Solution:

This sounds like a perfect opportunity to make use of Laravels queues, which also allow for delayed dispatching as you can find in the documentation when looking for queuing and delayed dispatching.

Basically, you will need a job that you can push onto the queue like the following

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class RemoveFileFromFtp implements ShouldQueue
{
    use Dispatchable, Queueable;

    protected $file;

    /**
     * Create a new job instance.
     *
     * @param  string $file
     * @return void
     */
    public function __construct(string $file)
    {
        $this->file = $file;
    }

    /**
     * Execute the job.
     *
     * @param  FtpService $ftpService
     * @return void
     */
    public function handle(FtpService $ftpService)
    {
        $ftpService->removeFileIfExists($this->file);
    }
}

You will then be able to dispatch the job like so:

function doSomethingWithFileOnFtp()
{
    // ... here you can do what you described

    // and then you queue a clean up job for the file
    RemoveFileFromFtp::dispatch($file)->delay(now()->addMinutes(30));
}

Of course this solution expects you to have set up Laravel queues properly. There is different ways you can do this, but I guess the documentation is your best friend regarding this (see here).

462

Answer

Solution:

You need CRON JOB - Laravel Scheduler

Class example:

namespace App\Console;

use 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->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

Then you need to switch on cron jobs on server:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

Here you have details: https://laravel.com/docs/5.6/scheduling

And nice video which explains somethings: https://www.youtube.com/watch?v=mp-XZm7INl8

People are also looking for solutions to the problem: magento - Rediness check of "PHP Extension intl" not working despite trying everything

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.