如何解决将失败的队列作业记录到文件回退
如果MySQL崩溃,是否有任何备用选项可将失败队列作业记录到文件?
我尝试
namespace App\Providers\AppServiceProvider;
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,];
\App\Repositories\FailedJobMysqlDown::set($data);
}
});
但是这可以检查工作失败的原因, 我想抓到插入failed_jobs异常
[2002] No such file or directory (SQL: insert into `failed_jobs` (`connection`,`queue`,`payload`,`exception`,`failed_at`) values (redis,superhigh,{"ty................
有什么想法吗?
谢谢
解决方法
找到解决方案
创建课程
<?php
namespace App\Override;
use Illuminate\Queue\Failed\FailedJobProviderInterface;
use Illuminate\Queue\Failed\DatabaseFailedJobProvider ;
use Illuminate\Support\Facades\Date;
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,];
\App\Repositories\FailedJobMysqlDown::set($data);
}
}
}
并在serviceprovider中注册
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Override\FallbackDatabaseFailedJobProvider;
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' => [
..............
App\Providers\FailedLogServiceProvider::class,]
使用当前版本或创建自己的实现日志功能
<?php
namespace App\Repositories;
/**
* 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 );
}
}
瞧瞧
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。