【发布时间】:2020-04-21 21:37:33
【问题描述】:
我有使用 API 的 saas 服务。它有限制,所以我需要一个用户帐户同时只执行一个请求。
为此我与OnQueue($user->name);排队
然后在handle()做工作...
我只需要一个作业可以在用户队列中运行。同时可能只运行不同的队列每 1 个队列 1 个作业。
我正在使用 redis 连接。
这是我的工作课程:
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");
}
}
这就是我派遣工作的方式:
$accounts = Accounts::select(['id', 'login', 'hashtag_filter', 'concurency_filter'])->whereNotNull('hashtag_filter')->get();
foreach ($accounts as $acc) {
doFollowing::dispatch($acc)->onQueue($acc->login);
}
【问题讨论】: