From 00eec8f85c4e566a653c87c3e6a5c4b33548c766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu> Date: Thu, 13 Apr 2017 11:06:43 +0200 Subject: [PATCH] Writing PID file whenever Flamenco-Worker is running. 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. This is an attempt to prevent the sysv script from starting Flamenco-Worker when it is already running (i.e. after two "service flamenco-worker start" commands). --- CHANGELOG.md | 6 +++++ README.md | 5 ++++ flamenco-worker.cfg | 2 ++ flamenco_worker/cli.py | 33 +++++++++++++++++++++++++ flamenco_worker/config.py | 1 + system-integration/sysv/flamenco-worker | 4 +-- 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f28115ac..aeaef822 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 e3639f00..205e4cf4 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 7b774fae..dee7ca1b 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 fd39d1e0..adfa2090 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 d3481cd7..7b364b65 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 4bd6032b..148dd298 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 "$?" } -- GitLab