在我负责的一个系统上,有时由于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);
}
发布于 2021-08-12 06:41:35
如果要绕过任何错误,则应使用\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);
}
发布于 2021-08-15 20:17:00
如果您不想设置Redis,只想修复/删除错误,请遵循本文: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之后,再次使用清除服务器缓存进行测试
https://stackoverflow.com/questions/68591838
复制相似问题