'python script return error after running redis as a job

I have a script writen in a python:

#!/usr/bin/env python

import time
import rediswq
from datetime import datetime
import os
#import subprocess

#host="redis"
# Uncomment next two lines if you do not have Kube-DNS working.
import os
host = os.getenv("REDIS_SERVICE_HOST")

q = rediswq.RedisWQ(name="dealer_codes", host=host)
print("Worker with sessionID: " +  q.sessionID())
print("Initial queue state: empty=" + str(q.empty()))
while not q.empty():
  item = q.lease(lease_secs=10, block=True, timeout=2)
  if item is not None:
    itemstr = item.decode("utf-8")
    time.sleep(10) # Put your actual work here instead of sleep.
    q.complete(item)
  else:
    print("Waiting for work")
print("Queue empty, exiting")

which is working when I run a redis pod with configuration:

apiVersion: v1
kind: Pod
metadata:
  name: redis-master
  namespace: sandbox
  labels:
    app: redis
spec:
  containers:
    - name: master
      image: redis
      command:
      - redis-server
      env:
        - name: MASTER
          value: "true"
      ports:
        - containerPort: 6379

when I convert this pod definition file to job like it follows:

apiVersion: batch/v1
kind: Job
metadata:
  name: redis-master
  namespace: sandbox
spec:
  parallelism: 1
  ttlSecondsAfterFinished: 10
  template:
    spec:
      containers:
        - name: master
          image: redis
          command:
          - redis-server
          env:
            - name: MASTER
              value: "true"
          ports:
            - containerPort: 6379
      restartPolicy: Never
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: sandbox
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis

I get an error from script that says ConnectionRefusedError: [Errno 111] Connection refused:

Worker with sessionID: c7537dc0-0a7c-4d8e-a845-b863441f6433
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 614, in connect
    sock = self.retry.call_with_retry(
  File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 45, in call_with_retry
    return do()
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 615, in <lambda>
    lambda: self._connect(), lambda error: self.disconnect(error)
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 680, in _connect
    raise err
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 668, in _connect
    sock.connect(socket_address)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "//worker.py", line 16, in <module>
    print("Initial queue state: empty=" + str(q.empty()))
  File "/rediswq.py", line 56, in empty
    return self._main_qsize() == 0 and self._processing_qsize() == 0
  File "/rediswq.py", line 45, in _main_qsize
    return self._db.llen(self._main_q_key)
  File "/usr/local/lib/python3.10/site-packages/redis/commands/core.py", line 2498, in llen
    return self.execute_command("LLEN", name)
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1215, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 1386, in get_connection
    connection.connect()
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 620, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to 10.104.106.102:6379. Connection refused.

I see that is something regarding connection. how can i make this script is working?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source