I have saas service which working with API. It has limits so I need that one user account doing only one request at the same time.
For this I queuing with OnQueue($user->name);
then in handle() doing job...
I need only one job can be run in users queue. At the same time may be run only diffent queues 1 job per 1 queue.
Im using redis connection.
This my job class:
public function __construct(Accounts $acc)
{
$this->acc = $acc;
$this->ownjob = $acc->prepareJobQueue();
}
public function handle()
{
$acc = $this->acc;
$job = $this->ownjob;
$api = new Api([
'login' => $acc->login,
'password' => $acc->password,
]);
if ($api->checkLogin()) {
info("{$acc->login} OK Authorized");
foreach ($job['queue'] as $term) {
switch($term['type']) {
case 'hashtag':
info("{$acc->login} Queuing: type - {$term['type']}, value - {$term['value']}");
$hashtag = Hashtags::where('cha_name',$term['value'])->first();
$answer = $api->getUsersByHashtag($hashtag,50);
break;
case 'concurency':
info("{$acc->login} Queuing: type - {$term['type']}, value - {$term['value']}");
$soc_user = Users::where('soc_unique_id',$term['value'])->first();
$answer = $api->getUserFollowers($soc_user);
break;
default:
break;
}
}
} else {
info("{$acc->login} NOT Authorized - STOP JOB");
}
}
This is how I dispatching job:
$accounts = Accounts::select(['id', 'login', 'hashtag_filter', 'concurency_filter'])->whereNotNull('hashtag_filter')->get();
foreach ($accounts as $acc) {
doFollowing::dispatch($acc)->onQueue($acc->login);
}