1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import fcntl |
9
|
|
|
import os |
10
|
|
|
import select |
11
|
|
|
import signal |
12
|
|
|
|
13
|
|
|
import zmq |
14
|
|
|
|
15
|
|
|
from enarksh.event.Event import Event |
16
|
|
|
from enarksh.message.Message import Message |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class EventQueueEmptyEventHandler: |
20
|
|
|
""" |
21
|
|
|
An event handler for an empty event queue. |
22
|
|
|
""" |
23
|
|
|
_wake_up_pipe = None |
24
|
|
|
""" |
25
|
|
|
The wakeup file descriptor when a signal is received. |
26
|
|
|
|
27
|
|
|
:type: None|(int,int) |
28
|
|
|
""" |
29
|
|
|
|
30
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
31
|
|
|
@classmethod |
32
|
|
|
def init(cls): |
|
|
|
|
33
|
|
|
cls._wake_up_pipe = os.pipe() |
34
|
|
|
fcntl.fcntl(cls._wake_up_pipe[0], fcntl.F_SETFL, os.O_NONBLOCK) |
35
|
|
|
|
36
|
|
|
signal.set_wakeup_fd(EventQueueEmptyEventHandler._wake_up_pipe[1]) |
37
|
|
|
|
38
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
39
|
|
|
@staticmethod |
40
|
|
|
def handle(_event, _event_data, spawner): |
41
|
|
|
""" |
42
|
|
|
Handles an empty event queue event. |
43
|
|
|
|
|
|
|
|
44
|
|
|
:param _event: The event. |
45
|
|
|
:param * _event_data: Not used. |
46
|
|
|
:param enarksh.spawner.Spawner.Spawner spawner: The spawner. |
47
|
|
|
""" |
48
|
|
|
del _event, _event_data |
49
|
|
|
|
50
|
|
|
# List with all file descriptors for reading. |
51
|
|
|
# Add the file descriptor for waking up select when a signal has been received. |
52
|
|
|
read = [EventQueueEmptyEventHandler._wake_up_pipe[0]] |
53
|
|
|
|
54
|
|
|
# Add the sockets for incoming messages to the list of read file descriptors. |
55
|
|
|
zmq_fds = set() |
56
|
|
|
for socket in Message.message_controller.end_points.values(): |
57
|
|
|
if socket.type in [zmq.PULL, zmq.REP]: |
|
|
|
|
58
|
|
|
zmq_fd = socket.get(zmq.FD) |
|
|
|
|
59
|
|
|
read.append(zmq_fd) |
60
|
|
|
zmq_fds.add(zmq_fd) |
61
|
|
|
|
62
|
|
|
# Add the job handlers to the list of read file descriptors. |
63
|
|
|
for pid, job_handler in spawner.job_handlers.items(): |
|
|
|
|
64
|
|
|
fd_stdout = job_handler.stdout |
65
|
|
|
if fd_stdout >= 0: |
66
|
|
|
read.append(fd_stdout) |
67
|
|
|
fd_stderr = job_handler.stderr |
68
|
|
|
if fd_stderr >= 0: |
69
|
|
|
read.append(fd_stderr) |
70
|
|
|
|
71
|
|
|
# Unblock interrupts. |
72
|
|
|
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGCHLD, signal.SIGHUP}) |
73
|
|
|
|
74
|
|
|
try: |
75
|
|
|
# Wait for a fd becomes available for read or wait for an interrupt. |
76
|
|
|
read, _, _ = select.select(read, [], []) |
77
|
|
|
|
78
|
|
|
except InterruptedError: |
79
|
|
|
# Ignore Interrupted system call errors (EINTR) in the select call. |
80
|
|
|
# Note: In Python 3.5 EINTR will not occur any more. Nevertheless, we must only allow interrupts here to |
81
|
|
|
# prevent unwanted side effect of the signal handlers. |
82
|
|
|
read = [] |
83
|
|
|
|
84
|
|
|
# Block all interrupts to prevent interrupted system call errors (EINTR). |
85
|
|
|
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGCHLD, signal.SIGHUP}) |
86
|
|
|
|
87
|
|
|
zmq_event = False |
88
|
|
|
for fd in read: |
89
|
|
|
if fd in zmq_fds: |
90
|
|
|
# fd of the message queue is ready to receive data. |
91
|
|
|
zmq_event = True |
92
|
|
|
elif fd == EventQueueEmptyEventHandler._wake_up_pipe[0]: |
93
|
|
|
os.read(EventQueueEmptyEventHandler._wake_up_pipe[0], 512) |
94
|
|
|
else: |
95
|
|
|
# fd of one or more job handlers are ready to receive data. |
96
|
|
|
for job_handler in spawner.job_handlers.values(): |
97
|
|
|
if fd == job_handler.stdout: |
98
|
|
|
job_handler.read(fd) |
99
|
|
|
if fd == job_handler.stderr: |
100
|
|
|
job_handler.read(fd) |
101
|
|
|
|
102
|
|
|
if zmq_event: |
103
|
|
|
spawner.zmq_incoming_message_event.fire() |
104
|
|
|
else: |
105
|
|
|
Event.event_controller.event_queue_empty.fire() # XXX test queue is empty |
106
|
|
|
|
107
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
108
|
|
|
|