Skip to content

Commit

Permalink
Fix incremental log for webhook (#42)
Browse files Browse the repository at this point in the history
* add more logging

* more logging

* Any output from webhook?

* use 0 bufsize

* Get the process ID for the hook script

* specify unbuffered input in #!
  • Loading branch information
feerrenrut authored Jun 30, 2021
1 parent 9cd63fd commit 904f666
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 13 additions & 6 deletions hooks/webhook
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
#!/usr/bin/env -S python3 -u
# -S allows for arguments
# -u unbuffered output

import sys
import logging
Expand All @@ -24,30 +26,35 @@ SCRIPTS_PATH = os.path.abspath('../mr/scripts')

def executeScriptsWebhook():
cmd = (os.path.join(SCRIPTS_PATH, "webhook"))
proc = subprocess.Popen(
logging.debug(f"Running: {cmd}")
proc: subprocess.Popen = subprocess.Popen(
cmd,
cwd=SCRIPTS_PATH,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
errors="UTF-8",
encoding="UTF-8",
bufsize=0,
)

logging.info(f"Proc pid: {proc.pid}")
logging.debug("Start logging output from proc")
# https://docs.python.org/3.7/library/subprocess.html#subprocess.Popen.stderr
# If the encoding or errors arguments were specified or the universal_newlines argument was True,
# the stream is a text stream, otherwise it is a byte stream. If the stderr argument was not PIPE,
# this attribute is None.
for stdout_line in proc.stdout:
yield stdout_line
logging.debug( stdout_line)
logging.debug("No more stdout")
proc.stdout.close()
logging.debug("waiting for proc")
return_code = proc.wait()
if return_code:
logging.debug("Called Process Error")
raise subprocess.CalledProcessError(return_code, cmd)

try:
logging.debug(f"starting scripts\\webHook")
for line in executeScriptsWebhook():
logging.debug(line)
executeScriptsWebhook()
except Exception as e:
logging.error(f"Error running scripts\\webhook process: {e}")
logging.debug(f"finished scripts\\webhook")
Expand Down
6 changes: 5 additions & 1 deletion scripts/webhook
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/python3
#!/usr/bin/env -S python3 -u
# -S allows for arguments
# -u unbuffered output

print("imports")
import os
import cgi
import re
Expand All @@ -15,6 +18,7 @@ SCRIPTS_PATH = "/home/nvdal10n/mr/scripts"
# Also add /usr/local/bin, which isn't already included in this context.
os.environ["PATH"] = "/home/nvdal10n/bin:/usr/local/bin:" + os.environ["PATH"]

print("load cgi field storage")
form = cgi.FieldStorage() # instantiate only once!

author = form['author'].value if 'author' in form else 'pratchett'
Expand Down

0 comments on commit 904f666

Please sign in to comment.