pyoco 0.1.0__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyoco/cli/main.py CHANGED
@@ -1,11 +1,13 @@
1
1
  import argparse
2
2
  import sys
3
3
  import os
4
+ import signal
4
5
  from ..schemas.config import PyocoConfig
5
6
  from ..discovery.loader import TaskLoader
6
7
  from ..core.models import Flow
7
8
  from ..core.engine import Engine
8
9
  from ..trace.console import ConsoleTraceBackend
10
+ from ..client import Client
9
11
 
10
12
  def main():
11
13
  parser = argparse.ArgumentParser(description="Pyoco Workflow Engine")
@@ -20,6 +22,7 @@ def main():
20
22
  run_parser.add_argument("--non-cute", action="store_false", dest="cute", help="Use plain trace style")
21
23
  # Allow overriding params via CLI
22
24
  run_parser.add_argument("--param", action="append", help="Override params (key=value)")
25
+ run_parser.add_argument("--server", help="Server URL for remote execution")
23
26
 
24
27
  # Check command
25
28
  check_parser = subparsers.add_parser("check", help="Verify a workflow")
@@ -30,35 +33,136 @@ def main():
30
33
  list_parser = subparsers.add_parser("list-tasks", help="List available tasks")
31
34
  list_parser.add_argument("--config", required=True, help="Path to flow.yaml")
32
35
 
36
+ # Server command
37
+ server_parser = subparsers.add_parser("server", help="Manage Kanban Server")
38
+ server_subparsers = server_parser.add_subparsers(dest="server_command")
39
+ server_start = server_subparsers.add_parser("start", help="Start the server")
40
+ server_start.add_argument("--host", default="0.0.0.0", help="Host to bind")
41
+ server_start.add_argument("--port", type=int, default=8000, help="Port to bind")
42
+
43
+ # Worker command
44
+ worker_parser = subparsers.add_parser("worker", help="Manage Worker")
45
+ worker_subparsers = worker_parser.add_subparsers(dest="worker_command")
46
+ worker_start = worker_subparsers.add_parser("start", help="Start a worker")
47
+ worker_start.add_argument("--server", required=True, help="Server URL")
48
+ worker_start.add_argument("--config", required=True, help="Path to flow.yaml")
49
+ worker_start.add_argument("--tags", help="Comma-separated tags")
50
+
51
+ # Runs command
52
+ runs_parser = subparsers.add_parser("runs", help="Manage runs")
53
+ runs_subparsers = runs_parser.add_subparsers(dest="runs_command")
54
+
55
+ runs_list = runs_subparsers.add_parser("list", help="List runs")
56
+ runs_list.add_argument("--server", default="http://localhost:8000", help="Server URL")
57
+ runs_list.add_argument("--status", help="Filter by status")
58
+
59
+ runs_show = runs_subparsers.add_parser("show", help="Show run details")
60
+ runs_show.add_argument("run_id", help="Run ID")
61
+ runs_show.add_argument("--server", default="http://localhost:8000", help="Server URL")
62
+
63
+ runs_cancel = runs_subparsers.add_parser("cancel", help="Cancel a run")
64
+ runs_cancel.add_argument("run_id", help="Run ID")
65
+ runs_cancel.add_argument("--server", default="http://localhost:8000", help="Server URL")
66
+
33
67
  args = parser.parse_args()
34
68
 
35
69
  if not args.command:
36
70
  parser.print_help()
37
71
  sys.exit(1)
38
72
 
39
- # Load config
40
- try:
41
- config = PyocoConfig.from_yaml(args.config)
42
- except Exception as e:
43
- print(f"Error loading config: {e}")
44
- sys.exit(1)
73
+ # Load config only if needed
74
+ config = None
75
+ if hasattr(args, 'config') and args.config:
76
+ try:
77
+ config = PyocoConfig.from_yaml(args.config)
78
+ except Exception as e:
79
+ print(f"Error loading config: {e}")
80
+ sys.exit(1)
45
81
 
46
- # Discover tasks
47
- loader = TaskLoader(config)
48
- loader.load()
82
+ # Discover tasks only if config is loaded
83
+ loader = None
84
+ if config:
85
+ loader = TaskLoader(config)
86
+ loader.load()
49
87
 
50
88
  if args.command == "list-tasks":
89
+ if not loader:
90
+ print("Error: Config not loaded.")
91
+ sys.exit(1)
51
92
  print("Available tasks:")
