将失败的队列作业记录到文件回退



如果MySQL宕机,是否有任何后备选项可以将失败的队列作业记录到文件中?

我尝试

namespace AppProvidersAppServiceProvider;
function register()
Queue::failing(function (JobFailed $event)  {
if($event->exception instanceof PDOException){ 
$data = [
'code'              =>  $event->exception->getCode(),
'connectionName'    =>  $event->connectionName,
'getQueue'          =>  $event->job->getQueue(),
'getRawBody'        =>  $event->job->getRawBody(),
'exception'         =>  (string)$event->exception,
];
AppRepositoriesFailedJobMysqlDown::set($data);
}
});

但这检查了工作减少的原因,我想捕捉插入失败作业异常

[2002] No such file or directory (SQL: insert into `failed_jobs` (`connection`, `queue`, `payload`, `exception`, `failed_at`) values (redis, superhigh, {"ty................

有什么想法吗?

感谢

找到解决方案

创建类

<?php
namespace AppOverride;
use IlluminateQueueFailedFailedJobProviderInterface;
use IlluminateQueueFailedDatabaseFailedJobProvider ;
use IlluminateSupportFacadesDate;
class FallbackDatabaseFailedJobProvider  extends DatabaseFailedJobProvider  implements FailedJobProviderInterface
{
public function log($connection, $queue, $payload, $exception)
{
try{
return parent::log(...func_get_args());
}catch (Exception $e) {
$failed_at = Date::now();
$exception = (string) $exception;
$data = [
'connectionName'    =>  $connection,
'getQueue'          =>  $queue,
'getRawBody'        =>  $payload,
'exception'         =>  $exception,
'failed_at'         =>  $failed_at,
];
AppRepositoriesFailedJobMysqlDown::set($data);
}
}
}

并在服务提供商中注册

<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use AppOverrideFallbackDatabaseFailedJobProvider;
class FailedLogServiceProvider extends ServiceProvider
{
public function boot()
{
// Get a default implementation to trigger a deferred binding
$_ = $this->app['queue.failer'];
// Swap the implementation
$this->app->singleton('queue.failer', function ($app) {
$config = $this->app['config']['queue.failed'];
return new FallbackDatabaseFailedJobProvider($this->app['db'], $config['database'], $config['table']);
});
}
}

添加到提供者中的condig/app.php

'providers' => [
..............
AppProvidersFailedLogServiceProvider::class,
]

使用当前或创建自己的实现日志函数

<?php
namespace AppRepositories;
/**
* Log failed job to file fallback
*/
class FailedJobMysqlDown
{
private static $file = '_xlogJobFailedMysqlDown'; //set full path
public static function get(){
$x = file(self::$file);
$data = [];
foreach($x as $line){
$data[] = json_decode($line);
}
return $data;
}
public static function set($message){
$message = json_encode($message);
file_put_contents(self::$file,$message.PHP_EOL , FILE_APPEND | LOCK_EX );
}
}

相关内容

  • 没有找到相关文章

最新更新