Skip to content

Commit

Permalink
Create localhost echo server (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: Xiaodong Li <[email protected]>
  • Loading branch information
ChaiTowKway and Xiaodong Li authored Jul 5, 2023
1 parent 406befb commit 25161a3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
28 changes: 15 additions & 13 deletions executable-monitor/executable-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,28 @@
logging.info(f"Storing logs in: {log_dir}")
logging.info(f"Timeout (seconds): {args.timeout_seconds}")

exe = subprocess.Popen([exe_abs_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

cur_time_seconds = time.time()
timeout_time_seconds = cur_time_seconds + args.timeout_seconds
# Initialize values
timeout_occurred = False

exe_exit_status = None
exe_exitted = False

success_line_found = False
cur_line_ouput = 1
wait_for_exit = args.success_exit_status is not None

wait_for_exit = args.success_exit_status is not None
# Launch the executable
exe = subprocess.Popen([exe_abs_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

cur_time_seconds = time.time()
timeout_time_seconds = cur_time_seconds + args.timeout_seconds

logging.info("START OF DEVICE OUTPUT\n")

while not (timeout_occurred or exe_exitted or (not wait_for_exit and success_line_found)):

# Check if executable exitted
exe_exit_status = exe.poll()
if exe_exit_status is not None:
exe_exitted = True

# Read executable's stdout and write to stdout and logfile
exe_stdout_line = exe.stdout.readline()
logging.info(exe_stdout_line)
Expand All @@ -99,16 +103,14 @@
if args.success_line is not None and args.success_line in exe_stdout_line:
success_line_found = True

# Check if executable exitted
exe_exit_status = exe.poll()
if exe_exit_status is not None:
exe_exitted = True

# Check for timeout
cur_time_seconds = time.time()
if cur_time_seconds >= timeout_time_seconds:
timeout_occurred = True

# Sleep for a short duration between loops to not steal all system resources
time.sleep(.1)

if not exe_exitted:
exe.kill()

Expand Down
16 changes: 16 additions & 0 deletions localhost-echo-server/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: 'Localhost Echo Server'
description: 'Starts an echo server using Python.'

inputs:
port_number:
description: "Port for echo server."
required: True

runs:
using: "composite"
steps:
- name: Run localhost Echo server
run: |
python3 --version
python3 $GITHUB_ACTION_PATH/local_echo_server.py --port_number=${{ inputs.port_number }} &
shell: bash
40 changes: 40 additions & 0 deletions localhost-echo-server/local_echo_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

import asyncio
import logging
import os
from argparse import ArgumentParser

logger = logging.getLogger('echoserver')

async def echo_handler(reader, writer):
address = writer.get_extra_info('peername')
logger.debug('accept: %s', address)
message = await reader.readline()
writer.write(message)
await writer.drain()
writer.close()

if __name__ == '__main__':
parser = ArgumentParser(description='Localhost Echo server.')
parser.add_argument('--port_number',
type=int,
required=True,
help='Port for echo server.')
args = parser.parse_args()
logging.basicConfig()
logger.setLevel(logging.DEBUG)
loop = asyncio.get_event_loop()
factory = asyncio.start_server(
echo_handler,
os.environ.get('HOST'),
os.environ.get('PORT', args.port_number)
)
server = loop.run_until_complete(factory)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

0 comments on commit 25161a3

Please sign in to comment.