Commit a8f32632 authored by 杨培's avatar 杨培

完成清理不使用的文件计划任务

parent 5313d7bd
...@@ -163,5 +163,17 @@ class UploadPools extends ActiveRecord ...@@ -163,5 +163,17 @@ class UploadPools extends ActiveRecord
return false; return false;
} }
/**
* 返回不在使用的文件列表
*
* @param int $limit
* @return UploadPools[]
*/
public static function fetchUnusedFiles($limit=500)
{
return self::find()->select('uuid,skey')->where('`status` IN (0,2) AND upload_time<:expire', [':expire'=>time()-86400])
->limit($limit)->all();
}
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2016/11/3
* Time: 17:57
*/
namespace console\controllers;
use common\models\UploadPools;
use Qiniu\Storage\BucketManager;
use yii\console\Controller;
class MaintainController extends Controller
{
/**
* 清理长期未被认领或已指定要删除的文件
*
* 建议每隔2-5个小时运行一次
*
* @throws \Exception
*/
public function actionClearUploadPool()
{
/* @var $qiniu \common\components\Qiniu */
$qiniu = \Yii::$app->qiniu;
$auth = $qiniu->getAuthInstance();
$bucketMgr = new BucketManager($auth);
$list = UploadPools::fetchUnusedFiles();
$success = $failed = 0;
foreach ($list as $file) {
list($storage, $key) = explode(':', $file->skey);
$bucket = $qiniu->getStorageConfig($storage, 'bucket');
/* @var $err \Qiniu\Http\Error */
$err = $bucketMgr->delete($bucket, $key);
if ($err == null || $err->getResponse()->statusCode == 612) {
$file->delete();
++$success;
} else {
++$failed;
\Yii::warning('Delete unused file failed: '.$err->message()."\n{uuid={$file->uuid}, skey={$file->skey}");
}
}
$info = "Deleted unused files finish, success=$success, failed=$failed.";
\Yii::info($info);
echo $info."\n";
}
}
\ No newline at end of file
...@@ -25,6 +25,11 @@ use yii\console\Controller; ...@@ -25,6 +25,11 @@ use yii\console\Controller;
class WithdrawController extends Controller class WithdrawController extends Controller
{ {
/**
* 结算今日之前用户的收入
*
* 建议每次凌晨或上午运行
*/
public function actionSettle() public function actionSettle()
{ {
$this->actionCreateSettleOrders(); $this->actionCreateSettleOrders();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment