diff --git a/app/queue/jobs/TranslationJob.php b/app/queue/jobs/TranslationJob.php new file mode 100644 index 0000000..94438ce --- /dev/null +++ b/app/queue/jobs/TranslationJob.php @@ -0,0 +1,72 @@ +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]); + } + + } +} \ No newline at end of file diff --git a/app/queue/redis/Translator.php b/app/queue/redis/Translator.php new file mode 100644 index 0000000..8290f04 --- /dev/null +++ b/app/queue/redis/Translator.php @@ -0,0 +1,40 @@ + '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); + } +} diff --git a/config/plugin/webman/redis-queue/app.php b/config/plugin/webman/redis-queue/app.php new file mode 100644 index 0000000..8f9c426 --- /dev/null +++ b/config/plugin/webman/redis-queue/app.php @@ -0,0 +1,4 @@ + true, +]; \ No newline at end of file diff --git a/config/plugin/webman/redis-queue/command.php b/config/plugin/webman/redis-queue/command.php new file mode 100644 index 0000000..8bfe2a1 --- /dev/null +++ b/config/plugin/webman/redis-queue/command.php @@ -0,0 +1,7 @@ + + * @copyright walkor + * @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], + ], + ] + ], + ] +]; diff --git a/config/plugin/webman/redis-queue/process.php b/config/plugin/webman/redis-queue/process.php new file mode 100644 index 0000000..570aa3d --- /dev/null +++ b/config/plugin/webman/redis-queue/process.php @@ -0,0 +1,11 @@ + [ + 'handler' => Webman\RedisQueue\Process\Consumer::class, + 'count' => getenv('REDIS_QUEUE_PROCESS_COUNT'), // 可以设置多进程同时消费 + 'constructor' => [ + // 消费者类目录 + 'consumer_dir' => app_path() . '/queue/redis' + ] + ] +]; \ No newline at end of file diff --git a/config/plugin/webman/redis-queue/redis.php b/config/plugin/webman/redis-queue/redis.php new file mode 100644 index 0000000..9054c1c --- /dev/null +++ b/config/plugin/webman/redis-queue/redis.php @@ -0,0 +1,20 @@ + [ + '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秒 + ], + ], +]; diff --git a/config/redis.php b/config/redis.php new file mode 100644 index 0000000..f69bbd1 --- /dev/null +++ b/config/redis.php @@ -0,0 +1,20 @@ + [ +// '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秒 + ], + ] +]; \ No newline at end of file