4

Im trying to run the laravel queue via redis connection and after a few seconds when the worker starts I get the following errors.

Queue command:

$> php artisan queue:work --tries=1 --queue=test_queue

Errors:

local.ERROR: read error on connection to 127.0.0.1:6379 {"exception":"[object] (RedisException(code: 0): read error on connection to 127.0.0.1:6379 at vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\Connection.php:116)
[stacktrace]
#0 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\Connection.php(116): Redis->blPop(Array, 0)
#1 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\PhpRedisConnection.php(440): Illuminate\\Redis\\Connections\\Connection->command('blpop', Array)
#2 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\PhpRedisConnection.php(179): Illuminate\\Redis\\Connections\\PhpRedisConnection->command('blpop', Array)
#3 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\RedisQueue.php(233): Illuminate\\Redis\\Connections\\PhpRedisConnection->blpop(Array, 0)
#4 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\RedisQueue.php(169): Illuminate\\Queue\\RedisQueue->retrieveNextJob('queues:test_queue')
#5 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Worker.php(270): Illuminate\\Queue\\RedisQueue->pop('test_queue')
#6 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Worker.php(123): Illuminate\\Queue\\Worker->getNextJob(Object(Illuminate\\Queue\\RedisQueue), 'test_queue')
#7 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Console\\WorkCommand.php(112): Illuminate\\Queue\\Worker->daemon('redis', 'test_queue', Object(Illuminate\\Queue\\WorkerOptions))
#8 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Console\\WorkCommand.php(96): Illuminate\\Queue\\Console\\WorkCommand->runWorker('redis', 'test_queue')
#9 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#10 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(32): call_user_func_array(Array, Array)
#11 vendor\\laravel\\framework\\src\\Illuminate\\Container\\Util.php(36): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#12 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(90): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#13 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(34): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#14 vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(590): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#15 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Command.php(201): Illuminate\\Container\\Container->call(Array)
#16 vendor\\symfony\\console\\Command\\Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#17 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Command.php(188): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#18 vendor\\symfony\\console\\Application.php(1012): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#19 vendor\\symfony\\console\\Application.php(272): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#20 vendor\\symfony\\console\\Application.php(148): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#22 vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(131): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#23 artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 {main}
"}

Here is the queue config:

'redis' => [
    'driver' => 'redis',
    'connection' => 'laravel_queues',
    'queue' => env('REDIS_QUEUE', 'default'),
    'retry_after' => 190, 
    'block_for' => 0,
],

Here is the connection config:

'laravel_queues' => [
    'host' => env('REDIS_HOST', 'localhost'),
    'password' => env('REDIS_PASSWORD', null),
    'port' => env('REDIS_PORT', '6379'),
    'database' => 2,
],

Edit 1

Redis Server is Up & Running and works fine for everything else:

enter image description here

enter image description here


Edit 2

As suggested, ive run monitor on CLI and this is what happens:

1596784000.713047 [2 127.0.0.1:62141] "EVAL" "-- Get all of the jobs with an expired \"score\"...\nlocal val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])\n\n-- If we have values in the array, we will remove them from the first queue\n-- and add them onto the destination queue in chunks of 100, which moves\n-- all of the appropriate jobs onto the destination queue very safely.\nif(next(val) ~= nil) then\n    redis.call('zremrangebyrank', KEYS[1], 0, #val - 1)\n\n    for i = 1, #val, 100 do\n        redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))\n        -- Push a notification for every job that was migrated...\n        for j = i, math.min(i+99, #val) do\n            redis.call('rpush', KEYS[3], 1)\n        end\n    end\nend\n\nreturn val" "3" "LOCALHOSTqueues:test_queue:delayed" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:notify" "1596784000"
1596784000.713180 [2 lua] "zrangebyscore" "LOCALHOSTqueues:test_queue:delayed" "-inf" "1596784000"
1596784000.713362 [2 127.0.0.1:62141] "EVAL" "-- Get all of the jobs with an expired \"score\"...\nlocal val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])\n\n-- If we have values in the array, we will remove them from the first queue\n-- and add them onto the destination queue in chunks of 100, which moves\n-- all of the appropriate jobs onto the destination queue very safely.\nif(next(val) ~= nil) then\n    redis.call('zremrangebyrank', KEYS[1], 0, #val - 1)\n\n    for i = 1, #val, 100 do\n        redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))\n        -- Push a notification for every job that was migrated...\n        for j = i, math.min(i+99, #val) do\n            redis.call('rpush', KEYS[3], 1)\n        end\n    end\nend\n\nreturn val" "3" "LOCALHOSTqueues:test_queue:reserved" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:notify" "1596784000"
1596784000.713563 [2 lua] "zrangebyscore" "LOCALHOSTqueues:test_queue:reserved" "-inf" "1596784000"
1596784000.713972 [2 127.0.0.1:62141] "EVAL" "-- Pop the first job off of the queue...\nlocal job = redis.call('lpop', KEYS[1])\nlocal reserved = false\n\nif(job ~= false) then\n    -- Increment the attempt count and place job on the reserved queue...\n    reserved = cjson.decode(job)\n    reserved['attempts'] = reserved['attempts'] + 1\n    reserved = cjson.encode(reserved)\n    redis.call('zadd', KEYS[2], ARGV[1], reserved)\n    redis.call('lpop', KEYS[3])\nend\n\nreturn {job, reserved}" "3" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:reserved" "LOCALHOSTqueues:test_queue:notify" "1596784190"
1596784000.714130 [2 lua] "lpop" "LOCALHOSTqueues:test_queue"
1596784000.714340 [2 127.0.0.1:62141] "BLPOP" "LOCALHOSTqueues:test_queue:notify" "0"
Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70
  • 1
    Is the redis server up? Can you see it via netstat, that the port is up and used by redis? What is the timeout configuration for redis? Some informations would be great.. – Tyralcori Aug 07 '20 at 06:50
  • 1
    Yes Redis server is up and running, see updates – Raheel Hasan Aug 07 '20 at 07:01
  • 1
    Good. In your REDIS-CLI type "MONITOR" but without the quotes, so you see any operation that happens serverside - leave that open and reload your script, which connects to the redis-server. You may see the connection resulting in a timeout. – Tyralcori Aug 07 '20 at 07:03
  • Done, check now. – Raheel Hasan Aug 07 '20 at 07:16
  • Ive monitored it for a while, there is no timeout in the cli, yet I get the errors with the queue worker – Raheel Hasan Aug 07 '20 at 07:27
  • Ive updated phpredis to latest stable version, still the same – Raheel Hasan Aug 07 '20 at 09:32
  • So now im moving the laravel queue to database connection instead (which works fine btw) until there is a solution – Raheel Hasan Aug 07 '20 at 09:33
  • Have you installed predis/predis ? – Tohid Dadashnezhad Aug 07 '20 at 19:35
  • Well according to the documentation https://laravel.com/docs/7.x/redis `we encourage you to install and use the PhpRedis PHP extension via PECL` So ive phpredis and not predis – Raheel Hasan Aug 08 '20 at 06:05
  • @RaheelHasan this might help https://developpaper.com/two-reasons-for-read-error-on-connection/ – Haridarshan Aug 06 '21 at 06:52
  • Can you test connecting to REDIS using CLI from your PHP/Laravel server? This to be sure it's not a network/firewall issue blocking your request from/to the laravel server? – Mtxz Aug 06 '21 at 22:10
  • @Oliver Kucharzewski can you login to the redis server with the docker-comopose exec ? did you use `phpredis` or `predis` ? have you tried the `monitor` command, what was your result? – Abilogos Aug 07 '21 at 13:59
  • 2
    @RaheelHasan, do you use docker? Is redis server running in the same container as php-fpm? If not, you cannot connect to it using 127.0.0.1, you should use a container name instead (and link both containers to the same network, before it, of course). Please show your docker-compose.yaml and environment variables. – Ilia Yatsenko Aug 09 '21 at 15:42
  • I think @IliaYatsenko you are right. He did mention that he is using Docker so they won't be able to connect to the localhost as it is looking at the containers localhost and not the one that's actually running on localhost:6379. Here is how you can have your docker container read from the localhost of the machine it is running on : https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Yash Digant Joshi Aug 11 '21 at 01:16
  • Hi everyone, just to get you updated: Im not using any Dockers (nor did I mention it ever). Also I switched to using database connection instead of redis connection for the laravel jobs and its been going flawless ever since. – Raheel Hasan Aug 12 '21 at 05:41
  • Here is the my database connection entry in queue config: `('driver' => 'database', 'connection' => 'mysql', 'table' => 'jobs', 'queue' => 'default')` – Raheel Hasan Aug 12 '21 at 05:43

1 Answers1

1

I have a WebApp application connected to a Redis docker and ran into a similar issue. Here is what I learned:

I recommend using docker-compose (highly recommend) it helped solve many of my issues. The host name of the redis docker will be the service name of the redis container -- see the '#' below

   version: '3.9'
     services:
       php-service:
         image: <your-php-image>

         ports:
           - "8000:8000"
         depends_on:
           - redis
         expose:
           - "8000"

      redis:  # this is going to be the hostname of redis for you php-service
        image: <your-redis-image>
        ports:
          - "6379:6379"
        sysctls:
          net.core.somaxconn: '511'

To reference the connection to the redis host from within your PHP-WebApp docker change the host name to

 redis://redis:6379/0 # notice the /0 after the port - I couldn't make it work without this -- also notice that the second redis (which is the host name - the first is the protocol name) matches the name of the service in docker-compose

If the PHP webapp is running on your local machine outside of a docker container while trying to connect to redis running inside a docker container, change the redis host name to

redis://localhost:6379/0 # notice the /0 is still necessary but instead of the service name inside of docker-compose being used as the host, localhost is used

Good luck and comment any question you have - I think I can be of help.

spencer.pinegar
  • 442
  • 2
  • 10