1#!/usr/local/bin/python3.8 -u
2
3# A sample long-running supervisor event listener which demonstrates
4# how to accept event notifications from supervisor and how to respond
5# properly.  This demonstration does *not* use the
6# supervisor.childutils module, which wraps the specifics of
7# communications in higher-level API functions.  If your listeners are
8# implemented using Python, it is recommended that you use the
9# childutils module API instead of modeling your scripts on the
10# lower-level protocol example below.
11
12import sys
13
14def write_stdout(s):
15    sys.stdout.write(s)
16    sys.stdout.flush()
17
18def write_stderr(s):
19    sys.stderr.write(s)
20    sys.stderr.flush()
21
22def main():
23    while 1:
24        write_stdout('READY\n') # transition from ACKNOWLEDGED to READY
25        line = sys.stdin.readline()  # read header line from stdin
26        write_stderr(line) # print it out to stderr (testing only)
27        headers = dict([ x.split(':') for x in line.split() ])
28        data = sys.stdin.read(int(headers['len'])) # read the event payload
29        write_stderr(data) # print the event payload to stderr (testing only)
30        write_stdout('RESULT 2\nOK') # transition from BUSY to ACKNOWLEDGED
31        #write_stdout('RESULT 4\nFAIL') # transition from BUSY TO ACKNOWLEDGED
32
33if __name__ == '__main__':
34    main()
35