forking watcher pattern

This commit is contained in:
2018-09-30 05:11:45 -04:00
parent 3e82146215
commit 3e0d7efcd5
2 changed files with 25 additions and 6 deletions
+7 -2
View File
@@ -1,7 +1,7 @@
"""BRUCE Operator.
Usage:
bruce-operator watch
bruce-operator watch [--buildpacks|--apps]
bruce-operator fetch [--buildpack=<buildpack>]
bruce-operator (-h | --help)
@@ -19,7 +19,12 @@ def main():
args = docopt(__doc__)
if args["watch"]:
watch()
if args["--buildpacks"]:
watch(buildpacks=True)
if args["--apps"]:
watch(apps=True)
watch(fork=True)
if args["fetch"]:
print("fetching")
+18 -4
View File
@@ -6,7 +6,7 @@ from functools import lru_cache
import logme
import kubernetes
import background
import delegator
from kubeconfig import KubeConfig
from kubernetes.client.configuration import Configuration
from kubernetes.client.api_client import ApiClient
@@ -162,6 +162,20 @@ class Operator:
operator = Operator()
def watch():
while True:
operator.watch()
@logme.log
def watch(fork=True, buildpacks=False, apps=False, logger=None):
if buildpacks and apps:
raise RuntimeError("Can only watch one at a time: buildpacks and apps.")
if fork:
subprocesses = []
for t in ("apps", "buildpacks"):
cmd = "bruce-operator watch --{t}"
logger.info(f"Running $ {cmd} in the background.")
c = delegator.run(cmd, block=False)
subprocesses.append(c)
logger.info(f"Blocking on subprocesses completion.")
for subprocess in subprocesses:
subprocess.block()