feat : ali

This commit is contained in:
2024-11-06 16:53:45 +08:00
parent 7b6f9e4aa7
commit 8d30a65528
9 changed files with 443 additions and 36 deletions

View File

@@ -10,14 +10,90 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "根据id删除oss的逻辑")]
class OssDelByOssIdTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
try {
$key = CommonRedisKey::getDeleteOssImgListByOssId();
$delNum = 0;
$ossIds = [];
for ($i = 1; $i < 50; $i++) {
$one = $this->redis->rPop($key);
if (empty($one)){
continue;
}
$ossIds[] = $one;
}
if (count($ossIds) == 0) {
$this->log->notice(__CLASS__.':success:无根据id删除的列表');
return;
}
$ossObjectModel = new OssObject();
//获取url列表用于删除oss
$urlList = $ossObjectModel->whereIn('id', $ossIds)->pluck('url');
if (empty($urlList)){
$this->log->error(__CLASS__.':删除内容关联失败,无法获取url');
return;
}
if(count($urlList->toArray()) != count($ossIds))
{
$this->log->error(__CLASS__.':删除内容关联失败url数量与id数量不一致');
return;
}
// 删除oss图片
$res = $ossObjectModel->whereIn('id', $ossIds)->delete();
if (!$res) {
$this->log->error(__CLASS__.':删除内容关联失败,删除sql执行失败');
return;
}
//删除oss资源
//把图片地址丢到删除redis队列
foreach ($urlList as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item);
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}catch (\Exception $e){
$this->log->error(__CLASS__.':'.$e->getMessage());
return;
}
}
}