Search code examples
phplaravellaravel-5queuejobs

Laravel Artisan command in queue does not remove entries from queue table in database


I am trying to queue the artisan command in the Laravel 5.8 version. But Job entry in the database table is not deleting automatically.

I tried the following things.

1. Directly Dispatch the artisan command as per documentation here

Artisan queue documentation

Artisan::queue('set:store_permissions', [
            'app_id' => 1,
            'user_id' => 1
        ])->onConnection('database')->onQueue('upgrade_for_permissions');

2. Create Job and dispatch it and in Job Call Artisan command.

[Dispatching]

$d_t_s = new \stdClass();
$d_t_s->id = 1;
UpgradeDatabaseForPermissions::dispatch($d_t_s)->onConnection('database')->onQueue('upgrade_for_permissions');

[Job calls Artisan Command]

protected $user_id;
public $tries = 5;

public function __construct($d) {
    $this->user_id = $d->id;
    //
}
public function handle() {
    $user_id = $this->user_id;
    Storage::disk('local')->append('upgrade_permission.log', "Started :: user :: " . $user_id);
    $artisan = Artisan::call('set:store_permissions', [
                'app_id' => 1,
                'user_id' => $user_id
    ]);
    Storage::disk('local')->append('upgrade_permission.log', "End :: user :: " . $user_id . ' :: ');
    return true;
}

Both of them work perfectly the issue is Job entry is not deleting from the database automatically.

If I remove the Artisan::call from the Job It will automatically removed from the database.

I also tried to return true from the command itself and Job as well but none of them worked.

When I run

php artisan queue:work database --queue="upgrade_for_permissions"

It outputs as follows

[2019-10-03 16:22:51][1] Processing: App\Jobs\UpgradeDatabaseForPermissions                                                                                                                     
[2019-10-03 16:22:52][1] Processed:  App\Jobs\UpgradeDatabaseForPermissions

But does not delete the queue entry itself from the database table. This only happens if I run artisan command in it.

[UpgradeDatabaseForPermissions::class]

<?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 Log;
use Artisan;
use Storage;

class UpgradeDatabaseForPermissions implements ShouldQueue {

    use Dispatchable,
        InteractsWithQueue,
        Queueable,
        SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $user_id;
    public $tries = 5;

    public function __construct($d) {
        $this->user_id = $d->id;
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle() {
        $user_id = $this->user_id;
        Storage::disk('local')->append('upgrade_permission.log', "Started :: user :: " . $user_id);
        $artisan = Artisan::call('set:store_permissions', [
                    'app_id' => 1,
                    'user_id' => $user_id
        ]);
        Storage::disk('local')->append('upgrade_permission.log', "End :: user :: " . $user_id . ' :: ');
        return true;
    }

}

[Data After job completed as follows]

  • id - 1
  • queue - upgrade_for_permissions
  • payload - Payload data
  • attempts - 1
  • reserved_at - 1570119771
  • available_at - 1570119767
  • created_at - 1570119767

[upgrade_permission.log] file

Started :: user :: 1
End :: user :: 1 :: 

Any suggestions will be helpful.


Solution

  • The actual problem is I am disconnecting one database connection for some other connection. That disconnecting causing the problems.

    I connected it to the Amazon SQS queue and now everything is working fine.