blob: eb38d9e1b42c75696f2cb19cf71b6caff5fba34f [file] [log] [blame]
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from recipe_engine import recipe_api
from contextlib import contextmanager
class DaemonizerApi(recipe_api.RecipeApi):
"""DaemonizerApi provides support for daemonizing processes."""
def __call__(self, *args, **kwargs):
"""Return a git command step."""
name = kwargs.pop("name", "daemonize " + args[0])
return self.m.step(name, [self.script] + list(args), **kwargs)
@property
def script(self):
return self.resource("daemonizer.py")
def start(self, pidfile, cmd, **kwargs):
return self("--pidfile", pidfile, *cmd, **kwargs)
def stop(self, pidfile, **kwargs):
return self("--action", "stop", "--pidfile", pidfile, **kwargs)
def restart(self, pidfile, cmd, **kwargs):
return self("--action", "restart", "--pidfile", pidfile, *cmd, **kwargs)
@contextmanager
def daemonize(self, cmd, **kwargs):
pidfile = self.m.path.mkstemp(prefix="pid")
try:
self.start(pidfile, cmd, **kwargs)
yield
finally:
self.stop(pidfile, **kwargs)