2
votes

Actually, I have created a shopify app and it's uninstall webhook with it. While uninstalling the app, shopify notify me about the uninstall of app so i could do some relevant process. If shopify doesn't get response in 5 seconds then it hit me with other notification which leads to two calls.

In my webhook function i just delete the shop from my database which sometimes take time. Is there anything i can do in laravel so my deletion process run asynchronously. After calling the deletion process which will be async so my next line of code could return "true". My uninstall function is in InstallController.php in given code below.

in App\Jobs. I have created BackgroundJobs.php


namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        DB::table("shops")
            ->where("id", $this->shopId)
            ->delete();
        return true;
    }
}

And here is a Controller InstallController.php in which "uninstall()" function dispatch queue.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class InstallController extends Controller
{
    public function index()
    {
        return view("install");
    }

    public function uninstall(Request $request)
    {
        $shop_id = $request->get('shopId');
        // dispatching job
        dispatch(new \App\Jobs\BackgroundJobs($shop_id));
        return "true";
    }
}

So how can i return "true" and doesn't need to wait for queue to finish ?

1

1 Answers

0
votes

You can use job events for execute code after or before complete your job

so you define on boot method in AppServiceProvider

public function boot()
{
    Queue::before(function (BackgroundJobs $event) {
        // $event->connectionName
        // $event->job
        // $event->job->payload()
    });

    Queue::after(function (BackgroundJobs $event) {
        // $event->connectionName
        // $event->job
        // $event->job->payload()
    });
}

for more details please check official document

https://laravel.com/docs/5.8/queues#job-events