diff --git a/CHANGELOG.md b/CHANGELOG.md index a92bc9206d1b926a0d3295d1baa3bca2bdb3f028..8290c20fa9750139135fdb12ebc96ea44eeb0d5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ changed functionality, fixed bugs). - Worker can be told to shut down by the Manager. The environment (for example systemd on Linux) is responsible for restarting Flamenco Worker after such a shutdown. - Added `--version` CLI option to show the version of Flamenco Worker and quit. +- Added `--single` or `-1` CLI option to shut down the Worker after executing a single task. ## Version 2.0.8 (released 2017-09-07) diff --git a/flamenco_worker/cli.py b/flamenco_worker/cli.py index fa43fc0d827bc7599ee30f7584ca1d8ce601140d..cd3bf17e38451455224ce0b5ec195db38731ffc2 100644 --- a/flamenco_worker/cli.py +++ b/flamenco_worker/cli.py @@ -31,6 +31,8 @@ def main(): help="Starts up in testing mode, in which only a handful of " "test-specific task types are accepted. This overrides the task_types " "in the configuration file.") + parser.add_argument('-1', '--single', action='store_true', + help="Runs a single tasks, then exits.") args = parser.parse_args() if args.version: @@ -55,6 +57,9 @@ def main(): confparser.erase('worker_id') confparser.erase('worker_secret') + if args.single: + log.info('Running in single-task mode, will stop after performing one task.') + # Find the Manager using UPnP/SSDP if we have no manager_url. if not confparser.value('manager_url'): from . import ssdp_discover @@ -108,6 +113,7 @@ def main(): push_log_max_entries=confparser.value('push_log_max_entries', int), push_act_max_interval=confparser.interval_secs('push_act_max_interval_seconds'), initial_state='testing' if args.test else 'awake', + run_single_task=args.single, ) mir = may_i_run.MayIRun( diff --git a/flamenco_worker/runner.py b/flamenco_worker/runner.py index 957a89f9bfcd48997d4d385d770d3d0cab9f2c81..7d27389ad2449182d5de37f8f0d23e408de1edeb 100644 --- a/flamenco_worker/runner.py +++ b/flamenco_worker/runner.py @@ -62,6 +62,7 @@ class TaskRunner: return False self._log.info('Task %s completed succesfully.', task_id) + self.current_command = None return True async def abort_current_task(self): diff --git a/flamenco_worker/worker.py b/flamenco_worker/worker.py index 2feb0f34327f847a773f17bb0c219079866f2062..d06c904fe220de33d10e848cb31162dbc1ba09ce 100644 --- a/flamenco_worker/worker.py +++ b/flamenco_worker/worker.py @@ -56,6 +56,7 @@ class FlamencoWorker: # Indicates the state in which the Worker should start initial_state = attr.ib(validator=attr.validators.instance_of(str), default='awake') + run_single_task = attr.ib(validator=attr.validators.instance_of(bool), default=False) # When Manager tells us we may no longer run our current task, this is set to True. # As a result, the cancelled state isn't pushed to Manager any more. It is reset @@ -433,6 +434,11 @@ class FlamencoWorker: except Exception: self._log.exception('While notifying manager of failure, another error happened.') finally: + if self.run_single_task: + self._log.info('Running in single-task mode, exiting.') + self.go_to_state_shutdown() + return + if self.state == WorkerState.AWAKE: # Schedule a new task run unless shutting down or sleeping; after a little delay to # not hammer the world when we're in some infinite failure loop. @@ -637,7 +643,7 @@ class FlamencoWorker: using systemd on Linux with Restart=always will do this. """ - self._log.info('Shutting down by request of the Flamenco Manager') + self._log.info('Shutting down by request of the Manager or due to single-task mode') self.state = WorkerState.SHUTTING_DOWN # Don't bother acknowledging this status, as we'll push an "offline" status anyway.