Skip to content

Commit cd81b0f

Browse files
committed
More shutdown cleanup
1 parent 9a101b6 commit cd81b0f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

bolt-jobs/bolt/jobs/workers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ def __init__(self, max_processes=None, max_jobs_per_process=None, stats_every=No
3030

3131
self.uuid = uuid.uuid4()
3232

33+
self._is_shutting_down = False
34+
3335
def run(self):
3436
logger.info(
3537
"Starting job worker with %s max processes",
3638
self.max_processes,
3739
)
3840

39-
while True:
41+
while not self._is_shutting_down:
4042
try:
4143
self.maybe_log_stats()
4244
self.maybe_check_job_results()
@@ -72,10 +74,10 @@ def run(self):
7274
self.executor.submit(process_job, job_uuid)
7375

7476
def shutdown(self):
75-
# Prevent duplicate keyboard and sigterm calls
76-
# (the way this works also only lets us shutdown once per instance)
77-
if getattr(self, "_is_shutting_down", False):
77+
if self._is_shutting_down:
78+
# Already shutting down somewhere else
7879
return
80+
7981
self._is_shutting_down = True
8082

8183
logger.info("Job worker shutdown started")
@@ -120,7 +122,12 @@ def maybe_check_job_results(self):
120122
self.check_job_results()
121123

122124
def log_stats(self):
123-
num_proccesses = len(self.executor._processes)
125+
try:
126+
num_proccesses = len(self.executor._processes)
127+
except (AttributeError, TypeError):
128+
# Depending on shutdown timing and internal behavior, this might not work
129+
num_proccesses = 0
130+
124131
num_backlog_jobs = (
125132
JobRequest.objects.count()
126133
+ Job.objects.filter(started_at__isnull=True).count()

0 commit comments

Comments
 (0)