翻译修改为队列
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\jobs;
|
||||
|
||||
use app\model\VehicleAr;
|
||||
use app\model\VehicleEn;
|
||||
use app\model\VehicleRu;
|
||||
use app\service\TranslatorService;
|
||||
use plugin\admin\app\model\Vehicle;
|
||||
use support\Log;
|
||||
|
||||
class TranslationJob
|
||||
{
|
||||
|
||||
protected array $payload;
|
||||
|
||||
public function __construct(array $payload)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$pt = $this->payload;
|
||||
$entityType = $pt['entityType'];
|
||||
$sourceFields = $pt['sourceFields'];
|
||||
$sourceLang = $pt['sourceLang'];
|
||||
// $locale = $pt['locale'];
|
||||
$id = $pt['id'];
|
||||
|
||||
$translator = new TranslatorService();
|
||||
if($entityType == 'location'){
|
||||
$translationModels = [
|
||||
'cn' => \app\model\Location::class,
|
||||
'en' => \app\model\LocationEn::class,
|
||||
'ar' => \app\model\LocationAr::class,
|
||||
'ru' => \app\model\LocationRu::class
|
||||
];
|
||||
|
||||
}elseif($entityType == 'vehicle'){
|
||||
$translationModels = [
|
||||
'cn' => Vehicle::class,
|
||||
'en' => VehicleEn::class,
|
||||
'ar' => VehicleAr::class,
|
||||
'ru' => VehicleRu::class
|
||||
];
|
||||
}
|
||||
|
||||
$languages = ['cn' => 'cn', 'en' => 'en', 'ar' => 'ar', 'ru' => 'ru'];
|
||||
unset($languages[$sourceLang]);
|
||||
foreach ($languages as $lang => $xflang) {
|
||||
|
||||
if($entityType =='location'){
|
||||
$fields = $translator->location($sourceFields,$lang,$sourceLang);
|
||||
}else{
|
||||
$fields = $translator->vehicle($sourceFields,$lang,$sourceLang);
|
||||
}
|
||||
|
||||
$translationModel = $translationModels[$xflang];
|
||||
|
||||
if(!empty($id)){
|
||||
$translation = $translationModel::find($id);
|
||||
}else{
|
||||
$translation = new $translationModel();
|
||||
}
|
||||
$translation->fill($fields);
|
||||
$res = $translation->save();
|
||||
log::info('$languages save', [$res,$fields]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use app\queue\jobs\TranslationJob;
|
||||
use support\Log;
|
||||
use Webman\RedisQueue\Consumer;
|
||||
|
||||
class Translator implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'translator';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
try {
|
||||
// 无需反序列化
|
||||
// var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
|
||||
$job = new TranslationJob($data);
|
||||
$job->handle();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("TranslationService 异步任务异常: ".$e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 消费失败回调
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure translator\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
return [
|
||||
'enable' => true,
|
||||
];
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Webman\RedisQueue\Command\MakeConsumerCommand;
|
||||
|
||||
return [
|
||||
MakeConsumerCommand::class
|
||||
];
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of webman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handlers' => [
|
||||
[
|
||||
'class' => Monolog\Handler\RotatingFileHandler::class,
|
||||
'constructor' => [
|
||||
runtime_path() . '/logs/redis-queue/queue.log',
|
||||
7, //$maxFiles
|
||||
Monolog\Logger::DEBUG,
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Monolog\Formatter\LineFormatter::class,
|
||||
'constructor' => [null, 'Y-m-d H:i:s', true],
|
||||
],
|
||||
]
|
||||
],
|
||||
]
|
||||
];
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
return [
|
||||
'consumer' => [
|
||||
'handler' => Webman\RedisQueue\Process\Consumer::class,
|
||||
'count' => getenv('REDIS_QUEUE_PROCESS_COUNT'), // 可以设置多进程同时消费
|
||||
'constructor' => [
|
||||
// 消费者类目录
|
||||
'consumer_dir' => app_path() . '/queue/redis'
|
||||
]
|
||||
]
|
||||
];
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
return [
|
||||
'default' => [
|
||||
'host' => getenv('REDIS_QUEUE_HOST'),
|
||||
'options' => [
|
||||
'auth' => getenv('REDIS_QUEUE_AUTH'), // 密码,字符串类型,可选参数
|
||||
'db' => getenv('REDIS_QUEUE_DB'), // 数据库
|
||||
'prefix' => getenv('REDIS_QUEUE_PREFIX'), // key 前缀
|
||||
'max_attempts' => getenv('REDIS_QUEUE_MAX_ATTEMPTS'), // 消费失败后,重试次数
|
||||
'retry_seconds' => getenv('REDIS_QUEUE_RETRY_SECONDS'), // 重试间隔,单位秒
|
||||
],
|
||||
'pool' => [ // 连接池配置
|
||||
'max_connections' => 5, // 连接池最大连接数
|
||||
'min_connections' => 1, // 连接池最小连接数
|
||||
'wait_timeout' => 3, // 从连接池获取连接最大等待时间
|
||||
'idle_timeout' => 50, // 连接池中连接空闲超时时间,超过该时间会被关闭,直到连接数为min_connections
|
||||
'heartbeat_interval' => 50, // 心跳检测间隔,不要大于60秒
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
return [
|
||||
'default' => [
|
||||
// 'host' => '127.0.0.1',
|
||||
// 'password' => '',
|
||||
// 'port' => 6379,
|
||||
// 'database' => 0,
|
||||
'host' => getenv('REDIS_HOST'),
|
||||
'password' => getenv('REDIS_PASSWORD'),
|
||||
'port' => getenv('REDIS_PORT'),
|
||||
'database' => getenv('REDIS_DATABASE'),
|
||||
'pool' => [ // 连接池配置
|
||||
'max_connections' => 5, // 连接池最大连接数
|
||||
'min_connections' => 1, // 连接池最小连接数
|
||||
'wait_timeout' => 3, // 从连接池获取连接最大等待时间
|
||||
'idle_timeout' => 50, // 连接池中连接空闲超时时间,超过该时间会被关闭,直到连接数为min_connections
|
||||
'heartbeat_interval' => 50, // 心跳检测间隔,不要大于60秒
|
||||
],
|
||||
]
|
||||
];
|
||||
Reference in New Issue
Block a user