| 1 | #!/usr/bin/env python3 |
| 2 | """Enqueue a signed task to the other node's inbox. |
| 3 | |
| 4 | Usage: |
| 5 | dispatch-send --from <node-id> --to <node-id> --request "..." [--priority normal] |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | import argparse |
| 9 | import os |
| 10 | import pathlib |
| 11 | import sys |
| 12 | |
| 13 | sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) |
| 14 | import dispatch_lib as d |
| 15 | |
| 16 | |
| 17 | def main() -> int: |
| 18 | ap = argparse.ArgumentParser() |
| 19 | ap.add_argument("--to", required=True, help="recipient node id") |
| 20 | ap.add_argument("--from", dest="sender", default=os.environ.get("DISPATCH_FROM"), |
| 21 | help="sender node id (or set DISPATCH_FROM)") |
| 22 | ap.add_argument("--request", required=True) |
| 23 | ap.add_argument("--priority", default="normal", choices=["low", "normal", "high"]) |
| 24 | ap.add_argument("--callback", default="file", choices=["file", "discord", "none"]) |
| 25 | args = ap.parse_args() |
| 26 | |
| 27 | if not args.sender: |
| 28 | print("ERR: --from required (or set DISPATCH_FROM)", file=sys.stderr) |
| 29 | return 2 |
| 30 | if args.sender == args.to: |
| 31 | print("ERR: --from and --to must differ", file=sys.stderr) |
| 32 | return 2 |
| 33 | |
| 34 | task = d.new_task(args.sender, args.to, args.request, args.priority, args.callback) |
| 35 | path = d.enqueue(task) |
| 36 | d.log_event(args.sender, "task_send", f"-> {args.to}: {args.request[:80]}", task["id"]) |
| 37 | print(f"{task['id']}\t{path}") |
| 38 | return 0 |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |
| 42 | raise SystemExit(main()) |