IOT Account
IOT Account

Reputation: 9

Laravel to call a function asynchronously

I need to call a function in laravel controller as asynchronous like the controller doesn't have to wait for that function to execute.

Any idea

Laravel with IIS

Upvotes: 0

Views: 2559

Answers (1)

mrhn
mrhn

Reputation: 18916

You have to setup a queue and make a job. In your controller you would call it like so.

pulbic function store()
{
    dispatch(new YourJob());
}

To make the job.

class YourJob implements implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        $this->yourAsyncCode();
    }
}

Setting up the queue is not so straight forward, when you are on ISS i would guess the easiest driver is database. Set your queue driver to the database in your .env file.

QUEUE_DRIVER=database

Create the tables for the database.

php artisan queue:table
php artisan migrate

Now you have to have a process that is alive at all time that keeps working on the jobs. I don't know how to do it in IIS, normally you would do it with the supervisor in ubuntu. There must be some equivalent.

php artisan queue:listen

Upvotes: 1

Related Questions