| # 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.""" |
| |
| @property |
| def script(self): |
| return self.resource("daemonizer.py") |
| |
| def start(self, pidfile, cmd, **kwargs): |
| return self._run("--pidfile", pidfile, *cmd, **kwargs) |
| |
| def stop(self, pidfile, **kwargs): |
| return self._run("--action", "stop", "--pidfile", pidfile, **kwargs) |
| |
| def restart(self, pidfile, cmd, **kwargs): |
| return self._run("--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) |
| |
| def _run(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) |