52
93
  for name in loader.tasks:
53
94
  print(f" - {name}")
54
95
  return
55
96
 
97
+ if args.command == "server":
98
+ if args.server_command == "start":
99
+ import uvicorn
100
+ print(f"🐇 Starting Kanban Server on {args.host}:{args.port}")
101
+ uvicorn.run("pyoco.server.api:app", host=args.host, port=args.port, log_level="info")
102
+ return
103
+
104
+ if args.command == "worker":
105
+ if args.worker_command == "start":
106
+ from ..worker.runner import Worker
107
+ tags = args.tags.split(",") if args.tags else []
108
+ worker = Worker(args.server, config, tags)
109
+ worker.start()
110
+ return
111
+
112
+ if args.command == "runs":
113
+ client = Client(args.server)
114
+ try:
115
+ if args.runs_command == "list":
116
+ runs = client.list_runs(status=args.status)
117
+ print(f"🐇 Active Runs ({len(runs)}):")
118
+ print(f"{'ID':<36} | {'Status':<12} | {'Flow':<15}")
119
+ print("-" * 70)
120
+ for r in runs:
121
+ # RunContext doesn't have flow_name in core model, but store adds it.
122
+ # We need to access it safely.
123
+ flow_name = r.get("flow_name", "???")
124
+ print(f"{r['run_id']:<36} | {r['status']:<12} | {flow_name:<15}")
125
+
126
+ elif args.runs_command == "show":
127
+ run = client.get_run(args.run_id)
128
+ print(f"🐇 Run: {run['run_id']}")
129
+ print(f"Status: {run['status']}")
130
+ print("Tasks:")
131
+ for t_name, t_state in run.get("tasks", {}).items():
132
+ print(f" [{t_state}] {t_name}")
133
+
134
+ elif args.runs_command == "cancel":
135
+ client.cancel_run(args.run_id)
136
+ print(f"🛑 Cancellation requested for run {args.run_id}")
137
+ except Exception as e:
138
+ print(f"Error: {e}")
139
+ return
140
+
56
141
  if args.command == "run":
57
142
  flow_conf = config.flows.get(args.flow)
58
143
  if not flow_conf:
59
144
  print(f"Flow '{args.flow}' not found in config.")
60
145
  sys.exit(1)
61
146
 
147
+ # Params
148
+ params = flow_conf.defaults.copy()
149
+ if args.param:
150
+ for p in args.param:
151
+ if "=" in p:
152
+ k, v = p.split("=", 1)
153
+ params[k] = v # Simple string parsing for now
154
+
155
+ if args.server:
156
+ # Remote execution
157
+ client = Client(args.server)
158
+ try:
159
+ run_id = client.submit_run(args.flow, params)
160
+ print(f"🚀 Flow submitted! Run ID: {run_id}")
161
+ print(f"📋 View status: pyoco runs show {run_id} --server {args.server}")
162
+ except Exception as e:
163
+ print(f"Error submitting flow: {e}")
164
+ sys.exit(1)
165
+ return
62
166
  # Build Flow from graph string
63
167
  from ..dsl.syntax import TaskWrapper
64
168
  eval_context = {name: TaskWrapper(task) for name, task in loader.tasks.items()}
@@ -76,13 +180,15 @@ def main():
76
180
  backend = ConsoleTraceBackend(style="cute" if args.cute else "plain")
77
181
  engine = Engine(trace_backend=backend)
78
182
 
79
- # Params
80
- params = flow_conf.defaults.copy()
81
- if args.param:
82
- for p in args.param:
83
- if "=" in p:
84
- k, v = p.split("=", 1)
85
- params[k] = v # Simple string parsing for now
183
+ # Params (Moved up)
184
+
185
+ # Signal handler for cancellation
186
+ def signal_handler(sig, frame):
187
+ print("\n🛑 Ctrl+C detected. Cancelling active runs...")
188
+ for rid in list(engine.active_runs.keys()):
189
+ engine.cancel(rid)
190
+
191
+ signal.signal(signal.SIGINT, signal_handler)
86
192
 
87
193
  engine.run(flow, params)
88
194
 
