Laravel bus chain runs multiple times

Viewed 32

I have a model in my custom package

this model has an observer that registered in the package service provider boot method

here is the observer source :

class DedicatedServerObserver
{
    /**
     * Handle the DedicatedServer "created" event.
     *
     * @param  DedicatedServer $dedicatedServer
     * @return void
     */
    public function created(DedicatedServer $dedicatedServer)
    {
        // Create a Virtualizor Connection
        $virtualizor = new Virtualizor($dedicatedServer->ip , $dedicatedServer->port , $dedicatedServer->api_key, $dedicatedServer->api_sec);

        // Run a Chain of Jobs for Sync Server Data
        Bus::chain(
            [
                new AllowServerTrafficOnCSF($dedicatedServer, $virtualizor),
                new SyncIPPools($dedicatedServer, $virtualizor),
                new SyncIPs($dedicatedServer,$virtualizor),
                new SyncOSTemplates($dedicatedServer, $virtualizor),
                new SyncStorages($dedicatedServer, $virtualizor),
                new SyncPlans($dedicatedServer, $virtualizor),
                new SyncVPSes($dedicatedServer, $virtualizor),
                new SetLastSync($dedicatedServer),
            ]
        )->onConnection('database')->onQueue('ip-service')->dispatch();
    }

    /**
     * Handle the DedicatedServer "updated" event.
     *
     * @param  DedicatedServer $dedicatedServer
     * @return void
     */
    public function updated(DedicatedServer $dedicatedServer)
    {
        // Create a Virtualizor Connection
        $virtualizor = new Virtualizor($dedicatedServer->ip , $dedicatedServer->port , $dedicatedServer->api_key, $dedicatedServer->api_sec);

        // Run a Chain of Jobs for Sync Server Data
        Bus::chain(
            [
                new AllowServerTrafficOnCSF($dedicatedServer, $virtualizor),
                new SyncIPPools($dedicatedServer, $virtualizor),
                new SyncIPs($dedicatedServer,$virtualizor),
                new SyncOSTemplates($dedicatedServer, $virtualizor),
                new SyncStorages($dedicatedServer, $virtualizor),
                new SyncPlans($dedicatedServer, $virtualizor),
                new SyncVPSes($dedicatedServer, $virtualizor),
                new SetLastSync($dedicatedServer),
            ]
        )->onConnection('database')->onQueue('ip-service')->dispatch();

        return;
    }
}

Now when I update the model this bus chain runs many times and non-stop and I don't know what the problem is.

the created method runs 1 time and it's okay but the updated method makes a new job every time it runs.

1 Answers

Finally, I find out what is the problem!

at the end of the bus chain, there is a job that updates the server sync time. so this job will trigger the updated method of observer and this bus chain will run again and it happened again and again.

so I ignore the updated_at and last_sync in the updated method if other columns are updated the bus chain will run otherwise nothing happened.

Related