【问题标题】:Laravel - Redis error when dispatching a jobLaravel - 调度作业时出现 Redis 错误
【发布时间】:2021-08-16 12:23:20
【问题描述】:

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

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);
}

【问题讨论】:

  • 您是否检查过 Redis 连接可用性?如果您正在使用其他 Redis 服务,例如 Caching with Redis,它是否工作正常? Check Redis connection availability
  • 您是否使用 AWS 的 Redis,因为有时会发生这种情况,因为 AWS ElasticeCache Redis 集群可能会使用 100% 的可用内存
  • 您是否使用队列来处理作业?还是它们同步运行?
  • @HuyPhạm 我正在使用 digitalocean 内存优化的 redis...
  • @MrEduar 是的,我正在使用队列...

标签: laravel laravel-queue


【解决方案1】:

如果你想绕过任何错误,你应该使用\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);
}

【讨论】:

    【解决方案2】:

    如果您不想设置 Redis,只想修复/删除错误,请关注这篇文章:https://laravel.com/docs/7.x/errors

    如果您想正确设置 Redis(config -> 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前哨缓存,可以创建新的缓存连接来使用上面的前哨连接,如下所示:

    '商店' = [

    //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 后,使用清除服务器缓存再次正确测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多