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

Raj Shah

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.

Namoshek

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I programmatically schedule an AWS lambda function to run X amount of time after an event?

React: How to force a function to run after another one is totally completed?

Specific function runs at the same moment on all of the views. How can I make it run one after the other?

Javascript: Call a function after specific time period

How can I run my function only after a key is pressed?

How can I get a Python decorator to run after the decorated function has completed?

How to schedule tcpdump to run for a specific period of time?

How can I change a label text after a specific time without after() function in tkinter python?

How to run a function after an async function completed in node.js?

How can i add a css rule to a div after specific period of time, set a delay and then add one more css rule, using jquery?

How to run a function after specific time then switch it off after specific time?

In JavaScript, how can I have a function run at a specific time?

How can I run a function with a specific duration of time?

Run a callback after multiple function have completed

How can i schedule my Code in Python at every 30 minutes increment after a particular time

How can i run all my code in one function

how can i run a code after the function root.destroy

How can I run a function after a user gets registered Django

Ember js how can i run a function after model change?

How can I run a function on page load and then after click?

how to fire a function after hovering for a specific time?

How can I execute a function right after an "await" request has been completed?

if statement to activate a function after a time period (solidity)

Executing a function after a certain period of time

How i can cancel an alarm after a period of time? | android

How can I configure Jmeter such that it hits two different tokens, one after the other, then after some period of time it hits only the second token?

how to disable login after specific period of time

How can I redirect to function controller after registration in laravel 8

How to dynamically schedule a function to run at specified time