Laravel - 调度作业时 Redis 错误

Laravel - Redis error when dispatching a job

在我负责的其中一个系统上,有时由于与 Redis 的连接问题而无法调度某些作业,这最终会向用户返回错误,在我们这边我们可以忽略它错误,只是错过了这份工作,我在 Google 上寻找如何处理它,但没有找到任何相关信息。

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff
    
    ResolveMessageBilling::dispatch($model, $request->all());

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

这是我们得到的错误:RedisException - socket error on read socket

如果出现错误如何忽略?一个简单的 try/catch 可以解决问题吗?

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff

    try {
        ResolveMessageBilling::dispatch($model, $request->all());
    } catch(\Exception $e) {}

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

如果你想绕过任何错误,你应该使用 \Throwable 而不是 \Exception

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff

    try {
        ResolveMessageBilling::dispatch($model, $request->all());
    } catch(\Throwable $e) {}

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

查看错误层次结构:https://www.php.net/manual/en/language.errors.php7.php

如果你只想绕过\RedisException,你应该可以使用:

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff

    try {
        ResolveMessageBilling::dispatch($model, $request->all());
    } catch(\RedisException $e) {}

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

如果您不想设置 Redis 只想 fixed/remove 错误,请按照本文:https://laravel.com/docs/7.x/errors

如果您想正确设置 Redis(配置 -> detabase.php),请按照以下几个步骤操作:

'redis' => [

    'client' => 'predis',

    // Keep Default as is you want to use both redis and sentinel for different service(cache, queue)'
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

    // Create a custom connection to use redis sentinel
    'cache_sentinel' => [
        // Set the Sentinel Host from Environment (optinal you can hardcode if want to use in prod only)
        env('CACHE_REDIS_SENTINEL_1'),
        env('CACHE_REDIS_SENTINEL_2'),
        env('CACHE_REDIS_SENTINEL_3'),
        'options' => [
            'replication' => 'sentinel',
            'service' => 'cachemaster'),
            'parameters' => [
                'password' => env('REDIS_PASSWORD', null),
                'database' => 0,
            ],
        ],
    ],
],

如果你需要 Redis sentinal 缓存,可以像这样创建新的缓存连接来使用上面的 sentinal 连接:

'stores' = [

//Default config
'redis' => [
    'driver'     => 'redis',
    'connection' => 'default',
],

// Custom cache connection(according to you)
'sentinel_redis' => [
    'driver'     => 'redis',
    'connection' => 'cache_sentinel',
],

在laravel应用程序中,您可以通过缓存门面轻松使用:

Cache::store('sentinel_redis')->get('key');

配置 Redis 后,使用清除服务器缓存再次正确测试