0

I need to run some business logic in the background when the request is received.
Could anyone please share what will be the proper way to implement this?

I need to run this in shared hosting

    public function runSync()
    {
        // `Variables` Db table to store current value is sync is running or not
        $res = Variables::where('key', 'sync')->first();
        if($res!= null && $res->value=="true" )
        {
            return response('Sync already running');
        }
        if( $res == null)
        {
            $syncvar = new Variables;
            $syncvar->value="true";
            $syncvar->key="sync";
            $syncvar->save();
        }

        //*** process  my data here
         
 
       
        //** don't wait for this block
        // return response to user

        return response('Sync has started..');
    }
Hexgear
  • 320
  • 1
  • 3
  • 13
  • 3
    Jobs in Laravel can help you, when a request a received, you should dispatch a job that contains the logic, and it will be processed in the background (https://laravel.com/docs/10.x/queues). If you're doing this globally on every request, you might need to create a middleware that dispatches the job and register it globally in `app/Http/Kernel.php`, if you're doing it inside a controller then you can dispatch the job either in the specified method or in the `__construct()` if you want it globally on controller level. – Eyad Mohammed Osama Apr 04 '23 at 02:47
  • As for the shared hosting part, maybe you can see this: https://stackoverflow.com/questions/46141652/running-laravel-queuework-on-a-shared-hosting – Eyad Mohammed Osama Apr 04 '23 at 02:47
  • 1
    Thanks, it worked. However for the shared hosting I ned to check – Hexgear Apr 04 '23 at 05:03

1 Answers1

0

You need to create laravel job

more used this Link for more information

https://laravel.com/docs/10.x/queues#creating-jobs