diff --git a/CHANGELOG.md b/CHANGELOG.md index f28115aca5cddc4d0cb8b6b64b202b2ef6b2617b..aeaef822fa5549d35b7958f760aa3860fae64704 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ This file logs the changes that are actually interesting to users (new features, changed functionality, fixed bugs). +## Version 2.0.3 (under development) + +- Added optional creation and removal of a PID file. This only happens when the `pid` + configuration option is given, pointing to the location of the PID file. + + ## Version 2.0.2 (released 2017-04-07) - Added support for task types. Workers only get tasks of the types they support. diff --git a/README.md b/README.md index e3639f00385f6ba6d44789ad9a6c3be98dd0485b..205e4cf49af77feda917a99f2f5ba5d9724f5c58 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,11 @@ config files. - `job_types`: Space-separated list of job types this worker may execute. - `task_update_queue_db`: filename of the SQLite3 database holding the queue of task updates to be sent to the Master. +- `pid`: Location of the PID file that'll be created when Flamenco-Worker is running, + and removed on startup. It'll contain the process ID. This is required when using the + SysV service script. When not present (or with empty value), the PID file will neither + be created nor removed. + These configuration keys are also required, but are created automatically upon startup when they don't exist yet: diff --git a/flamenco-worker.cfg b/flamenco-worker.cfg index 7b774fae456a8b1bfb7948bcd8bb1a7c6473301f..dee7ca1b5a899f62decbaeade81877fac2a2e36e 100644 --- a/flamenco-worker.cfg +++ b/flamenco-worker.cfg @@ -1,4 +1,6 @@ [flamenco-worker] +# Uncomment to create a PID file at the configured location. +# pid = /var/run/flamenco-worker/flamenco-worker.pid manager_url = http://localhost:8083/ task_types = sleep blender-render file-management task_update_queue_db = flamenco-worker.db diff --git a/flamenco_worker/cli.py b/flamenco_worker/cli.py index fd39d1e072e9928119a6e4c2f0e2f262c30df5be..adfa2090911e81829be5a2950161c24aa0de7482 100644 --- a/flamenco_worker/cli.py +++ b/flamenco_worker/cli.py @@ -4,10 +4,36 @@ import argparse import asyncio import logging import logging.config +import pathlib import requests +def create_pidfile(path: pathlib.Path): + """Creates a PID file in the given location. + + Als uses the atexit module to remove the PID file once the process is terminated. + """ + + import atexit + import os + + def remove_pidfile(): + if path.exists(): + path.unlink() + + atexit.register(remove_pidfile) + + log = logging.getLogger(__name__) + pid = os.getpid() + + if path.exists(): + log.info('Overwriting PID file %s with our PID %i', path, pid) + else: + log.info('Creating PID file %s with our PID %i', path, pid) + path.write_text(str(pid), encoding='ascii') + + def main(): parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', @@ -25,6 +51,13 @@ def main(): log = logging.getLogger(__name__) log.debug('Starting') + # Create the PID file, if the location is configured. + pidfile = confparser.value('pid') + if pidfile: + create_pidfile(pathlib.Path(pidfile)) + else: + log.info("Not creating PID file because the configuration has no value for the 'pid' key.") + # Patch AsyncIO from . import patch_asyncio patch_asyncio.patch_asyncio() diff --git a/flamenco_worker/config.py b/flamenco_worker/config.py index d3481cd7244b8bc4c555a75ee1db3ee2803bab29..7b364b65b676d100a997b9944cb33d9c5d0870ae 100644 --- a/flamenco_worker/config.py +++ b/flamenco_worker/config.py @@ -14,6 +14,7 @@ CONFIG_SECTION = 'flamenco-worker' DEFAULT_CONFIG = { 'flamenco-worker': collections.OrderedDict([ + ('pid', ''), # Optional location of the PID-file ('manager_url', 'http://flamenco-manager/'), ('task_types', 'unknown sleep blender-render'), ('task_update_queue_db', 'flamenco-worker.db'), diff --git a/system-integration/sysv/flamenco-worker b/system-integration/sysv/flamenco-worker index 4bd6032b90fb7051335437f126d8e05146689e6c..148dd2980487dd181eba2333f5fb678e4ef1ac00 100644 --- a/system-integration/sysv/flamenco-worker +++ b/system-integration/sysv/flamenco-worker @@ -21,7 +21,7 @@ DESC="Flamenco Worker" NAME=flamenco-worker DAEMON=$FLAMENCO_WORKER SCRIPTNAME=/etc/init.d/$NAME -PIDFILE=/var/run/$NAME.pid +PIDFILE=/home/guest/local-flamenco-worker/$NAME.pid USER=guest GROUP=guest @@ -38,7 +38,7 @@ do_start() { sudo -u $USER mkdir -p /home/guest/local-flamenco-worker start-stop-daemon --start --quiet --pidfile $PIDFILE --name $NAME \ - --exec $DAEMON --chuid $USER:$GROUP --chdir $WORK_DIR --make-pidfile --background + --exec $DAEMON --chuid $USER:$GROUP --chdir $WORK_DIR --background return "$?" }