forked from agentscope-ai/QwenPaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron_cmd.py
More file actions
405 lines (378 loc) · 11.4 KB
/
cron_cmd.py
File metadata and controls
405 lines (378 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
from __future__ import annotations
import json
from pathlib import Path
from typing import Optional
import click
from .http import client, print_json
from ..app.channels.schema import DEFAULT_CHANNEL
def _base_url(ctx: click.Context, base_url: Optional[str]) -> str:
"""Resolve base_url with priority:
1) command --base-url
2) global --host/--port
(already resolved in main.py, may come from config.json)
"""
if base_url:
return base_url.rstrip("/")
host = (ctx.obj or {}).get("host", "127.0.0.1")
port = (ctx.obj or {}).get("port", 8088)
return f"http://{host}:{port}"
@click.group("cron")
def cron_group() -> None:
"""Manage scheduled cron jobs via the HTTP API (/cron).
Use list/get/state to inspect jobs; create/delete to add or remove;
pause/resume to toggle execution; run to trigger a one-off run.
"""
@cron_group.command("list")
@click.option(
"--base-url",
default=None,
help=(
"Override the API base URL (e.g. http://127.0.0.1:8088). "
"If omitted, uses global --host and --port from config."
),
)
@click.pass_context
def list_jobs(ctx: click.Context, base_url: Optional[str]) -> None:
"""List all cron jobs. Output is JSON from GET /cron/jobs."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.get("/cron/jobs")
r.raise_for_status()
print_json(r.json())
@cron_group.command("get")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def get_job(ctx: click.Context, job_id: str, base_url: Optional[str]) -> None:
"""Fetch a cron job by ID. Returns JSON from GET /cron/jobs/<id>."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.get(f"/cron/jobs/{job_id}")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())
@cron_group.command("state")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def job_state(
ctx: click.Context,
job_id: str,
base_url: Optional[str],
) -> None:
"""Get the runtime state of a cron job (e.g. next run time, paused)."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.get(f"/cron/jobs/{job_id}/state")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())
def _build_spec_from_cli(
task_type: str,
name: str,
cron: str,
channel: str,
target_user: str,
target_session: str,
text: Optional[str],
timezone: str,
enabled: bool,
mode: str,
) -> dict:
"""Build CronJobSpec JSON payload from CLI args (no id)."""
schedule = {"type": "cron", "cron": cron, "timezone": timezone}
dispatch = {
"type": "channel",
"channel": channel,
"target": {"user_id": target_user, "session_id": target_session},
"mode": mode,
"meta": {},
}
runtime = {
"max_concurrency": 1,
"timeout_seconds": 120,
"misfire_grace_seconds": 60,
}
if task_type == "text":
if not (text and text.strip()):
raise click.UsageError(
"--text is required when task type is 'text'",
)
return {
"id": "",
"name": name,
"enabled": enabled,
"schedule": schedule,
"task_type": "text",
"text": text.strip(),
"dispatch": dispatch,
"runtime": runtime,
"meta": {},
}
if task_type == "agent":
if not (text and text.strip()):
raise click.UsageError(
"--text is required when task type is 'agent' "
"(the question/prompt sent to the agent)",
)
return {
"id": "",
"name": name,
"enabled": enabled,
"schedule": schedule,
"task_type": "agent",
"request": {
"input": [
{
"role": "user",
"type": "message",
"content": [{"type": "text", "text": text.strip()}],
},
],
"session_id": target_session,
"user_id": "cron",
},
"dispatch": dispatch,
"runtime": runtime,
"meta": {},
}
raise click.UsageError(f"Unsupported task type: {task_type}")
@cron_group.command("create")
@click.option(
"-f",
"--file",
"file_",
type=click.Path(exists=True, dir_okay=False, path_type=Path),
default=None,
help=(
"Path to a JSON file containing the full cron job spec. "
"Mutually exclusive with inline options (--type, --name, etc.)."
),
)
@click.option(
"--type",
"task_type",
type=click.Choice(["text", "agent"], case_sensitive=False),
default=None,
help=(
"Task type: 'text' sends fixed content to the channel; "
"'agent' sends a question to the agent and delivers the reply to the "
"channel. Required when not using -f/--file."
),
)
@click.option(
"--name",
default=None,
help="Display name for the job. Required when not using -f/--file.",
)
@click.option(
"--cron",
default=None,
help=(
"Cron expression (5 fields: minute hour day month weekday). "
"Example: '0 9 * * *' for daily at 09:00. Required without -f/--file."
),
)
@click.option(
"--channel",
default=None,
help=(
"Delivery channel: e.g. imessage, dingtalk, discord, qq, console. "
"Required when not using -f/--file."
),
)
@click.option(
"--target-user",
default=None,
help=(
"Target user_id for the channel (recipient identifier). "
"Required when not using -f/--file."
),
)
@click.option(
"--target-session",
default=None,
help=(
"Target session_id for the channel. "
"Required when not using -f/--file."
),
)
@click.option(
"--text",
default=None,
help=(
"Content: for 'text' tasks this is the message sent to the channel; "
"for 'agent' tasks this is the prompt/question sent to the agent. "
"Required for both task types."
),
)
@click.option(
"--timezone",
default="UTC",
help="Timezone for the cron schedule (e.g. UTC, America/New_York).",
)
@click.option(
"--enabled/--no-enabled",
default=True,
help="Create the job as enabled (--enabled) or disabled (--no-enabled).",
)
@click.option(
"--mode",
type=click.Choice(["stream", "final"], case_sensitive=False),
default="final",
help=(
"Delivery mode: 'stream' sends incremental updates; "
"'final' sends only the final result."
),
)
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def create_job(
ctx: click.Context,
file_: Optional[Path],
task_type: Optional[str],
name: Optional[str],
cron: Optional[str],
channel: Optional[str],
target_user: Optional[str],
target_session: Optional[str],
text: Optional[str],
timezone: str,
enabled: bool,
mode: str,
base_url: Optional[str],
) -> None:
"""Create a cron job.
Either pass -f/--file with a JSON spec, or use --type, --name, --cron,
--channel, --target-user, --target-session and --text to define the job
inline.
"""
base_url = _base_url(ctx, base_url)
if file_ is not None:
payload = json.loads(file_.read_text(encoding="utf-8"))
else:
for value, label in [
(task_type, "--type"),
(name, "--name"),
(cron, "--cron"),
(channel, "--channel"),
(target_user, "--target-user"),
(target_session, "--target-session"),
]:
if not value or (isinstance(value, str) and not value.strip()):
raise click.UsageError(
f"When creating without -f/--file, {label} is required",
)
payload = _build_spec_from_cli(
task_type=task_type or "agent",
name=name or "",
cron=cron or "",
channel=channel or DEFAULT_CHANNEL,
target_user=target_user or "",
target_session=target_session or "",
text=text,
timezone=timezone,
enabled=enabled,
mode=mode,
)
with client(base_url) as c:
r = c.post("/cron/jobs", json=payload)
r.raise_for_status()
print_json(r.json())
@cron_group.command("delete")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def delete_job(
ctx: click.Context,
job_id: str,
base_url: Optional[str],
) -> None:
"""Permanently delete a cron job. The job is removed from the server."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.delete(f"/cron/jobs/{job_id}")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())
@cron_group.command("pause")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def pause_job(
ctx: click.Context,
job_id: str,
base_url: Optional[str],
) -> None:
"""Pause a cron job so it no longer runs on schedule.
Use 'resume' to re-enable.
"""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.post(f"/cron/jobs/{job_id}/pause")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())
@cron_group.command("resume")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def resume_job(
ctx: click.Context,
job_id: str,
base_url: Optional[str],
) -> None:
"""Resume a paused cron job so it runs again on its schedule."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.post(f"/cron/jobs/{job_id}/resume")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())
@cron_group.command("run")
@click.argument("job_id", metavar="JOB_ID")
@click.option(
"--base-url",
default=None,
help="Override the API base URL. Defaults to global --host/--port.",
)
@click.pass_context
def run_job(ctx: click.Context, job_id: str, base_url: Optional[str]) -> None:
"""Trigger a one-off run of a cron job immediately (ignores schedule)."""
base_url = _base_url(ctx, base_url)
with client(base_url) as c:
r = c.post(f"/cron/jobs/{job_id}/run")
if r.status_code == 404:
raise click.ClickException("Job not found.")
r.raise_for_status()
print_json(r.json())