-
-
Save little-isaac/a6d70c82f372406f827fb3b399745c8e to your computer and use it in GitHub Desktop.
Different queue for each tenant in laravel multi-tenant application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use App\Classes\Common; | |
use DB; | |
use App\Utility; | |
use Config; | |
class WebhookQueue extends Command { | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'queue:work_stores {app_id} {user_id}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'This command will run queue for specific store'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() { | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() { | |
$app_id = $this->argument('app_id'); | |
$user_id = $this->argument('user_id'); // this is multitenant user-id | |
$validator = Common::Validate_App_User($app_id, $user_id); // validate the user id | |
Config::set('queue.connections.database.connection', 'AWSRDS_stores'); | |
if ($validator->fails()) { | |
$this->error($validator->errors()->first()); | |
exit(); | |
} else { | |
Common::SetAppData(); | |
Common::SetDatabase($user_id); // Using that user id set database and other options. | |
Utility::set_utilites('STATUS_WEBHOOK_PROCESS_RUNNING', 1, $app_id); // Set flag for queue running for that user_id | |
$count = DB::connection(Common::get_connection_name())->table('jobs')->count(); // get count of current job. | |
while ($count > 0) { // if count is > 0 run following command | |
$output = $this->call('queue:work', [ | |
"--once" => true, | |
"database" | |
]); // thi command will execute single row from jobs table (Jobs table is also diff. for each tenant) | |
$count = DB::connection(Common::get_connection_name())->table('jobs')->count(); // again count tha number of jobs. | |
$this->info($count); | |
} | |
Utility::set_utilites('STATUS_WEBHOOK_PROCESS_RUNNING', -1, $app_id); // After running all jobs simple set flag to -1 so that when new job enters we can again run this command. | |
exit(); // exit | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment