Skip to content

Commit

Permalink
Merge pull request #1321 from facebookresearch/lshamis/mrp_script
Browse files Browse the repository at this point in the history
Add example for scripting with cmd interface
  • Loading branch information
zephirefaith committed Sep 9, 2022
2 parents ade7f5a + a117012 commit 3308290
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mrp/examples/16_script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Example 16: Scripting msetup commands

All `mrp` commands are available in both CLI and script form.

In this example we demo a `launch.py` script that runs processes sequentially.

Running launch.py brings up a `redis` server, runs a command to set `foo=bar`, then runs another command to print the value of `foo`.
5 changes: 5 additions & 0 deletions mrp/examples/16_script/get_foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import redis

print("============")
print("foo =", redis.Redis().get("foo"))
print("============")
12 changes: 12 additions & 0 deletions mrp/examples/16_script/launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import mrp
import time

mrp.import_msetup(".")

mrp.cmd.up("redis")
time.sleep(0.5)

mrp.cmd.up("set_foo", wait=True)
mrp.cmd.up("get_foo", attach=True, wait=True)

mrp.cmd.down("redis", wait=True)
27 changes: 27 additions & 0 deletions mrp/examples/16_script/msetup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mrp

mrp.process(
name="redis",
runtime=mrp.Docker(image="redis"),
)

mrp.process(
name="set_foo",
runtime=mrp.Conda(
channels=["conda-forge"],
dependencies=["redis-py"],
run_command=["python", "set_foo.py"],
),
)

mrp.process(
name="get_foo",
runtime=mrp.Conda(
channels=["conda-forge"],
dependencies=["redis-py"],
run_command=["python", "get_foo.py"],
),
)

if __name__ == "__main__":
mrp.main()
3 changes: 3 additions & 0 deletions mrp/examples/16_script/set_foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import redis

redis.Redis().set("foo", "bar")
8 changes: 8 additions & 0 deletions mrp/src/mrp/cmd/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def callback(system_state):
@click.option("-f", "--force/--noforce", is_flag=True, default=False)
@click.option("--reset_logs", is_flag=True, default=False)
@click.option("--attach", is_flag=True, default=False)
@click.option("--wait", is_flag=True, default=False)
def cli(
*cmd_procs,
procs=None,
Expand All @@ -109,6 +110,7 @@ def cli(
force=False,
reset_logs=False,
attach=False,
wait=False,
):
procs = procs or []

Expand All @@ -128,6 +130,9 @@ def cli(
if attach and not run:
raise ValueError("Cannot attach without running")

if wait and not run:
raise ValueError("Cannot wait without running")

down_existing(names, force)

if reset_logs:
Expand Down Expand Up @@ -185,3 +190,6 @@ def cli(

if attach:
mrp.cmd.attach(names[0])

if wait:
mrp.cmd.wait(*names)

0 comments on commit 3308290

Please sign in to comment.