mirror of
https://github.com/kuaifan/dootask.git
synced 2026-06-25 16:52:20 +00:00
- 新增 config/dootask.php 承载 11 个无 config 对应的 env key(默认值随迁)
- 29 处 env() 替换:APP_KEY→config('app.key')、APP_NAME→config('app.name')、其余→config('dootask.*')
- 语义逐处保持一致(?:/三元留在调用点);消除 config:cache 后 env() 返回 null 的隐患
- 注:User.php / Setting.php 同时含本批 ide-helper 生成的 @property 注释增量
- 验证:grep env( app/ 归零;composer stan 通过;phpunit 146 测试全过
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Tasks;
|
|
|
|
use App\Models\File;
|
|
use App\Models\TaskWorker;
|
|
use App\Models\Tmp;
|
|
use App\Models\UserDevice;
|
|
use App\Models\UmengLog;
|
|
use App\Models\WebSocketTmpMsg;
|
|
use App\Module\Base;
|
|
use Carbon\Carbon;
|
|
|
|
/**
|
|
* 删除过期临时数据任务
|
|
* Class DeleteTmpTask
|
|
* @package App\Tasks
|
|
*/
|
|
class DeleteTmpTask extends AbstractTask
|
|
{
|
|
protected $data;
|
|
protected $hours; // 多久后删除,单位小时
|
|
|
|
/**
|
|
* @param string $data
|
|
* @param int $hours
|
|
*/
|
|
public function __construct(string $data, int $hours = 24)
|
|
{
|
|
parent::__construct(...func_get_args());
|
|
$this->data = $data;
|
|
$this->hours = $hours;
|
|
}
|
|
|
|
public function start()
|
|
{
|
|
switch ($this->data) {
|
|
case 'tmp_msgs':
|
|
WebSocketTmpMsg::where('created_at', '<', Carbon::now()->subHours($this->hours))
|
|
->orderBy('id')
|
|
->chunk(500, function ($msgs) {
|
|
/** @var WebSocketTmpMsg $msg */
|
|
foreach ($msgs as $msg) {
|
|
$msg->delete();
|
|
}
|
|
});
|
|
break;
|
|
|
|
case 'tmp':
|
|
Tmp::where('created_at', '<', Carbon::now()->subHours($this->hours))
|
|
->orderBy('id')
|
|
->chunk(500, function ($tmps) {
|
|
/** @var Tmp $tmp */
|
|
foreach ($tmps as $tmp) {
|
|
$tmp->delete();
|
|
}
|
|
});
|
|
break;
|
|
|
|
case 'task_worker':
|
|
TaskWorker::onlyTrashed()
|
|
->where('deleted_at', '<', Carbon::now()->subHours($this->hours))
|
|
->orderBy('id')
|
|
->forceDelete();
|
|
break;
|
|
|
|
case 'file':
|
|
$day = intval(config('dootask.auto_empty_file_recycle'));
|
|
if ($day <= 0) {
|
|
return;
|
|
}
|
|
File::onlyTrashed()
|
|
->where('deleted_at', '<', Carbon::now()->subHours($day))
|
|
->orderBy('id')
|
|
->chunk(500, function ($files) {
|
|
/** @var File $file */
|
|
foreach ($files as $file) {
|
|
$file->forceDeleteFile();
|
|
}
|
|
});
|
|
break;
|
|
|
|
case 'tmp_file':
|
|
$day = intval(config('dootask.auto_empty_temp_file'));
|
|
if ($day <= 0) {
|
|
return;
|
|
}
|
|
$files = Base::recursiveFiles(public_path('uploads/tmp'));
|
|
foreach ($files as $file) {
|
|
$time = @filemtime($file);
|
|
if ($time && $time < time() - 3600 * 24 * $day) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'user_device':
|
|
UserDevice::where('expired_at', '<', Carbon::now()->subHours($this->hours))
|
|
->orderBy('id')
|
|
->chunk(500, function ($devices) {
|
|
/** @var UserDevice $device */
|
|
foreach ($devices as $device) {
|
|
UserDevice::forget($device);
|
|
}
|
|
});
|
|
break;
|
|
|
|
case 'umeng_log':
|
|
UmengLog::where('created_at', '<', Carbon::now()->subHours($this->hours))
|
|
->orderBy('id')
|
|
->chunk(500, function ($logs) {
|
|
/** @var UmengLog $log */
|
|
foreach ($logs as $log) {
|
|
$log->delete();
|
|
}
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function end()
|
|
{
|
|
|
|
}
|
|
}
|