@@ -50,30 +50,22 @@ def worker(queues):
50
50
51
51
52
52
class WorkerHealthcheck (base .BaseCheck ):
53
- NAME = 'RQ Worker Healthcheck'
54
- INTERVAL = datetime .timedelta (minutes = 5 )
55
- _last_check_time = {}
56
-
57
- def time_to_check (self , pid ):
58
- now = datetime .datetime .utcnow ()
59
-
60
- if pid not in self ._last_check_time :
61
- self ._last_check_time [pid ] = now
62
-
63
- if now - self ._last_check_time [pid ] >= self .INTERVAL :
64
- self ._last_check_time [pid ] = now
65
- return True
66
-
67
- return False
53
+ NAME = "RQ Worker Healthcheck"
68
54
69
55
def __call__ (self , process_spec ):
70
- pid = process_spec ['pid' ]
71
- if not self .time_to_check (pid ):
72
- return True
73
-
56
+ pid = process_spec ["pid" ]
74
57
all_workers = Worker .all (connection = rq_redis_connection )
75
- worker = [w for w in all_workers if w .hostname == socket .gethostname ().encode () and
76
- w .pid == pid ].pop ()
58
+ workers = [
59
+ w
60
+ for w in all_workers
61
+ if w .hostname == socket .gethostname () and w .pid == pid
62
+ ]
63
+
64
+ if not workers :
65
+ self ._log (f"Cannot find worker for hostname { socket .gethostname ()} and pid { pid } . ==> Is healthy? False" )
66
+ return False
67
+
68
+ worker = workers .pop ()
77
69
78
70
is_busy = worker .get_state () == WorkerStatus .BUSY
79
71
@@ -85,17 +77,25 @@ def __call__(self, process_spec):
85
77
86
78
is_healthy = is_busy or seen_lately or has_nothing_to_do
87
79
88
- self ._log ("Worker %s healthcheck: Is busy? %s. "
89
- "Seen lately? %s (%d seconds ago). "
90
- "Has nothing to do? %s (%d jobs in watched queues). "
91
- "==> Is healthy? %s" ,
92
- worker .key , is_busy , seen_lately , time_since_seen .seconds ,
93
- has_nothing_to_do , total_jobs_in_watched_queues , is_healthy )
80
+ self ._log (
81
+ "Worker %s healthcheck: Is busy? %s. "
82
+ "Seen lately? %s (%d seconds ago). "
83
+ "Has nothing to do? %s (%d jobs in watched queues). "
84
+ "==> Is healthy? %s" ,
85
+ worker .key ,
86
+ is_busy ,
87
+ seen_lately ,
88
+ time_since_seen .seconds ,
89
+ has_nothing_to_do ,
90
+ total_jobs_in_watched_queues ,
91
+ is_healthy ,
92
+ )
94
93
95
94
return is_healthy
96
95
97
96
98
97
@manager .command ()
99
98
def healthcheck ():
100
99
return check_runner .CheckRunner (
101
- 'worker_healthcheck' , 'worker' , None , [(WorkerHealthcheck , {})]).run ()
100
+ "worker_healthcheck" , "worker" , None , [(WorkerHealthcheck , {})]
101
+ ).run ()
0 commit comments