Skip to content

Commit

Permalink
Handle exit command from run method
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jul 11, 2024
1 parent 8087c0b commit 7c869f9
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions internal/runner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Command:
args: Dict[str, Any] = field(default_factory=dict)


class ExitError(Exception):
pass


class Runner:
def __init__(self, cmd, stdout):
self.cmd = cmd
Expand All @@ -28,8 +32,12 @@ def run(self):
resp = self.validate(args)
elif name == "make":
resp = self.make(args)
elif name == "exit":
self.exit(args)
else:
raise Exception("Unknown command")
except ExitError:
raise
except BaseException as err:
self.write_error(self.stdout, err)
return
Expand All @@ -46,6 +54,9 @@ def make(self, args):
bag = make_bag(bag_dir, **args)
return {"version": bag.version}

def exit(self, args):
raise ExitError

@staticmethod
def write(stdout, resp):
print(json.dumps(resp), file=stdout, flush=True)
Expand All @@ -69,11 +80,11 @@ def main():

cmd = Command(name=payload.get("name"), args=payload.get("args"))

if cmd.name == "exit":
break

runner = Runner(cmd, sys.stdout)
runner.run()
try:
runner.run()
except ExitError:
return


if __name__ == "__main__":
Expand Down

0 comments on commit 7c869f9

Please sign in to comment.