Skip to content
Snippets Groups Projects
Commit 00eec8f8 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

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).
parent ed3f3d9d
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,12 @@ This file logs the changes that are actually interesting to users (new features, ...@@ -4,6 +4,12 @@ This file logs the changes that are actually interesting to users (new features,
changed functionality, fixed bugs). 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) ## Version 2.0.2 (released 2017-04-07)
- Added support for task types. Workers only get tasks of the types they support. - Added support for task types. Workers only get tasks of the types they support.
......
...@@ -36,6 +36,11 @@ config files. ...@@ -36,6 +36,11 @@ config files.
- `job_types`: Space-separated list of job types this worker may execute. - `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 - `task_update_queue_db`: filename of the SQLite3 database holding the queue of task
updates to be sent to the Master. 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 These configuration keys are also required, but are created automatically upon startup
when they don't exist yet: when they don't exist yet:
......
[flamenco-worker] [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/ manager_url = http://localhost:8083/
task_types = sleep blender-render file-management task_types = sleep blender-render file-management
task_update_queue_db = flamenco-worker.db task_update_queue_db = flamenco-worker.db
......
...@@ -4,10 +4,36 @@ import argparse ...@@ -4,10 +4,36 @@ import argparse
import asyncio import asyncio
import logging import logging
import logging.config import logging.config
import pathlib
import requests 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(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', parser.add_argument('-c', '--config',
...@@ -25,6 +51,13 @@ def main(): ...@@ -25,6 +51,13 @@ def main():
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.debug('Starting') 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 # Patch AsyncIO
from . import patch_asyncio from . import patch_asyncio
patch_asyncio.patch_asyncio() patch_asyncio.patch_asyncio()
......
...@@ -14,6 +14,7 @@ CONFIG_SECTION = 'flamenco-worker' ...@@ -14,6 +14,7 @@ CONFIG_SECTION = 'flamenco-worker'
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
'flamenco-worker': collections.OrderedDict([ 'flamenco-worker': collections.OrderedDict([
('pid', ''), # Optional location of the PID-file
('manager_url', 'http://flamenco-manager/'), ('manager_url', 'http://flamenco-manager/'),
('task_types', 'unknown sleep blender-render'), ('task_types', 'unknown sleep blender-render'),
('task_update_queue_db', 'flamenco-worker.db'), ('task_update_queue_db', 'flamenco-worker.db'),
......
...@@ -21,7 +21,7 @@ DESC="Flamenco Worker" ...@@ -21,7 +21,7 @@ DESC="Flamenco Worker"
NAME=flamenco-worker NAME=flamenco-worker
DAEMON=$FLAMENCO_WORKER DAEMON=$FLAMENCO_WORKER
SCRIPTNAME=/etc/init.d/$NAME SCRIPTNAME=/etc/init.d/$NAME
PIDFILE=/var/run/$NAME.pid PIDFILE=/home/guest/local-flamenco-worker/$NAME.pid
USER=guest USER=guest
GROUP=guest GROUP=guest
...@@ -38,7 +38,7 @@ do_start() ...@@ -38,7 +38,7 @@ do_start()
{ {
sudo -u $USER mkdir -p /home/guest/local-flamenco-worker sudo -u $USER mkdir -p /home/guest/local-flamenco-worker
start-stop-daemon --start --quiet --pidfile $PIDFILE --name $NAME \ 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 "$?" return "$?"
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment