@qodalis/cli-server-jobs 2.0.0-beta.3

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.
package/public-api.js ADDED
@@ -0,0 +1,1001 @@
1
+ 'use strict';
2
+
3
+ var cliCore = require('@qodalis/cli-core');
4
+
5
+ // src/lib/processors/cli-jobs-command-processor.ts
6
+
7
+ // src/lib/services/cli-jobs-service.ts
8
+ var CliJobsService = class {
9
+ constructor(baseUrl, headers = {}) {
10
+ this.baseUrl = baseUrl;
11
+ this.headers = headers;
12
+ }
13
+ async request(path, init) {
14
+ const url = `${this.baseUrl}/api/v1/qcli/jobs${path}`;
15
+ const response = await fetch(url, {
16
+ ...init,
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ ...this.headers,
20
+ ...init?.headers ?? {}
21
+ }
22
+ });
23
+ if (!response.ok) {
24
+ let errorMessage = `${response.status} ${response.statusText}`;
25
+ try {
26
+ const body = await response.json();
27
+ if (body.error) {
28
+ errorMessage = body.error;
29
+ }
30
+ } catch {
31
+ }
32
+ throw new Error(errorMessage);
33
+ }
34
+ const text = await response.text();
35
+ if (!text) {
36
+ return void 0;
37
+ }
38
+ return JSON.parse(text);
39
+ }
40
+ async listJobs() {
41
+ return this.request("");
42
+ }
43
+ async getJob(id) {
44
+ return this.request(`/${encodeURIComponent(id)}`);
45
+ }
46
+ async triggerJob(id) {
47
+ await this.request(`/${encodeURIComponent(id)}/trigger`, {
48
+ method: "POST"
49
+ });
50
+ }
51
+ async pauseJob(id) {
52
+ await this.request(`/${encodeURIComponent(id)}/pause`, {
53
+ method: "POST"
54
+ });
55
+ }
56
+ async resumeJob(id) {
57
+ await this.request(`/${encodeURIComponent(id)}/resume`, {
58
+ method: "POST"
59
+ });
60
+ }
61
+ async stopJob(id) {
62
+ await this.request(`/${encodeURIComponent(id)}/stop`, {
63
+ method: "POST"
64
+ });
65
+ }
66
+ async cancelJob(id) {
67
+ await this.request(`/${encodeURIComponent(id)}/cancel`, {
68
+ method: "POST"
69
+ });
70
+ }
71
+ async updateJob(id, request) {
72
+ await this.request(`/${encodeURIComponent(id)}`, {
73
+ method: "PUT",
74
+ body: JSON.stringify(request)
75
+ });
76
+ }
77
+ async getHistory(id, opts) {
78
+ const params = new URLSearchParams();
79
+ if (opts?.limit != null) params.set("limit", String(opts.limit));
80
+ if (opts?.offset != null) params.set("offset", String(opts.offset));
81
+ if (opts?.status) params.set("status", opts.status);
82
+ const qs = params.toString();
83
+ return this.request(
84
+ `/${encodeURIComponent(id)}/history${qs ? `?${qs}` : ""}`
85
+ );
86
+ }
87
+ async getExecution(jobId, execId) {
88
+ return this.request(
89
+ `/${encodeURIComponent(jobId)}/history/${encodeURIComponent(execId)}`
90
+ );
91
+ }
92
+ };
93
+
94
+ // src/lib/processors/cli-jobs-command-processor.ts
95
+ function getServices(context, args) {
96
+ const manager = context.services.get("cli-server-manager");
97
+ if (!manager || !manager.connections) {
98
+ return [];
99
+ }
100
+ const targetServer = args["server"];
101
+ const results = [];
102
+ for (const [name, connection] of manager.connections) {
103
+ if (targetServer && name !== targetServer) continue;
104
+ if (!connection.connected) continue;
105
+ const config = connection.config;
106
+ const baseUrl = config.url.endsWith("/") ? config.url.slice(0, -1) : config.url;
107
+ const headers = config.headers ?? {};
108
+ results.push({ name, service: new CliJobsService(baseUrl, headers) });
109
+ }
110
+ return results;
111
+ }
112
+ async function resolveJobId(service, nameOrId) {
113
+ if (/^[0-9a-f-]{8,}$/i.test(nameOrId)) {
114
+ try {
115
+ return await service.getJob(nameOrId);
116
+ } catch {
117
+ }
118
+ }
119
+ const jobs = await service.listJobs();
120
+ const match = jobs.find(
121
+ (j) => j.name.toLowerCase() === nameOrId.toLowerCase() || j.id === nameOrId
122
+ );
123
+ return match ?? null;
124
+ }
125
+ function formatRelativeTime(isoString) {
126
+ if (!isoString) return "-";
127
+ const diff = Date.now() - new Date(isoString).getTime();
128
+ if (diff < 0) {
129
+ const absDiff = -diff;
130
+ if (absDiff < 6e4) return `in ${Math.round(absDiff / 1e3)}s`;
131
+ if (absDiff < 36e5) return `in ${Math.round(absDiff / 6e4)}m`;
132
+ if (absDiff < 864e5) return `in ${Math.round(absDiff / 36e5)}h`;
133
+ return `in ${Math.round(absDiff / 864e5)}d`;
134
+ }
135
+ if (diff < 6e4) return `${Math.round(diff / 1e3)}s ago`;
136
+ if (diff < 36e5) return `${Math.round(diff / 6e4)}m ago`;
137
+ if (diff < 864e5) return `${Math.round(diff / 36e5)}h ago`;
138
+ return `${Math.round(diff / 864e5)}d ago`;
139
+ }
140
+ function formatDuration(ms) {
141
+ if (ms == null) return "-";
142
+ if (ms < 1e3) return `${ms}ms`;
143
+ if (ms < 6e4) return `${(ms / 1e3).toFixed(1)}s`;
144
+ return `${(ms / 6e4).toFixed(1)}m`;
145
+ }
146
+ function statusColor(status, writer) {
147
+ switch (status) {
148
+ case "active":
149
+ case "completed":
150
+ case "running":
151
+ return writer.wrapInColor(status, cliCore.CliForegroundColor.Green);
152
+ case "paused":
153
+ return writer.wrapInColor(status, cliCore.CliForegroundColor.Yellow);
154
+ case "stopped":
155
+ case "failed":
156
+ case "timed_out":
157
+ case "cancelled":
158
+ return writer.wrapInColor(status, cliCore.CliForegroundColor.Red);
159
+ default:
160
+ return status;
161
+ }
162
+ }
163
+ function logLevelColor(level, writer) {
164
+ switch (level) {
165
+ case "debug":
166
+ return writer.wrapInColor(level, cliCore.CliForegroundColor.Cyan);
167
+ case "info":
168
+ return writer.wrapInColor(level, cliCore.CliForegroundColor.Green);
169
+ case "warning":
170
+ return writer.wrapInColor(level, cliCore.CliForegroundColor.Yellow);
171
+ case "error":
172
+ return writer.wrapInColor(level, cliCore.CliForegroundColor.Red);
173
+ default:
174
+ return level;
175
+ }
176
+ }
177
+ var serverParam = {
178
+ name: "server",
179
+ description: "Target a specific server by name",
180
+ required: false,
181
+ type: "string"
182
+ };
183
+ var CliJobsCommandProcessor = class {
184
+ constructor() {
185
+ this.command = "server";
186
+ this.extendsProcessor = true;
187
+ this.description = "Manage background jobs on connected servers";
188
+ this.author = cliCore.DefaultLibraryAuthor;
189
+ this.metadata = {
190
+ icon: cliCore.CliIcon.Gear,
191
+ module: "@qodalis/cli-server-jobs"
192
+ };
193
+ this.processors = [
194
+ {
195
+ command: "jobs",
196
+ description: "Manage background jobs on connected servers",
197
+ author: cliCore.DefaultLibraryAuthor,
198
+ metadata: {
199
+ icon: cliCore.CliIcon.Gear,
200
+ module: "@qodalis/cli-server-jobs"
201
+ },
202
+ parameters: [serverParam],
203
+ processors: [
204
+ // list
205
+ {
206
+ command: "list",
207
+ description: "List all jobs across connected servers",
208
+ aliases: ["ls"],
209
+ parameters: [
210
+ serverParam,
211
+ {
212
+ name: "group",
213
+ description: "Filter jobs by group",
214
+ required: false,
215
+ type: "string"
216
+ }
217
+ ],
218
+ processCommand: async (cmd, context) => {
219
+ await this.handleList(cmd, context);
220
+ }
221
+ },
222
+ // info
223
+ {
224
+ command: "info",
225
+ description: "Show detailed information about a job",
226
+ valueRequired: true,
227
+ parameters: [serverParam],
228
+ processCommand: async (cmd, context) => {
229
+ await this.handleInfo(cmd, context);
230
+ }
231
+ },
232
+ // trigger
233
+ {
234
+ command: "trigger",
235
+ description: "Trigger immediate execution of a job",
236
+ valueRequired: true,
237
+ parameters: [serverParam],
238
+ processCommand: async (cmd, context) => {
239
+ await this.handleAction(cmd, context, "trigger");
240
+ }
241
+ },
242
+ // pause
243
+ {
244
+ command: "pause",
245
+ description: "Pause scheduled execution of a job",
246
+ valueRequired: true,
247
+ parameters: [serverParam],
248
+ processCommand: async (cmd, context) => {
249
+ await this.handleAction(cmd, context, "pause");
250
+ }
251
+ },
252
+ // resume
253
+ {
254
+ command: "resume",
255
+ description: "Resume a paused job",
256
+ valueRequired: true,
257
+ parameters: [serverParam],
258
+ processCommand: async (cmd, context) => {
259
+ await this.handleAction(cmd, context, "resume");
260
+ }
261
+ },
262
+ // stop
263
+ {
264
+ command: "stop",
265
+ description: "Stop a job and cancel current execution if running",
266
+ valueRequired: true,
267
+ parameters: [serverParam],
268
+ processCommand: async (cmd, context) => {
269
+ await this.handleAction(cmd, context, "stop");
270
+ }
271
+ },
272
+ // cancel
273
+ {
274
+ command: "cancel",
275
+ description: "Cancel the current execution of a job",
276
+ valueRequired: true,
277
+ parameters: [serverParam],
278
+ processCommand: async (cmd, context) => {
279
+ await this.handleAction(cmd, context, "cancel");
280
+ }
281
+ },
282
+ // history
283
+ {
284
+ command: "history",
285
+ description: "Show execution history for a job",
286
+ valueRequired: true,
287
+ parameters: [
288
+ serverParam,
289
+ {
290
+ name: "limit",
291
+ description: "Number of entries to show (default 20)",
292
+ required: false,
293
+ type: "number",
294
+ defaultValue: 20
295
+ },
296
+ {
297
+ name: "offset",
298
+ description: "Offset for pagination",
299
+ required: false,
300
+ type: "number",
301
+ defaultValue: 0
302
+ },
303
+ {
304
+ name: "status",
305
+ description: "Filter by execution status",
306
+ required: false,
307
+ type: "string"
308
+ }
309
+ ],
310
+ processCommand: async (cmd, context) => {
311
+ await this.handleHistory(cmd, context);
312
+ }
313
+ },
314
+ // logs
315
+ {
316
+ command: "logs",
317
+ description: "Show logs from the latest or a specific execution",
318
+ valueRequired: true,
319
+ parameters: [
320
+ serverParam,
321
+ {
322
+ name: "exec",
323
+ description: "Specific execution ID to show logs for",
324
+ required: false,
325
+ type: "string"
326
+ }
327
+ ],
328
+ processCommand: async (cmd, context) => {
329
+ await this.handleLogs(cmd, context);
330
+ }
331
+ },
332
+ // edit
333
+ {
334
+ command: "edit",
335
+ description: "Interactively edit job settings",
336
+ valueRequired: true,
337
+ parameters: [serverParam],
338
+ processCommand: async (cmd, context) => {
339
+ await this.handleEdit(cmd, context);
340
+ }
341
+ },
342
+ // watch
343
+ {
344
+ command: "watch",
345
+ description: "Live view of job events via WebSocket",
346
+ parameters: [serverParam],
347
+ processCommand: async (cmd, context) => {
348
+ await this.handleWatch(cmd, context);
349
+ }
350
+ }
351
+ ],
352
+ processCommand: async (cmd, context) => {
353
+ await this.handleList(cmd, context);
354
+ }
355
+ }
356
+ ];
357
+ }
358
+ async processCommand(_cmd, _context) {
359
+ }
360
+ // ── List ──────────────────────────────────────────────────────────
361
+ async handleList(cmd, context) {
362
+ const services = getServices(context, cmd.args);
363
+ if (services.length === 0) {
364
+ context.writer.writeInfo(
365
+ 'No connected servers. Use "server list" to check connections.'
366
+ );
367
+ return;
368
+ }
369
+ const groupFilter = cmd.args["group"];
370
+ const multiServer = services.length > 1;
371
+ for (const { name, service } of services) {
372
+ try {
373
+ let jobs = await service.listJobs();
374
+ if (groupFilter) {
375
+ jobs = jobs.filter(
376
+ (j) => j.group?.toLowerCase() === groupFilter.toLowerCase()
377
+ );
378
+ }
379
+ if (multiServer) {
380
+ context.writer.writeln(
381
+ context.writer.wrapInColor(
382
+ `Server: ${name}`,
383
+ cliCore.CliForegroundColor.Cyan
384
+ )
385
+ );
386
+ }
387
+ if (jobs.length === 0) {
388
+ context.writer.writeInfo(" No jobs found.");
389
+ continue;
390
+ }
391
+ const headers = [
392
+ "Name",
393
+ "Group",
394
+ "Status",
395
+ "Schedule",
396
+ "Last Run",
397
+ "Next Run",
398
+ "Duration"
399
+ ];
400
+ const rows = jobs.map((j) => [
401
+ j.name,
402
+ j.group ?? "-",
403
+ statusColor(j.status, context.writer),
404
+ j.schedule ?? (j.interval ? `every ${j.interval}ms` : "-"),
405
+ formatRelativeTime(j.lastRunAt),
406
+ formatRelativeTime(j.nextRunAt),
407
+ formatDuration(j.lastRunDuration)
408
+ ]);
409
+ context.writer.writeTable(headers, rows);
410
+ } catch (e) {
411
+ context.writer.writeError(
412
+ `Failed to list jobs on ${name}: ${e.message}`
413
+ );
414
+ }
415
+ }
416
+ }
417
+ // ── Info ──────────────────────────────────────────────────────────
418
+ async handleInfo(cmd, context) {
419
+ const nameOrId = (cmd.value ?? "").trim();
420
+ if (!nameOrId) {
421
+ context.writer.writeError("Usage: jobs info <id|name>");
422
+ return;
423
+ }
424
+ const services = getServices(context, cmd.args);
425
+ if (services.length === 0) {
426
+ context.writer.writeInfo("No connected servers.");
427
+ return;
428
+ }
429
+ for (const { name, service } of services) {
430
+ try {
431
+ const job = await resolveJobId(service, nameOrId);
432
+ if (!job) {
433
+ if (services.length === 1) {
434
+ context.writer.writeError(
435
+ `Job "${nameOrId}" not found on server ${name}.`
436
+ );
437
+ }
438
+ continue;
439
+ }
440
+ if (services.length > 1) {
441
+ context.writer.writeln(
442
+ context.writer.wrapInColor(
443
+ `Server: ${name}`,
444
+ cliCore.CliForegroundColor.Cyan
445
+ )
446
+ );
447
+ }
448
+ const lines = [
449
+ ["ID", job.id],
450
+ ["Name", job.name],
451
+ ["Description", job.description],
452
+ ["Group", job.group ?? "-"],
453
+ ["Status", statusColor(job.status, context.writer)],
454
+ [
455
+ "Schedule",
456
+ job.schedule ?? (job.interval ? `every ${job.interval}ms` : "-")
457
+ ],
458
+ ["Max Retries", String(job.maxRetries)],
459
+ [
460
+ "Timeout",
461
+ job.timeout ? `${job.timeout}ms` : "none"
462
+ ],
463
+ ["Overlap Policy", job.overlapPolicy],
464
+ [
465
+ "Current Execution",
466
+ job.currentExecutionId ?? "none"
467
+ ],
468
+ ["Next Run", formatRelativeTime(job.nextRunAt)],
469
+ ["Last Run", formatRelativeTime(job.lastRunAt)],
470
+ [
471
+ "Last Run Status",
472
+ job.lastRunStatus ? statusColor(job.lastRunStatus, context.writer) : "-"
473
+ ],
474
+ ["Last Run Duration", formatDuration(job.lastRunDuration)]
475
+ ];
476
+ for (const [label, value] of lines) {
477
+ context.writer.writeln(
478
+ ` ${context.writer.wrapInColor(
479
+ label.padEnd(20),
480
+ cliCore.CliForegroundColor.Yellow
481
+ )} ${value}`
482
+ );
483
+ }
484
+ return;
485
+ } catch (e) {
486
+ context.writer.writeError(
487
+ `Error on server ${name}: ${e.message}`
488
+ );
489
+ }
490
+ }
491
+ }
492
+ // ── Action (trigger/pause/resume/stop/cancel) ────────────────────
493
+ async handleAction(cmd, context, action) {
494
+ const nameOrId = (cmd.value ?? "").trim();
495
+ if (!nameOrId) {
496
+ context.writer.writeError(`Usage: jobs ${action} <id|name>`);
497
+ return;
498
+ }
499
+ const services = getServices(context, cmd.args);
500
+ if (services.length === 0) {
501
+ context.writer.writeInfo("No connected servers.");
502
+ return;
503
+ }
504
+ for (const { name, service } of services) {
505
+ try {
506
+ const job = await resolveJobId(service, nameOrId);
507
+ if (!job) {
508
+ if (services.length === 1) {
509
+ context.writer.writeError(
510
+ `Job "${nameOrId}" not found on server ${name}.`
511
+ );
512
+ }
513
+ continue;
514
+ }
515
+ switch (action) {
516
+ case "trigger":
517
+ await service.triggerJob(job.id);
518
+ break;
519
+ case "pause":
520
+ await service.pauseJob(job.id);
521
+ break;
522
+ case "resume":
523
+ await service.resumeJob(job.id);
524
+ break;
525
+ case "stop":
526
+ await service.stopJob(job.id);
527
+ break;
528
+ case "cancel":
529
+ await service.cancelJob(job.id);
530
+ break;
531
+ }
532
+ const serverSuffix = services.length > 1 ? ` on server ${name}` : "";
533
+ context.writer.writeSuccess(
534
+ `Job "${job.name}" ${action}${action.endsWith("e") ? "d" : "ed"}${serverSuffix}.`
535
+ );
536
+ return;
537
+ } catch (e) {
538
+ context.writer.writeError(
539
+ `Failed to ${action} job on ${name}: ${e.message}`
540
+ );
541
+ }
542
+ }
543
+ }
544
+ // ── History ──────────────────────────────────────────────────────
545
+ async handleHistory(cmd, context) {
546
+ const nameOrId = (cmd.value ?? "").trim();
547
+ if (!nameOrId) {
548
+ context.writer.writeError("Usage: jobs history <id|name>");
549
+ return;
550
+ }
551
+ const services = getServices(context, cmd.args);
552
+ if (services.length === 0) {
553
+ context.writer.writeInfo("No connected servers.");
554
+ return;
555
+ }
556
+ const limit = cmd.args["limit"] ?? 20;
557
+ const offset = cmd.args["offset"] ?? 0;
558
+ const status = cmd.args["status"];
559
+ for (const { name, service } of services) {
560
+ try {
561
+ const job = await resolveJobId(service, nameOrId);
562
+ if (!job) {
563
+ if (services.length === 1) {
564
+ context.writer.writeError(
565
+ `Job "${nameOrId}" not found on server ${name}.`
566
+ );
567
+ }
568
+ continue;
569
+ }
570
+ if (services.length > 1) {
571
+ context.writer.writeln(
572
+ context.writer.wrapInColor(
573
+ `Server: ${name}`,
574
+ cliCore.CliForegroundColor.Cyan
575
+ )
576
+ );
577
+ }
578
+ const history = await service.getHistory(job.id, {
579
+ limit,
580
+ offset,
581
+ status
582
+ });
583
+ if (history.items.length === 0) {
584
+ context.writer.writeInfo(
585
+ `No execution history for "${job.name}".`
586
+ );
587
+ return;
588
+ }
589
+ context.writer.writeln(
590
+ `Showing ${history.items.length} of ${history.total} executions for "${job.name}":`
591
+ );
592
+ const headers = [
593
+ "ID",
594
+ "Status",
595
+ "Started",
596
+ "Duration",
597
+ "Retry",
598
+ "Error"
599
+ ];
600
+ const rows = history.items.map((exec) => [
601
+ exec.id.substring(0, 8),
602
+ statusColor(exec.status, context.writer),
603
+ formatRelativeTime(exec.startedAt),
604
+ formatDuration(exec.duration),
605
+ String(exec.retryAttempt),
606
+ exec.error ? exec.error.substring(0, 40) : "-"
607
+ ]);
608
+ context.writer.writeTable(headers, rows);
609
+ return;
610
+ } catch (e) {
611
+ context.writer.writeError(
612
+ `Error on server ${name}: ${e.message}`
613
+ );
614
+ }
615
+ }
616
+ }
617
+ // ── Logs ─────────────────────────────────────────────────────────
618
+ async handleLogs(cmd, context) {
619
+ const nameOrId = (cmd.value ?? "").trim();
620
+ if (!nameOrId) {
621
+ context.writer.writeError("Usage: jobs logs <id|name>");
622
+ return;
623
+ }
624
+ const services = getServices(context, cmd.args);
625
+ if (services.length === 0) {
626
+ context.writer.writeInfo("No connected servers.");
627
+ return;
628
+ }
629
+ const execId = cmd.args["exec"];
630
+ for (const { name, service } of services) {
631
+ try {
632
+ const job = await resolveJobId(service, nameOrId);
633
+ if (!job) {
634
+ if (services.length === 1) {
635
+ context.writer.writeError(
636
+ `Job "${nameOrId}" not found on server ${name}.`
637
+ );
638
+ }
639
+ continue;
640
+ }
641
+ let execution;
642
+ if (execId) {
643
+ execution = await service.getExecution(job.id, execId);
644
+ } else {
645
+ const history = await service.getHistory(job.id, {
646
+ limit: 1
647
+ });
648
+ if (history.items.length === 0) {
649
+ context.writer.writeInfo(
650
+ `No executions found for "${job.name}".`
651
+ );
652
+ return;
653
+ }
654
+ execution = await service.getExecution(
655
+ job.id,
656
+ history.items[0].id
657
+ );
658
+ }
659
+ if (services.length > 1) {
660
+ context.writer.writeln(
661
+ context.writer.wrapInColor(
662
+ `Server: ${name}`,
663
+ cliCore.CliForegroundColor.Cyan
664
+ )
665
+ );
666
+ }
667
+ context.writer.writeln(
668
+ `Logs for "${job.name}" execution ${execution.id.substring(0, 8)} (${statusColor(execution.status, context.writer)}):`
669
+ );
670
+ if (!execution.logs || execution.logs.length === 0) {
671
+ context.writer.writeInfo(" No log entries.");
672
+ return;
673
+ }
674
+ for (const entry of execution.logs) {
675
+ const ts = new Date(entry.timestamp).toLocaleTimeString();
676
+ const level = logLevelColor(
677
+ entry.level,
678
+ context.writer
679
+ ).padEnd(12);
680
+ context.writer.writeln(
681
+ ` ${context.writer.wrapInColor(ts, cliCore.CliForegroundColor.White)} ${level} ${entry.message}`
682
+ );
683
+ }
684
+ return;
685
+ } catch (e) {
686
+ context.writer.writeError(
687
+ `Error on server ${name}: ${e.message}`
688
+ );
689
+ }
690
+ }
691
+ }
692
+ // ── Edit ─────────────────────────────────────────────────────────
693
+ async handleEdit(cmd, context) {
694
+ const nameOrId = (cmd.value ?? "").trim();
695
+ if (!nameOrId) {
696
+ context.writer.writeError("Usage: jobs edit <id|name>");
697
+ return;
698
+ }
699
+ const services = getServices(context, cmd.args);
700
+ if (services.length === 0) {
701
+ context.writer.writeInfo("No connected servers.");
702
+ return;
703
+ }
704
+ for (const { name, service } of services) {
705
+ try {
706
+ const job = await resolveJobId(service, nameOrId);
707
+ if (!job) {
708
+ if (services.length === 1) {
709
+ context.writer.writeError(
710
+ `Job "${nameOrId}" not found on server ${name}.`
711
+ );
712
+ }
713
+ continue;
714
+ }
715
+ context.writer.writeln(
716
+ `Editing job "${context.writer.wrapInColor(job.name, cliCore.CliForegroundColor.Cyan)}" on server ${name}`
717
+ );
718
+ context.writer.writeln(
719
+ "Press Enter to keep current value, or type a new value."
720
+ );
721
+ context.writer.writeln("");
722
+ const update = {};
723
+ const newDesc = await context.reader.readLine(
724
+ `Description [${job.description}]: `
725
+ );
726
+ if (newDesc == null) {
727
+ context.writer.writeInfo("Edit cancelled.");
728
+ return;
729
+ }
730
+ if (newDesc.trim()) {
731
+ update.description = newDesc.trim();
732
+ }
733
+ const newGroup = await context.reader.readLine(
734
+ `Group [${job.group ?? "none"}]: `
735
+ );
736
+ if (newGroup == null) {
737
+ context.writer.writeInfo("Edit cancelled.");
738
+ return;
739
+ }
740
+ if (newGroup.trim()) {
741
+ update.group = newGroup.trim();
742
+ }
743
+ const currentSchedule = job.schedule ?? (job.interval ? `interval: ${job.interval}ms` : "none");
744
+ const newSchedule = await context.reader.readLine(
745
+ `Cron schedule [${currentSchedule}]: `
746
+ );
747
+ if (newSchedule == null) {
748
+ context.writer.writeInfo("Edit cancelled.");
749
+ return;
750
+ }
751
+ if (newSchedule.trim()) {
752
+ update.schedule = newSchedule.trim();
753
+ }
754
+ if (!newSchedule.trim()) {
755
+ const newInterval = await context.reader.readLine(
756
+ `Interval (e.g. 30s, 5m) [${job.interval ? `${job.interval}ms` : "none"}]: `
757
+ );
758
+ if (newInterval != null && newInterval.trim()) {
759
+ update.interval = newInterval.trim();
760
+ }
761
+ }
762
+ const newRetries = await context.reader.readLine(
763
+ `Max retries [${job.maxRetries}]: `
764
+ );
765
+ if (newRetries != null && newRetries.trim()) {
766
+ const parsed = parseInt(newRetries.trim(), 10);
767
+ if (!isNaN(parsed)) {
768
+ update.maxRetries = parsed;
769
+ }
770
+ }
771
+ const newTimeout = await context.reader.readLine(
772
+ `Timeout (e.g. 5m, 1h) [${job.timeout ? `${job.timeout}ms` : "none"}]: `
773
+ );
774
+ if (newTimeout != null && newTimeout.trim()) {
775
+ update.timeout = newTimeout.trim();
776
+ }
777
+ const newOverlap = await context.reader.readLine(
778
+ `Overlap policy (skip/queue/cancel) [${job.overlapPolicy}]: `
779
+ );
780
+ if (newOverlap != null && newOverlap.trim() && ["skip", "queue", "cancel"].includes(
781
+ newOverlap.trim().toLowerCase()
782
+ )) {
783
+ update.overlapPolicy = newOverlap.trim().toLowerCase();
784
+ }
785
+ if (Object.keys(update).length === 0) {
786
+ context.writer.writeInfo("No changes made.");
787
+ return;
788
+ }
789
+ const confirm = await context.reader.readConfirm(
790
+ "Apply changes?"
791
+ );
792
+ if (!confirm) {
793
+ context.writer.writeInfo("Edit cancelled.");
794
+ return;
795
+ }
796
+ await service.updateJob(job.id, update);
797
+ context.writer.writeSuccess(
798
+ `Job "${job.name}" updated successfully.`
799
+ );
800
+ return;
801
+ } catch (e) {
802
+ context.writer.writeError(
803
+ `Error on server ${name}: ${e.message}`
804
+ );
805
+ }
806
+ }
807
+ }
808
+ // ── Watch ────────────────────────────────────────────────────────
809
+ async handleWatch(cmd, context) {
810
+ const manager = context.services.get("cli-server-manager");
811
+ if (!manager || !manager.connections) {
812
+ context.writer.writeInfo("No connected servers.");
813
+ return;
814
+ }
815
+ const targetServer = cmd.args["server"];
816
+ context.writer.writeln(
817
+ context.writer.wrapInColor(
818
+ "Watching job events... (press Ctrl+C to stop)",
819
+ cliCore.CliForegroundColor.Cyan
820
+ )
821
+ );
822
+ context.writer.writeln("");
823
+ const sockets = [];
824
+ const abortHandler = () => {
825
+ for (const ws of sockets) {
826
+ try {
827
+ ws.close();
828
+ } catch {
829
+ }
830
+ }
831
+ };
832
+ context.onAbort.subscribe(abortHandler);
833
+ for (const [serverName, connection] of manager.connections) {
834
+ if (targetServer && serverName !== targetServer) continue;
835
+ if (!connection.connected) continue;
836
+ const config = connection.config;
837
+ const baseUrl = config.url.endsWith("/") ? config.url.slice(0, -1) : config.url;
838
+ const wsUrl = baseUrl.replace(/^https:/, "wss:").replace(/^http:/, "ws:") + "/ws/v1/qcli/events";
839
+ try {
840
+ const ws = new WebSocket(wsUrl);
841
+ sockets.push(ws);
842
+ ws.onmessage = (event) => {
843
+ try {
844
+ const data = JSON.parse(event.data);
845
+ if (!data.type || !data.type.startsWith("job:")) {
846
+ return;
847
+ }
848
+ const ts = (/* @__PURE__ */ new Date()).toLocaleTimeString();
849
+ const serverPrefix = manager.connections.size > 1 ? context.writer.wrapInColor(
850
+ `[${serverName}] `,
851
+ cliCore.CliForegroundColor.Cyan
852
+ ) : "";
853
+ let detail = "";
854
+ if (data.jobId) {
855
+ detail += ` jobId=${data.jobId}`;
856
+ }
857
+ if (data.executionId) {
858
+ detail += ` execId=${data.executionId.substring(0, 8)}`;
859
+ }
860
+ if (data.duration != null) {
861
+ detail += ` duration=${formatDuration(data.duration)}`;
862
+ }
863
+ if (data.error) {
864
+ detail += ` error="${data.error}"`;
865
+ }
866
+ const eventType = statusColor(
867
+ data.type.replace("job:", ""),
868
+ context.writer
869
+ );
870
+ context.writer.writeln(
871
+ `${context.writer.wrapInColor(ts, cliCore.CliForegroundColor.White)} ${serverPrefix}${eventType}${detail}`
872
+ );
873
+ } catch {
874
+ }
875
+ };
876
+ ws.onerror = () => {
877
+ context.writer.writeError(
878
+ `WebSocket error on server ${serverName}`
879
+ );
880
+ };
881
+ ws.onclose = () => {
882
+ };
883
+ } catch (e) {
884
+ context.writer.writeError(
885
+ `Failed to connect to ${serverName}: ${e.message}`
886
+ );
887
+ }
888
+ }
889
+ await new Promise((resolve) => {
890
+ const sub = context.onAbort.subscribe(() => {
891
+ sub.unsubscribe();
892
+ resolve();
893
+ });
894
+ if (context.signal) {
895
+ context.signal.addEventListener("abort", () => {
896
+ sub.unsubscribe();
897
+ resolve();
898
+ });
899
+ }
900
+ });
901
+ context.writer.writeln("");
902
+ context.writer.writeInfo("Stopped watching job events.");
903
+ }
904
+ };
905
+
906
+ // src/lib/completion/cli-job-name-completion-provider.ts
907
+ var JOB_NAME_SUBCOMMANDS = /* @__PURE__ */ new Set([
908
+ "info",
909
+ "trigger",
910
+ "pause",
911
+ "resume",
912
+ "stop",
913
+ "cancel",
914
+ "history",
915
+ "logs",
916
+ "edit"
917
+ ]);
918
+ var CliJobNameCompletionProvider = class {
919
+ constructor() {
920
+ this.priority = 50;
921
+ this.services = null;
922
+ }
923
+ setServices(services) {
924
+ this.services = services;
925
+ }
926
+ async getCompletions(context) {
927
+ const { tokens, tokenIndex, token } = context;
928
+ if (tokenIndex !== 3 || tokens.length < 3) {
929
+ return [];
930
+ }
931
+ if (tokens[0].toLowerCase() !== "server") {
932
+ return [];
933
+ }
934
+ if (tokens[1].toLowerCase() !== "jobs") {
935
+ return [];
936
+ }
937
+ const subCommand = tokens[2].toLowerCase();
938
+ if (!JOB_NAME_SUBCOMMANDS.has(subCommand)) {
939
+ return [];
940
+ }
941
+ if (!this.services) {
942
+ return [];
943
+ }
944
+ const lowerPrefix = token.toLowerCase();
945
+ try {
946
+ const manager = this.services.get("cli-server-manager");
947
+ if (!manager?.connections) {
948
+ return [];
949
+ }
950
+ const names = [];
951
+ for (const [, connection] of manager.connections) {
952
+ if (!connection.connected) continue;
953
+ const config = connection.config;
954
+ const baseUrl = config.url.endsWith("/") ? config.url.slice(0, -1) : config.url;
955
+ const headers = config.headers ?? {};
956
+ const service = new CliJobsService(baseUrl, headers);
957
+ try {
958
+ const jobs = await service.listJobs();
959
+ for (const job of jobs) {
960
+ if (!names.includes(job.name)) {
961
+ names.push(job.name);
962
+ }
963
+ }
964
+ } catch {
965
+ }
966
+ }
967
+ return names.filter((name) => name.toLowerCase().startsWith(lowerPrefix)).sort();
968
+ } catch {
969
+ return [];
970
+ }
971
+ }
972
+ };
973
+
974
+ // src/lib/version.ts
975
+ var LIBRARY_VERSION = "2.0.0-beta.3";
976
+ var API_VERSION = 2;
977
+ var completionProvider = new CliJobNameCompletionProvider();
978
+ var jobsModule = {
979
+ apiVersion: API_VERSION,
980
+ name: "@qodalis/cli-server-jobs",
981
+ processors: [new CliJobsCommandProcessor()],
982
+ services: [
983
+ {
984
+ provide: cliCore.ICliCompletionProvider_TOKEN,
985
+ useValue: completionProvider,
986
+ multi: true
987
+ }
988
+ ],
989
+ async onInit(context) {
990
+ completionProvider.setServices(context.services);
991
+ }
992
+ };
993
+
994
+ exports.API_VERSION = API_VERSION;
995
+ exports.CliJobNameCompletionProvider = CliJobNameCompletionProvider;
996
+ exports.CliJobsCommandProcessor = CliJobsCommandProcessor;
997
+ exports.CliJobsService = CliJobsService;
998
+ exports.LIBRARY_VERSION = LIBRARY_VERSION;
999
+ exports.jobsModule = jobsModule;
1000
+ //# sourceMappingURL=public-api.js.map
1001
+ //# sourceMappingURL=public-api.js.map