From 49b489a8803bea826de009c2d534747a77dc997c Mon Sep 17 00:00:00 2001 From: rpkak <67059904+rpkak@users.noreply.github.com> Date: Tue, 18 May 2021 03:41:59 +0200 Subject: [PATCH] Display ip address of worker in `rq info` (#1463) * `rq info` displays id address add feature of #1386 * making redis < 5.0.0 compatible * address -> ip_address https://github.com/rq/rq/pull/1463#discussion_r633264327 --- rq/cli/helpers.py | 2 +- rq/worker.py | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rq/cli/helpers.py b/rq/cli/helpers.py index f214b00..a0803f9 100644 --- a/rq/cli/helpers.py +++ b/rq/cli/helpers.py @@ -131,7 +131,7 @@ def show_workers(queues, raw, by_queue, queue_class, worker_class): for worker in workers: queue_names = ', '.join(worker.queue_names()) - name = '%s (%s %s)' % (worker.name, worker.hostname, worker.pid) + name = '%s (%s %s %s)' % (worker.name, worker.hostname, worker.ip_address, worker.pid) if not raw: click.echo('%s: %s %s' % (name, state_symbol(worker.get_state()), queue_names)) else: diff --git a/rq/worker.py b/rq/worker.py index 7a1b94e..e22a391 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -176,13 +176,6 @@ class Worker: self.redis_server_version = None - if prepare_for_work: - self.hostname = socket.gethostname() - self.pid = os.getpid() - else: - self.hostname = None - self.pid = None - self.job_class = backend_class(self, 'job_class', override=job_class) self.queue_class = backend_class(self, 'queue_class', override=queue_class) self.version = VERSION @@ -225,6 +218,16 @@ class Worker: self.disable_default_exception_handler = disable_default_exception_handler + if prepare_for_work: + self.hostname = socket.gethostname() + self.pid = os.getpid() + connection.client_setname(self.name) + self.ip_address = [client['addr'] for client in connection.client_list() if client['name'] == self.name][0] + else: + self.hostname = None + self.pid = None + self.ip_address = None + if isinstance(exception_handlers, (list, tuple)): for handler in exception_handlers: self.push_exc_handler(handler) @@ -301,6 +304,7 @@ class Worker: 'queues': queues, 'pid': self.pid, 'hostname': self.hostname, + 'ip_address': self.ip_address, 'version': self.version, 'python_version': self.python_version, } @@ -704,13 +708,15 @@ class Worker: def refresh(self): data = self.connection.hmget( self.key, 'queues', 'state', 'current_job', 'last_heartbeat', - 'birth', 'failed_job_count', 'successful_job_count', - 'total_working_time', 'current_job_working_time', 'hostname', 'pid', 'version', 'python_version', + 'birth', 'failed_job_count', 'successful_job_count', 'total_working_time', + 'current_job_working_time', 'hostname', 'ip_address', 'pid', 'version', 'python_version', ) (queues, state, job_id, last_heartbeat, birth, failed_job_count, - successful_job_count, total_working_time, current_job_working_time, hostname, pid, version, python_version) = data + successful_job_count, total_working_time, current_job_working_time, + hostname, ip_address, pid, version, python_version) = data queues = as_text(queues) self.hostname = as_text(hostname) + self.ip_address = as_text(ip_address) self.pid = int(pid) if pid else None self.version = as_text(version) self.python_version = as_text(python_version)