Skip to content

Commit

Permalink
feat(pic):队列分离
Browse files Browse the repository at this point in the history
  • Loading branch information
david7207 committed Aug 18, 2019
1 parent 30af7a2 commit 2cfa3d2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 48 deletions.
28 changes: 28 additions & 0 deletions application/Job/CompressJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace app\Job;

use app\common\Service\CompressService;
use think\facade\App;
use think\facade\Cache;

class CompressJob extends JobBase {
public function handle(): void {

$root = App::getRootPath() . '/public/';
$redis = Cache::store('redis')->handler();
$redis->select(9);
$data = $redis->lpop('imgCompress');
if ($data) {
/**
* @var string $data
*/
$picInfo = json_decode($data, true);
$source = $root . $picInfo['pathname'];
$percent = 1; #原图压缩,不缩放,但体积大大降低
//echo $root . $source;
(new CompressService($source, $percent))->compressImg($source);
echo '处理-' . $picInfo['pathname'] . '完成' . PHP_EOL;
}
}
}
13 changes: 13 additions & 0 deletions application/Job/JobBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace app\Job;

abstract class JobBase {
protected $data;

public function __construct(array $data) {
$this->data = $data;
}

abstract public function handle(): void ;
}
4 changes: 2 additions & 2 deletions application/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// | Author: yunwuxin <[email protected]>
// +----------------------------------------------------------------------

use app\command\Compress;
use app\command\Queue;

return [
'compress' => Compress::class
'queue' => Queue::class
];
43 changes: 0 additions & 43 deletions application/command/Compress.php

This file was deleted.

43 changes: 43 additions & 0 deletions application/command/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace app\command;

//use think\Cache;
use app\Job\CompressJob;
use app\Job\JobBase;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Queue extends Command {
private function getQueueJob(): array {
return [
[
'class' => CompressJob::class,
'data' => []
]
];
}

protected function configure() {
// 指令配置
$this->setName('queue');
// 设置参数

}

protected function execute(Input $input, Output $output) {
// 指令输出
//$output->writeln('compress');
while (true) {
foreach ($this->getQueueJob() as $job) {
$class = new $job['class']($job['data']);
/**
* @var JobBase $class
*/
$class->handle();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace app\common\Cache;

class CompressJob extends BaseCache {
class CompressPush extends BaseCache {
public function __construct() {
parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions application/index/controller/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace app\index\controller;

use app\common\Cache\CompressJob;
use app\common\Cache\CompressPush;
use app\common\model\Folders;
use app\common\model\Images;
use app\common\model\Users;
Expand Down Expand Up @@ -169,7 +169,7 @@ public function execute($user = null)
$data['quota'] = sprintf('%.2f', (float)$user->quota);
$data['use_quota'] = sprintf('%.2f', (float)$user->use_quota);
}
(new CompressJob())->addCompressJob($imageData);
(new CompressPush())->addCompressJob($imageData);
return $data;
}

Expand Down

0 comments on commit 2cfa3d2

Please sign in to comment.