-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathcron_cmd.py
More file actions
875 lines (793 loc) · 29.5 KB
/
Copy pathcron_cmd.py
File metadata and controls
875 lines (793 loc) · 29.5 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
"""Cron scheduler CLI commands backed by OpenSquilla gateway RPCs."""
from __future__ import annotations
import os
import re
from pathlib import Path
from typing import Annotated, Any
import typer
from rich.table import Table
from opensquilla.cli.gateway_rpc import confirm_or_exit, run_gateway_sync
from opensquilla.cli.output import exit_invalid_request, print_json
from opensquilla.cli.ui import ACCENT_HEADER, console
cron_app = typer.Typer(help="Inspect and manage scheduled OpenSquilla runs.")
_SESSION_TARGETS = {"isolated", "main", "current", "session"}
_JOB_KINDS = {"auto", "reminder", "agent_turn", "system_event"}
_WAKE_MODES = {"now", "next-heartbeat"}
def _validate_session_target(value: str) -> str:
normalized = value.strip().lower()
if normalized not in _SESSION_TARGETS:
raise typer.BadParameter(
"--session-target must be one of isolated, main, current, session"
)
return normalized
def _validate_job_kind(value: Any) -> str:
if not isinstance(value, str):
return "auto"
normalized = value.strip().lower()
if normalized not in _JOB_KINDS:
raise typer.BadParameter(
"--job-kind must be one of auto, reminder, agent_turn, system_event"
)
return normalized
_DURATION_RE = re.compile(r"^\s*(\d+(?:\.\d+)?)\s*(s|m|h|sec|secs|min|mins|hr|hrs)?\s*$")
_DURATION_UNIT_SECONDS = {
"": 1.0,
"s": 1.0,
"sec": 1.0,
"secs": 1.0,
"m": 60.0,
"min": 60.0,
"mins": 60.0,
"h": 3600.0,
"hr": 3600.0,
"hrs": 3600.0,
}
def _parse_duration_seconds(value: str | float | None) -> float | None:
"""Accept '30s', '5m', '1h', or a plain numeric string/float. Return seconds.
None / empty string → None (caller decides default). Anything ambiguous
raises typer.BadParameter so the user sees the failure at parse time.
"""
if value is None:
return None
if isinstance(value, (int, float)):
return float(value)
raw = str(value).strip()
if not raw:
return None
match = _DURATION_RE.match(raw.lower())
if not match:
raise typer.BadParameter(
f"invalid duration {value!r}: expected '30s', '5m', '1h', or seconds"
)
qty = float(match.group(1))
unit = match.group(2) or ""
return qty * _DURATION_UNIT_SECONDS[unit]
def _build_schedule_param(
*,
expression: str | None,
cron: str | None,
every: str | int | float | None,
at: str | None,
tz: str | None,
) -> dict[str, Any]:
sources = [
("expression", expression.strip() if isinstance(expression, str) else expression),
("cron", cron.strip() if isinstance(cron, str) else cron),
("every", every),
("at", at.strip() if isinstance(at, str) else at),
]
provided = [(name, value) for name, value in sources if value not in (None, "")]
if len(provided) != 1:
raise typer.BadParameter(
"provide exactly one schedule source: --expression, --cron, --every, or --at"
)
name, value = provided[0]
if name == "expression":
return {"expression": str(value)}
if name == "cron":
schedule: dict[str, Any] = {"kind": "cron", "expr": str(value)}
if tz:
schedule["tz"] = tz
return {"schedule": schedule}
if name == "every":
seconds = _parse_duration_seconds(value)
if seconds is None or seconds < 1:
raise typer.BadParameter("--every must be a duration >= 1 second")
if not seconds.is_integer():
raise typer.BadParameter("--every must resolve to whole seconds")
return {"schedule": {"kind": "every", "every_seconds": int(seconds)}}
return {"schedule": {"kind": "at", "at": str(value)}}
def _build_optional_schedule_param(
*,
expression: str | None,
cron: str | None,
every: str | int | float | None,
at: str | None,
tz: str | None,
) -> dict[str, Any]:
sources = [
expression.strip() if isinstance(expression, str) else expression,
cron.strip() if isinstance(cron, str) else cron,
every,
at.strip() if isinstance(at, str) else at,
]
if all(value in (None, "") for value in sources):
return {}
if sum(1 for value in sources if value not in (None, "")) > 1:
raise typer.BadParameter(
"provide at most one schedule source: --expression, --cron, --every, or --at"
)
return _build_schedule_param(
expression=expression,
cron=cron,
every=every,
at=at,
tz=tz,
)
def _resolve_webhook_token(
*,
inline: str | None,
env: str | None,
path: str | None,
) -> str | None:
"""Resolve a webhook bearer token from the safest available source.
Priority: --webhook-token-env > --webhook-token-file > --webhook-token.
Multiple sources is a ValueError so scripts fail loud instead of guessing.
Inline --webhook-token is supported but emits a warning because it leaks
via shell history and process listings.
"""
sources = [bool(env), bool(path), bool(inline)]
if sum(sources) > 1:
raise typer.BadParameter(
"specify at most one of --webhook-token, --webhook-token-env, "
"--webhook-token-file"
)
if env:
value = os.environ.get(env)
if not value:
raise typer.BadParameter(
f"--webhook-token-env: environment variable {env!r} is unset or empty"
)
return value
if path:
try:
return Path(path).expanduser().read_text(encoding="utf-8").strip()
except OSError as exc:
raise typer.BadParameter(f"--webhook-token-file: {exc}") from exc
if inline:
typer.echo(
"warning: --webhook-token is visible in shell history and process "
"listings; prefer --webhook-token-env NAME or --webhook-token-file PATH.",
err=True,
)
return inline
return None
def _build_failure_destination_dict(
*,
mode: str | None,
channel: str | None,
to: str | None,
account: str | None,
webhook_url: str | None,
webhook_token: str | None,
) -> dict[str, Any] | None:
"""Translate --failure-* flags into a delivery.failureDestination dict.
Returns None when no failure-* flag is set. Raises BadParameter when the
selected mode is missing required fields (webhook URL for webhook mode,
channel + recipient for channel mode).
"""
any_failure_flag = any(
bool(v) for v in (mode, channel, to, account, webhook_url, webhook_token)
)
if not any_failure_flag:
return None
if not mode:
raise typer.BadParameter(
"--failure-* flags require --failure-mode (channel or webhook)"
)
mode_norm = mode.strip().lower()
if mode_norm not in ("channel", "webhook"):
raise typer.BadParameter("--failure-mode must be 'channel' or 'webhook'")
if mode_norm == "webhook":
if not webhook_url:
raise typer.BadParameter(
"--failure-mode=webhook requires --failure-webhook-url"
)
fd: dict[str, Any] = {"mode": "webhook", "webhookUrl": webhook_url}
if webhook_token:
fd["webhookToken"] = webhook_token
return fd
# channel mode
if webhook_url or webhook_token:
raise typer.BadParameter(
"--failure-webhook-* requires --failure-mode=webhook"
)
if not (channel or to):
raise typer.BadParameter(
"--failure-mode=channel requires --failure-channel and/or --failure-to"
)
fd = {"mode": "channel"}
if channel:
fd["channelName"] = channel.strip().lower()
if to:
fd["to"] = to
if account:
fd["accountId"] = account
return fd
def _build_delivery_params(
*,
announce: bool,
no_deliver: bool,
channel: str | None,
to: str | None,
account: str | None,
best_effort: bool,
webhook_url: str | None,
webhook_token: str | None,
failure_destination: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
"""Translate CLI delivery flags into a delivery dict for the cron.add RPC.
Returns None when the user did not request any delivery override AND no
failure_destination was provided — the backend then falls back to its
own inference (session last_channel or none). When only a failure
destination is set, returns ``{"failureDestination": {...}}`` so the
backend attaches it to the inferred or default primary delivery.
"""
declared = sum([announce, no_deliver, bool(webhook_url)])
if declared > 1:
raise typer.BadParameter(
"choose at most one delivery mode: --announce, --no-deliver, "
"or --webhook-url"
)
if webhook_url:
delivery: dict[str, Any] = {"mode": "webhook", "webhookUrl": webhook_url}
if webhook_token:
delivery["webhookToken"] = webhook_token
if best_effort:
delivery["bestEffort"] = True
if failure_destination is not None:
delivery["failureDestination"] = failure_destination
return delivery
if webhook_token and not webhook_url:
raise typer.BadParameter(
"--webhook-token* requires --webhook-url"
)
if no_deliver:
result: dict[str, Any] = {"mode": "none"}
if failure_destination is not None:
result["failureDestination"] = failure_destination
return result
# Channel-mode announce. 'last' is a CLI sentinel that means "let the
# backend infer from the session's last route"; do not forward it as an
# explicit channelName because the RPC's _parse_delivery_overrides treats
# any channelName as an explicit override.
channel_norm = (channel or "").strip().lower()
has_target = bool(to) or (channel_norm not in ("", "last"))
if not announce and not has_target and not best_effort and not account:
if failure_destination is not None:
# FD-only — backend keeps inferred primary delivery and attaches FD.
return {"failureDestination": failure_destination}
return None # nothing requested
delivery = {"mode": "announce"}
if channel_norm and channel_norm != "last":
delivery["channelName"] = channel_norm
if to:
delivery["to"] = to
if account:
delivery["accountId"] = account
if best_effort:
delivery["bestEffort"] = True
if failure_destination is not None:
delivery["failureDestination"] = failure_destination
return delivery
def _job_rows(payload: Any) -> list[dict[str, Any]]:
if isinstance(payload, list):
return [row for row in payload if isinstance(row, dict)]
if isinstance(payload, dict):
rows = payload.get("jobs", [])
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
return []
def _render_jobs(rows: list[dict[str, Any]], *, title: str = "Cron jobs") -> None:
if not rows:
typer.echo("No cron jobs.")
return
table = Table(title=title, show_header=True, header_style=ACCENT_HEADER)
table.add_column("ID")
table.add_column("Name")
table.add_column("Enabled")
table.add_column("Expression")
table.add_column("Agent")
table.add_column("Next run")
table.add_column("Last run")
table.add_column("Errors", justify="right")
for row in rows:
table.add_row(
str(row.get("id") or ""),
str(row.get("name") or ""),
str(row.get("enabled") or False),
str(row.get("expression") or row.get("schedule_raw") or ""),
str(row.get("agentId") or row.get("agent_id") or ""),
str(row.get("next_run") or ""),
str(row.get("last_run") or ""),
str(row.get("error_count") or row.get("consecutive_errors") or 0),
)
console.print(table)
def _render_mapping(payload: dict[str, Any], *, title: str) -> None:
table = Table(title=title, show_header=True, header_style=ACCENT_HEADER)
table.add_column("Field")
table.add_column("Value")
for key, value in payload.items():
table.add_row(str(key), str(value))
console.print(table)
def _render_runs(rows: list[dict[str, Any]]) -> None:
if not rows:
typer.echo("No cron runs.")
return
table = Table(title="Cron runs", show_header=True, header_style=ACCENT_HEADER)
table.add_column("ID")
table.add_column("Started")
table.add_column("Finished")
table.add_column("Status")
table.add_column("Duration ms", justify="right")
table.add_column("Error")
for row in rows:
table.add_row(
str(row.get("id") or ""),
str(row.get("started_at") or ""),
str(row.get("finished_at") or ""),
str(row.get("status") or ("ok" if row.get("success") else "error")),
str(row.get("duration_ms") or ""),
str(row.get("error") or ""),
)
console.print(table)
def _emit_success(payload: Any, *, json_output: bool, title: str) -> None:
if json_output:
print_json(payload)
elif isinstance(payload, dict):
_render_mapping(payload, title=title)
else:
typer.echo(str(payload))
@cron_app.command("list")
def cron_list(
agent: str | None = typer.Option(None, "--agent", help="Filter by agent id"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""List scheduled cron jobs."""
async def _run(client):
params: dict[str, Any] = {}
if agent:
params["agentId"] = agent
return await client.call("cron.list", params)
payload = run_gateway_sync(_run, json_output=json_output)
if json_output:
print_json(payload)
return
_render_jobs(_job_rows(payload))
@cron_app.command("status")
def cron_status(
job_id: str = typer.Argument(..., help="Cron job id"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Show one cron job."""
async def _run(client):
return await client.call("cron.status", {"id": job_id})
payload = run_gateway_sync(_run, json_output=json_output)
_emit_success(payload, json_output=json_output, title=f"Cron job {job_id}")
@cron_app.command("add")
def cron_add(
expression: Annotated[
str | None, typer.Option("--expression", help="Cron expression")
] = None,
cron: Annotated[
str | None, typer.Option("--cron", help="Cron expression schedule")
] = None,
every: Annotated[
str | None, typer.Option("--every", help="Fixed interval, e.g. 30s, 5m, 1h")
] = None,
at: Annotated[
str | None, typer.Option("--at", help="One-time ISO-8601 time with timezone")
] = None,
text: str = typer.Option(..., "--text", help="Prompt text to run"),
name: str | None = typer.Option(None, "--name", help="Display name"),
agent: str | None = typer.Option(None, "--agent", help="Agent id"),
job_kind: str = typer.Option(
"auto",
"--job-kind",
help=(
"Cron payload kind: auto, reminder, agent_turn, or system_event. "
"auto creates static reminders for non-main targets and system events for main."
),
),
session_target: str = typer.Option(
"isolated",
"--session-target",
help="Target session mode: isolated, main, current, or session",
),
timeout: float | None = typer.Option(None, "--timeout", help="Run timeout in seconds"),
tz: str | None = typer.Option(
None,
"--tz",
help=(
"IANA timezone for cron expressions (e.g. 'America/Los_Angeles'). "
"Empty/omitted keeps UTC."
),
),
wake: str | None = typer.Option(
None,
"--wake",
help="Wake mode for main-session jobs: now or next-heartbeat",
),
exact: bool = typer.Option(
False,
"--exact",
help="Fire exactly on schedule (jitter_seconds=0); overrides default stagger.",
),
jitter: str | None = typer.Option(
None,
"--jitter",
help=(
"Explicit stagger (per-job). Accepts '30s', '5m', '1h', or a numeric "
"second count. 0 == --exact; takes precedence over --exact."
),
),
announce: bool = typer.Option(
False, "--announce", help="Announce summary delivery (channel mode)."
),
no_deliver: bool = typer.Option(
False,
"--no-deliver",
help="Disable any delivery for this job.",
),
channel: str | None = typer.Option(
None,
"--channel",
help=(
"Delivery channel (e.g. slack, feishu). 'last' or unset → let the "
"backend infer from the session's last route."
),
),
to: str | None = typer.Option(
None, "--to", help="Delivery destination (channel-specific recipient)"
),
account: str | None = typer.Option(
None, "--account", help="Channel account id for delivery (multi-account setups)"
),
best_effort_deliver: bool = typer.Option(
False,
"--best-effort-deliver",
help="Do not fail the job when delivery fails",
),
webhook_url: str | None = typer.Option(
None,
"--webhook-url",
help="Webhook delivery URL (http/https); mutually exclusive with --announce/--no-deliver.",
),
webhook_token: str | None = typer.Option(
None,
"--webhook-token",
help=(
"Webhook bearer token (visible in shell history; prefer "
"--webhook-token-env or --webhook-token-file)."
),
),
webhook_token_env: str | None = typer.Option(
None,
"--webhook-token-env",
help="Read webhook bearer token from this environment variable.",
),
webhook_token_file: str | None = typer.Option(
None,
"--webhook-token-file",
help="Read webhook bearer token from this file (whitespace-trimmed).",
),
failure_mode: str | None = typer.Option(
None,
"--failure-mode",
help=(
"Route failure alerts separately from primary delivery. "
"One of: 'channel', 'webhook'."
),
),
failure_channel: str | None = typer.Option(
None,
"--failure-channel",
help="Failure-destination channel name (slack, feishu, …) for --failure-mode=channel.",
),
failure_to: str | None = typer.Option(
None,
"--failure-to",
help="Failure-destination recipient (channel-specific) for --failure-mode=channel.",
),
failure_account: str | None = typer.Option(
None,
"--failure-account",
help="Failure-destination channel account id (multi-account setups).",
),
failure_webhook_url: str | None = typer.Option(
None,
"--failure-webhook-url",
help="Failure-destination webhook URL (http/https) for --failure-mode=webhook.",
),
failure_webhook_token: str | None = typer.Option(
None,
"--failure-webhook-token",
help=(
"Failure-destination webhook bearer token (visible in shell history; "
"prefer --failure-webhook-token-env or --failure-webhook-token-file)."
),
),
failure_webhook_token_env: str | None = typer.Option(
None,
"--failure-webhook-token-env",
help="Read failure-destination webhook token from this environment variable.",
),
failure_webhook_token_file: str | None = typer.Option(
None,
"--failure-webhook-token-file",
help="Read failure-destination webhook token from this file (trimmed).",
),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Add a scheduled cron job."""
target = _validate_session_target(session_target)
payload_kind = _validate_job_kind(job_kind)
if payload_kind == "auto":
payload_kind = "system_event" if target == "main" else "reminder"
if payload_kind == "reminder" and target == "main":
raise typer.BadParameter("--job-kind reminder cannot use --session-target main")
if payload_kind == "agent_turn" and target == "main":
raise typer.BadParameter("--job-kind agent_turn cannot use --session-target main")
if payload_kind == "system_event" and target != "main":
raise typer.BadParameter("--job-kind system_event requires --session-target main")
if target == "current":
raise typer.BadParameter(
"--session-target current is only available from session-bound clients; "
"use the WebUI/current chat surface or choose --session-target isolated"
)
params: dict[str, Any] = {
**_build_schedule_param(
expression=expression,
cron=cron,
every=every,
at=at,
tz=tz,
),
"text": text,
"payloadKind": payload_kind,
"sessionTarget": target,
}
if name:
params["name"] = name
if agent:
params["agentId"] = agent
if timeout is not None:
params["timeout"] = timeout
if tz:
params["tz"] = tz
if wake is not None:
wake_norm = wake.strip().lower()
if wake_norm not in _WAKE_MODES:
raise typer.BadParameter("--wake must be now or next-heartbeat")
params["wakeMode"] = wake_norm
jitter_seconds = _parse_duration_seconds(jitter)
if jitter_seconds is not None:
params["jitterSeconds"] = max(0.0, jitter_seconds)
elif exact:
params["exact"] = True
token = _resolve_webhook_token(
inline=webhook_token,
env=webhook_token_env,
path=webhook_token_file,
)
failure_token = _resolve_webhook_token(
inline=failure_webhook_token,
env=failure_webhook_token_env,
path=failure_webhook_token_file,
)
failure_destination = _build_failure_destination_dict(
mode=failure_mode,
channel=failure_channel,
to=failure_to,
account=failure_account,
webhook_url=failure_webhook_url,
webhook_token=failure_token,
)
delivery = _build_delivery_params(
announce=announce,
no_deliver=no_deliver,
channel=channel,
to=to,
account=account,
best_effort=best_effort_deliver,
webhook_url=webhook_url,
webhook_token=token,
failure_destination=failure_destination,
)
if delivery is not None:
params["delivery"] = delivery
async def _run(client):
return await client.call("cron.add", params)
payload = run_gateway_sync(_run, json_output=json_output)
_emit_success(payload, json_output=json_output, title="Cron job added")
@cron_app.command("update")
def cron_update(
job_id: str = typer.Argument(..., help="Cron job id"),
expression: Annotated[
str | None, typer.Option("--expression", help="Cron expression")
] = None,
cron: Annotated[
str | None, typer.Option("--cron", help="Cron expression schedule")
] = None,
every: Annotated[
str | None, typer.Option("--every", help="Fixed interval, e.g. 30s, 5m, 1h")
] = None,
at: Annotated[
str | None, typer.Option("--at", help="One-time ISO-8601 time with timezone")
] = None,
text: str | None = typer.Option(None, "--text", help="Prompt text to run"),
name: str | None = typer.Option(None, "--name", help="Display name"),
enabled: bool | None = typer.Option(None, "--enabled/--disabled", help="Enable/disable job"),
timeout: float | None = typer.Option(None, "--timeout", help="Run timeout in seconds"),
tz: str | None = typer.Option(
None,
"--tz",
help=(
"Update IANA timezone for cron expressions. Pass an empty string to clear "
"(revert to UTC matching)."
),
),
wake: str | None = typer.Option(
None,
"--wake",
help="Wake mode: now or next-heartbeat",
),
failure_mode: str | None = typer.Option(
None,
"--failure-mode",
help=(
"Patch the failure-alert route. One of: 'channel', 'webhook'. "
"Other delivery fields (primary channel/webhook) are not patchable "
"from this CLI — remove + re-add to repoint primary delivery."
),
),
failure_channel: str | None = typer.Option(
None,
"--failure-channel",
help="Failure-destination channel name for --failure-mode=channel.",
),
failure_to: str | None = typer.Option(
None,
"--failure-to",
help="Failure-destination recipient for --failure-mode=channel.",
),
failure_account: str | None = typer.Option(
None,
"--failure-account",
help="Failure-destination channel account id (multi-account setups).",
),
failure_webhook_url: str | None = typer.Option(
None,
"--failure-webhook-url",
help="Failure-destination webhook URL for --failure-mode=webhook.",
),
failure_webhook_token: str | None = typer.Option(
None,
"--failure-webhook-token",
help=(
"Failure-destination webhook bearer token (prefer "
"--failure-webhook-token-env or --failure-webhook-token-file)."
),
),
failure_webhook_token_env: str | None = typer.Option(
None,
"--failure-webhook-token-env",
help="Read failure-destination webhook token from this environment variable.",
),
failure_webhook_token_file: str | None = typer.Option(
None,
"--failure-webhook-token-file",
help="Read failure-destination webhook token from this file (trimmed).",
),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Update a scheduled cron job.
Primary delivery (channel / webhook URL) is intentionally NOT patchable
from this CLI — remove + re-add when those need to change. Failure
destination IS patchable via the --failure-* flags.
"""
params: dict[str, Any] = {"id": job_id}
params.update(
_build_optional_schedule_param(
expression=expression,
cron=cron,
every=every,
at=at,
tz=tz,
)
)
if text is not None:
params["text"] = text
if name is not None:
params["name"] = name
if enabled is not None:
params["enabled"] = enabled
if timeout is not None:
params["timeout"] = timeout
if tz is not None:
params["tz"] = tz
if wake is not None:
wake_norm = wake.strip().lower()
if wake_norm not in _WAKE_MODES:
raise typer.BadParameter("--wake must be now or next-heartbeat")
params["wakeMode"] = wake_norm
failure_token = _resolve_webhook_token(
inline=failure_webhook_token,
env=failure_webhook_token_env,
path=failure_webhook_token_file,
)
failure_destination = _build_failure_destination_dict(
mode=failure_mode,
channel=failure_channel,
to=failure_to,
account=failure_account,
webhook_url=failure_webhook_url,
webhook_token=failure_token,
)
if failure_destination is not None:
params["delivery"] = {"failureDestination": failure_destination}
if len(params) == 1:
raise typer.BadParameter("provide at least one field to update")
async def _run(client):
return await client.call("cron.update", params)
payload = run_gateway_sync(_run, json_output=json_output)
_emit_success(payload, json_output=json_output, title="Cron job updated")
@cron_app.command("remove")
def cron_remove(
job_id: str = typer.Argument(..., help="Cron job id"),
yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Remove a scheduled cron job."""
confirm_or_exit(f"Remove cron job {job_id!r}?", yes=yes, json_output=json_output)
async def _run(client):
await client.call("cron.remove", {"id": job_id})
return {"id": job_id, "removed": True}
payload = run_gateway_sync(_run, json_output=json_output)
_emit_success(payload, json_output=json_output, title="Cron job removed")
@cron_app.command("run")
def cron_run(
job_id: str = typer.Argument(..., help="Cron job id"),
yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""Run a scheduled cron job now."""
confirm_or_exit(
f"Run cron job {job_id!r} now? This may post into a live session or channel.",
yes=yes,
json_output=json_output,
)
async def _run(client):
return await client.call("cron.run", {"id": job_id})
payload = run_gateway_sync(_run, json_output=json_output)
_emit_success(payload, json_output=json_output, title="Cron run result")
@cron_app.command("runs")
def cron_runs(
job_id: str = typer.Argument(..., help="Cron job id"),
limit: int = typer.Option(20, "--limit", "-n", help="Maximum rows"),
json_output: bool = typer.Option(False, "--json", help="Emit machine-readable JSON"),
) -> None:
"""List recent runs for a cron job."""
if limit < 1:
exit_invalid_request(
"--limit must be >= 1",
json_output=json_output,
details={"parameter": "limit", "minimum": 1},
)
async def _run(client):
return await client.call("cron.runs", {"id": job_id, "limit": limit})
payload = run_gateway_sync(_run, json_output=json_output)
if json_output:
print_json(payload)
return
_render_runs(_job_rows(payload))