mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 01:17:37 -02:30
Use closing consistently.
This commit is contained in:
@@ -9,6 +9,7 @@ import logging
|
|||||||
import json
|
import json
|
||||||
import signal
|
import signal
|
||||||
import time
|
import time
|
||||||
|
from contextlib import closing
|
||||||
from optparse import make_option
|
from optparse import make_option
|
||||||
from multiprocessing import Process, Queue
|
from multiprocessing import Process, Queue
|
||||||
|
|
||||||
@@ -90,62 +91,62 @@ class CallbackReceiver(object):
|
|||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
def callback_handler(self, use_workers, worker_queues):
|
def callback_handler(self, use_workers, worker_queues):
|
||||||
pubsub = PubSub('callbacks')
|
|
||||||
message_number = 0
|
message_number = 0
|
||||||
total_messages = 0
|
total_messages = 0
|
||||||
last_parent_events = {}
|
last_parent_events = {}
|
||||||
|
|
||||||
for message in pubsub.subscribe():
|
with closing(PubSub('callbacks')) as callbacks:
|
||||||
total_messages += 1
|
for message in callbacks.subscribe(wait=0.1):
|
||||||
if not use_workers:
|
total_messages += 1
|
||||||
self.process_job_event(message)
|
if not use_workers:
|
||||||
else:
|
self.process_job_event(message)
|
||||||
job_parent_events = last_parent_events.get(message['job_id'], {})
|
|
||||||
if message['event'] in ('playbook_on_play_start', 'playbook_on_stats', 'playbook_on_vars_prompt'):
|
|
||||||
parent = job_parent_events.get('playbook_on_start', None)
|
|
||||||
elif message['event'] in ('playbook_on_notify', 'playbook_on_setup',
|
|
||||||
'playbook_on_task_start',
|
|
||||||
'playbook_on_no_hosts_matched',
|
|
||||||
'playbook_on_no_hosts_remaining',
|
|
||||||
'playbook_on_import_for_host',
|
|
||||||
'playbook_on_not_import_for_host'):
|
|
||||||
parent = job_parent_events.get('playbook_on_play_start', None)
|
|
||||||
elif message['event'].startswith('runner_on_'):
|
|
||||||
list_parents = []
|
|
||||||
list_parents.append(job_parent_events.get('playbook_on_setup', None))
|
|
||||||
list_parents.append(job_parent_events.get('playbook_on_task_start', None))
|
|
||||||
list_parents = sorted(filter(lambda x: x is not None, list_parents), cmp=lambda x, y: y.id-x.id)
|
|
||||||
parent = list_parents[0] if len(list_parents) > 0 else None
|
|
||||||
else:
|
else:
|
||||||
parent = None
|
job_parent_events = last_parent_events.get(message['job_id'], {})
|
||||||
if parent is not None:
|
if message['event'] in ('playbook_on_play_start', 'playbook_on_stats', 'playbook_on_vars_prompt'):
|
||||||
message['parent'] = parent.id
|
parent = job_parent_events.get('playbook_on_start', None)
|
||||||
if 'created' in message:
|
elif message['event'] in ('playbook_on_notify', 'playbook_on_setup',
|
||||||
del(message['created'])
|
'playbook_on_task_start',
|
||||||
if message['event'] in ('playbook_on_start', 'playbook_on_play_start',
|
'playbook_on_no_hosts_matched',
|
||||||
'playbook_on_setup', 'playbook_on_task_start'):
|
'playbook_on_no_hosts_remaining',
|
||||||
job_parent_events[message['event']] = self.process_job_event(message)
|
'playbook_on_import_for_host',
|
||||||
else:
|
'playbook_on_not_import_for_host'):
|
||||||
if message['event'] == 'playbook_on_stats':
|
parent = job_parent_events.get('playbook_on_play_start', None)
|
||||||
job_parent_events = {}
|
elif message['event'].startswith('runner_on_'):
|
||||||
queue_actual_worker = worker_queues[total_messages % WORKERS]
|
list_parents = []
|
||||||
queue_actual_worker[0] += 1
|
list_parents.append(job_parent_events.get('playbook_on_setup', None))
|
||||||
queue_actual_worker[1].put(message)
|
list_parents.append(job_parent_events.get('playbook_on_task_start', None))
|
||||||
if queue_actual_worker[0] >= MAX_REQUESTS:
|
list_parents = sorted(filter(lambda x: x is not None, list_parents), cmp=lambda x, y: y.id-x.id)
|
||||||
queue_actual_worker[0] = 0
|
parent = list_parents[0] if len(list_parents) > 0 else None
|
||||||
# print("Recycling worker process")
|
else:
|
||||||
# queue_actual_worker[2].join()
|
parent = None
|
||||||
# connection.close()
|
if parent is not None:
|
||||||
# w = Process(target=self.callback_worker, args=(queue_actual_worker[1],))
|
message['parent'] = parent.id
|
||||||
# w.daemon = True
|
if 'created' in message:
|
||||||
# w.start()
|
del(message['created'])
|
||||||
|
if message['event'] in ('playbook_on_start', 'playbook_on_play_start',
|
||||||
|
'playbook_on_setup', 'playbook_on_task_start'):
|
||||||
|
job_parent_events[message['event']] = self.process_job_event(message)
|
||||||
|
else:
|
||||||
|
if message['event'] == 'playbook_on_stats':
|
||||||
|
job_parent_events = {}
|
||||||
|
queue_actual_worker = worker_queues[total_messages % WORKERS]
|
||||||
|
queue_actual_worker[0] += 1
|
||||||
|
queue_actual_worker[1].put(message)
|
||||||
|
if queue_actual_worker[0] >= MAX_REQUESTS:
|
||||||
|
queue_actual_worker[0] = 0
|
||||||
|
# print("Recycling worker process")
|
||||||
|
# queue_actual_worker[2].join()
|
||||||
|
# connection.close()
|
||||||
|
# w = Process(target=self.callback_worker, args=(queue_actual_worker[1],))
|
||||||
|
# w.daemon = True
|
||||||
|
# w.start()
|
||||||
|
|
||||||
# signal.signal(signal.SIGINT, shutdown_handler([w]))
|
# signal.signal(signal.SIGINT, shutdown_handler([w]))
|
||||||
# signal.signal(signal.SIGTERM, shutdown_handler([w]))
|
# signal.signal(signal.SIGTERM, shutdown_handler([w]))
|
||||||
|
|
||||||
# queue_actual_worker[2] = w
|
# queue_actual_worker[2] = w
|
||||||
last_parent_events[message['job_id']] = job_parent_events
|
last_parent_events[message['job_id']] = job_parent_events
|
||||||
self.consumer_subscriber.send("1")
|
self.consumer_subscriber.send("1")
|
||||||
|
|
||||||
def process_job_event(self, data):
|
def process_job_event(self, data):
|
||||||
# Sanity check: Do we need to do anything at all?
|
# Sanity check: Do we need to do anything at all?
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ class PubSub(object):
|
|||||||
Ideally this should be used with `contextmanager.closing` to ensure
|
Ideally this should be used with `contextmanager.closing` to ensure
|
||||||
well-behavedness:
|
well-behavedness:
|
||||||
|
|
||||||
from contextmanager import closing
|
from contextlib import closing
|
||||||
|
|
||||||
with closing(PubSub('foobar')) as foobar:
|
with closing(PubSub('foobar')) as foobar:
|
||||||
for message in foobar.subscribe(wait=0.1):
|
for message in foobar.subscribe(wait=0.1):
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import sys
|
|||||||
import urllib
|
import urllib
|
||||||
import urlparse
|
import urlparse
|
||||||
import time
|
import time
|
||||||
|
from contextlib import closing
|
||||||
|
|
||||||
# Requests
|
# Requests
|
||||||
import requests
|
import requests
|
||||||
@@ -125,8 +126,8 @@ class CallbackModule(object):
|
|||||||
self.connection_pid = active_pid
|
self.connection_pid = active_pid
|
||||||
|
|
||||||
# Publish the callback through Redis.
|
# Publish the callback through Redis.
|
||||||
pubsub = PubSub('callbacks')
|
with closing(PubSub('callbacks')) as callbacks:
|
||||||
pubsub.publish(msg)
|
callbacks.publish(msg)
|
||||||
return
|
return
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
self.logger.info('Publish Exception: %r, retry=%d', e,
|
self.logger.info('Publish Exception: %r, retry=%d', e,
|
||||||
|
|||||||
Reference in New Issue
Block a user