pyoco/client.py ADDED
@@ -0,0 +1,69 @@
1
+ import httpx
2
+ from typing import Dict, List, Optional, Any
3
+ from .core.models import RunStatus, TaskState
4
+
5
+ class Client:
6
+ def __init__(self, server_url: str, client_id: str = "cli"):
7
+ self.server_url = server_url.rstrip("/")
8
+ self.client_id = client_id
9
+ self.client = httpx.Client(base_url=self.server_url)
10
+
11
+ def submit_run(self, flow_name: str, params: Dict[str, Any], tags: List[str] = []) -> str:
12
+ resp = self.client.post("/runs", json={
13
+ "flow_name": flow_name,
14
+ "params": params,
15
+ "tags": tags
16
+ })
17
+ resp.raise_for_status()
18
+ return resp.json()["run_id"]
19
+
20
+ def list_runs(self, status: Optional[str] = None) -> List[Dict]:
21
+ params = {}
22
+ if status:
23
+ params["status"] = status
24
+ resp = self.client.get("/runs", params=params)
25
+ resp.raise_for_status()
26
+ return resp.json()
27
+
28
+ def get_run(self, run_id: str) -> Dict:
29
+ resp = self.client.get(f"/runs/{run_id}")
30
+ resp.raise_for_status()
31
+ return resp.json()
32
+
33
+ def cancel_run(self, run_id: str):
34
+ resp = self.client.post(f"/runs/{run_id}/cancel")
35
+ resp.raise_for_status()
36
+
37
+ def poll(self, tags: List[str] = []) -> Optional[Dict[str, Any]]:
38
+ try:
39
+ resp = self.client.post("/workers/poll", json={
40
+ "worker_id": self.client_id,
41
+ "tags": tags
42
+ })
43
+ resp.raise_for_status()
44
+ data = resp.json()
45
+ if data.get("run_id"):
46
+ return data
47
+ return None
48
+ except Exception as e:
49
+ # print(f"Poll failed: {e}")
50
+ return None
51
+
52
+ def heartbeat(self, run_id: str, task_states: Dict[str, TaskState], run_status: RunStatus) -> bool:
53
+ """
54
+ Sends heartbeat. Returns True if cancellation is requested.
55
+ """
56
+ try:
57
+ # Convert Enums to values
58
+ states_json = {k: v.value if hasattr(v, 'value') else v for k, v in task_states.items()}
59
+ status_value = run_status.value if hasattr(run_status, 'value') else run_status
60
+
61
+ resp = self.client.post(f"/runs/{run_id}/heartbeat", json={
62
+ "task_states": states_json,
63
+ "run_status": status_value
64
+ })
65
+ resp.raise_for_status()
66
+ return resp.json().get("cancel_requested", False)
67
+ except Exception as e:
68
+ print(f"Heartbeat failed: {e}")
69
+ return False
pyoco/core/context.py CHANGED
@@ -1,21 +1,37 @@
1
1
  import threading
2
- from typing import Any, Dict, Optional
2
+ from typing import Any, Dict, List, Optional
3
3
  from dataclasses import dataclass, field
4
+ from .models import RunContext
4
5
 
5
6
  @dataclass
6
7
  class Context:
8
+ """
9
+ Execution context passed to tasks.
10
+ """
7
11
  params: Dict[str, Any] = field(default_factory=dict)
8
- env: Dict[str, str] = field(default_factory=dict)
9
12
  results: Dict[str, Any] = field(default_factory=dict)
10
13
  scratch: Dict[str, Any] = field(default_factory=dict)
11
14
  artifacts: Dict[str, Any] = field(default_factory=dict)
12
- run_id: Optional[str] = None
13
- artifact_dir: str = field(default="./artifacts")
15
+ env: Dict[str, str] = field(default_factory=dict)
16
+ artifact_dir: Optional[str] = None
17
+
18
+ # Reference to the parent run context (v0.2.0+)
19
+ run_context: Optional[RunContext] = None
14
20
 
15
21
  _lock: threading.Lock = field(default_factory=threading.Lock, repr=False)
16
22
 
23
+ @property
24
+ def is_cancelled(self) -> bool:
25
+ if self.run_context:
26
+ from .models import RunStatus
27
+ return self.run_context.status in [RunStatus.CANCELLING, RunStatus.CANCELLED]
28
+ return False
29
+
17
30
  def __post_init__(self):
18
31
  # Ensure artifact directory exists
32
+ if self.artifact_dir is None:
33
+ self.artifact_dir = "./artifacts"
34
+
19
35
  import pathlib
20
36
  pathlib.Path(self.artifact_dir).mkdir(parents=True, exist_ok=True)
21
37