Skip to content

Commit

Permalink
Tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Sep 14, 2016
1 parent 14a45d4 commit 44356f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
28 changes: 10 additions & 18 deletions workflow/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
# Created on 2014-04-06
#

"""
Run background tasks
"""
"""Run background tasks."""

from __future__ import print_function, unicode_literals

Expand All @@ -34,40 +32,38 @@ def wf():


def _arg_cache(name):
"""Return path to pickle cache file for arguments
"""Return path to pickle cache file for arguments.
:param name: name of task
:type name: ``unicode``
:returns: Path to cache file
:rtype: ``unicode`` filepath
"""

return wf().cachefile('{0}.argcache'.format(name))


def _pid_file(name):
"""Return path to PID file for ``name``
"""Return path to PID file for ``name``.
:param name: name of task
:type name: ``unicode``
:returns: Path to PID file for task
:rtype: ``unicode`` filepath
"""

return wf().cachefile('{0}.pid'.format(name))


def _process_exists(pid):
"""Check if a process with PID ``pid`` exists
"""Check if a process with PID ``pid`` exists.
:param pid: PID to check
:type pid: ``int``
:returns: ``True`` if process exists, else ``False``
:rtype: ``Boolean``
"""
"""
try:
os.kill(pid, 0)
except OSError: # not running
Expand All @@ -76,8 +72,7 @@ def _process_exists(pid):


def is_running(name):
"""
Test whether task is running under ``name``
"""Test whether task is running under ``name``.
:param name: name of task
:type name: ``unicode``
Expand Down Expand Up @@ -113,7 +108,6 @@ def _background(stdin='/dev/null', stdout='/dev/null',
:type stderr: filepath
"""

# Do first fork.
try:
pid = os.fork()
Expand Down Expand Up @@ -150,8 +144,7 @@ def _background(stdin='/dev/null', stdout='/dev/null',


def run_in_background(name, args, **kwargs):
"""Pickle arguments to cache file, then call this script again via
:func:`subprocess.call`.
r"""Cache arguments then call this script again via :func:`subprocess.call`.
:param name: name of task
:type name: ``unicode``
Expand All @@ -175,7 +168,6 @@ def run_in_background(name, args, **kwargs):
return immediately and will not run the specified command.
"""

if is_running(name):
wf().logger.info('Task `{0}` is already running'.format(name))
return
Expand All @@ -199,12 +191,12 @@ def run_in_background(name, args, **kwargs):


def main(wf): # pragma: no cover
"""
"""Run command in a background process.
Load cached arguments, fork into background, then call
:meth:`subprocess.call` with cached arguments
:meth:`subprocess.call` with cached arguments.
"""

name = wf.args[0]
argcache = _arg_cache(name)
if not os.path.exists(argcache):
Expand Down
Loading

0 comments on commit 44356f9

Please sign in to comment.