You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.7 KiB
76 lines
1.7 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Agent;
|
|
use App\Models\MiniProgramTemplate;
|
|
use App\Models\MiniProgramUploadLog;
|
|
use App\Service\UploadMiniProgram;
|
|
use Exception;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class UploadMiniProgramQueue implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
private int $template_id; //小程序模板ID
|
|
private int $agent_id;
|
|
|
|
/**
|
|
* 任务可尝试的次数
|
|
* @var int
|
|
*/
|
|
public $tries = 3;
|
|
|
|
/**
|
|
* 任务失败前允许的最大异常数
|
|
* @var int
|
|
*/
|
|
public $maxExceptions = 3;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param int $template_id 小程序模板ID
|
|
* @param int $agent_id 代理商ID
|
|
*/
|
|
public function __construct(int $template_id, int $agent_id)
|
|
{
|
|
$this->template_id = $template_id;
|
|
$this->agent_id = $agent_id;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
public function handle()
|
|
{
|
|
$agent = Agent::find($this->agent_id);
|
|
$template = MiniProgramTemplate::find($this->template_id);
|
|
|
|
if (!$agent || !$template) return;
|
|
|
|
try {
|
|
# 如果已经有上传成功的版本则不再上传
|
|
$not_uploaded = MiniProgramUploadLog::where([
|
|
'is_success' => 0,
|
|
'agent_id' => $this->agent_id,
|
|
'template_id' => $this->template_id,
|
|
])->doesntExist();
|
|
|
|
if ($not_uploaded) {
|
|
new UploadMiniProgram($agent, $template);
|
|
}
|
|
} catch (GuzzleException | \Exception $e) {
|
|
throw new Exception($agent->id . '-' . $agent->appid . $e->getMessage());
|
|
}
|
|
}
|
|
}
|