crewly 1.4.63 → 1.4.64
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/dist/backend/backend/src/controllers/scheduler/unified-scheduler.controller.d.ts +91 -0
- package/dist/backend/backend/src/controllers/scheduler/unified-scheduler.controller.d.ts.map +1 -0
- package/dist/backend/backend/src/controllers/scheduler/unified-scheduler.controller.js +290 -0
- package/dist/backend/backend/src/controllers/scheduler/unified-scheduler.controller.js.map +1 -0
- package/dist/backend/backend/src/routes/api.routes.d.ts.map +1 -1
- package/dist/backend/backend/src/routes/api.routes.js +3 -0
- package/dist/backend/backend/src/routes/api.routes.js.map +1 -1
- package/dist/backend/backend/src/routes/modules/unified-scheduler.routes.d.ts +27 -0
- package/dist/backend/backend/src/routes/modules/unified-scheduler.routes.d.ts.map +1 -0
- package/dist/backend/backend/src/routes/modules/unified-scheduler.routes.js +40 -0
- package/dist/backend/backend/src/routes/modules/unified-scheduler.routes.js.map +1 -0
- package/dist/backend/backend/src/services/index.d.ts +1 -0
- package/dist/backend/backend/src/services/index.d.ts.map +1 -1
- package/dist/backend/backend/src/services/index.js +1 -0
- package/dist/backend/backend/src/services/index.js.map +1 -1
- package/dist/backend/backend/src/services/workflow/unified-scheduler.service.d.ts +234 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.service.d.ts.map +1 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.service.js +636 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.service.js.map +1 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.types.d.ts +156 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.types.d.ts.map +1 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.types.js +81 -0
- package/dist/backend/backend/src/services/workflow/unified-scheduler.types.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified Scheduler Service
|
|
3
|
+
*
|
|
4
|
+
* Consolidates cron, interval, and idle-based scheduling into a single
|
|
5
|
+
* persistent service with agent auto-start capabilities.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Cron schedules: standard 5-field expressions
|
|
9
|
+
* - Interval schedules: fixed-interval triggers
|
|
10
|
+
* - Idle schedules: trigger when agent has been idle for N minutes
|
|
11
|
+
* - Persistent storage in ~/.crewly/unified-schedules.json
|
|
12
|
+
* - Auto-start agents that are offline when triggered
|
|
13
|
+
* - Auto-pause after maxConsecutiveTriggers without agent response
|
|
14
|
+
* - Manual trigger, pause, and resume
|
|
15
|
+
*
|
|
16
|
+
* Runs a 60-second evaluation loop that checks all active schedules
|
|
17
|
+
* and triggers those that are due.
|
|
18
|
+
*
|
|
19
|
+
* @module services/workflow/unified-scheduler.service
|
|
20
|
+
*/
|
|
21
|
+
import { EventEmitter } from 'events';
|
|
22
|
+
import type { UnifiedSchedule, CreateScheduleRequest, UpdateScheduleRequest, SchedulerHealth } from './unified-scheduler.types.js';
|
|
23
|
+
/**
|
|
24
|
+
* UnifiedSchedulerService manages all schedule types (cron, interval, idle)
|
|
25
|
+
* in a single persistent store with a 60-second evaluation loop.
|
|
26
|
+
*
|
|
27
|
+
* Uses sendMessageToAgent (via callback) for message delivery and
|
|
28
|
+
* supports auto-starting offline agents.
|
|
29
|
+
*/
|
|
30
|
+
export declare class UnifiedSchedulerService extends EventEmitter {
|
|
31
|
+
private static instance;
|
|
32
|
+
private readonly logger;
|
|
33
|
+
private readonly storagePath;
|
|
34
|
+
/** All schedules indexed by ID */
|
|
35
|
+
private schedules;
|
|
36
|
+
/** Evaluation loop timer */
|
|
37
|
+
private evalTimer;
|
|
38
|
+
/** Whether the eval loop is running */
|
|
39
|
+
private running;
|
|
40
|
+
/** Timestamp of last evaluation cycle */
|
|
41
|
+
private lastEvaluatedAt;
|
|
42
|
+
/** Total triggers since service start */
|
|
43
|
+
private totalTriggersSinceStart;
|
|
44
|
+
/**
|
|
45
|
+
* Callback for sending messages to agents.
|
|
46
|
+
* Set via setMessageCallback() — decouples from AgentRegistrationService.
|
|
47
|
+
*/
|
|
48
|
+
private messageCallback;
|
|
49
|
+
/**
|
|
50
|
+
* Callback for checking if an agent is online.
|
|
51
|
+
* Set via setAgentStatusCallback().
|
|
52
|
+
*/
|
|
53
|
+
private agentStatusCallback;
|
|
54
|
+
/**
|
|
55
|
+
* Callback for starting an offline agent.
|
|
56
|
+
* Set via setAgentStartCallback().
|
|
57
|
+
*/
|
|
58
|
+
private agentStartCallback;
|
|
59
|
+
/**
|
|
60
|
+
* Callback for checking agent idle status.
|
|
61
|
+
* Set via setIdleCheckCallback().
|
|
62
|
+
*/
|
|
63
|
+
private idleCheckCallback;
|
|
64
|
+
private constructor();
|
|
65
|
+
/**
|
|
66
|
+
* Get the singleton instance.
|
|
67
|
+
*
|
|
68
|
+
* @param crewlyHome - Override ~/.crewly path (for testing)
|
|
69
|
+
* @returns Service instance
|
|
70
|
+
*/
|
|
71
|
+
static getInstance(crewlyHome?: string): UnifiedSchedulerService;
|
|
72
|
+
/**
|
|
73
|
+
* Reset the singleton (for testing).
|
|
74
|
+
*/
|
|
75
|
+
static resetInstance(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Set the callback for sending messages to agents.
|
|
78
|
+
*
|
|
79
|
+
* @param callback - Function that sends a message to an agent by session name
|
|
80
|
+
*/
|
|
81
|
+
setMessageCallback(callback: (sessionName: string, message: string) => Promise<{
|
|
82
|
+
success: boolean;
|
|
83
|
+
}>): void;
|
|
84
|
+
/**
|
|
85
|
+
* Set the callback for checking if an agent is online.
|
|
86
|
+
*
|
|
87
|
+
* @param callback - Returns true if the agent is currently active
|
|
88
|
+
*/
|
|
89
|
+
setAgentStatusCallback(callback: (sessionName: string) => Promise<boolean>): void;
|
|
90
|
+
/**
|
|
91
|
+
* Set the callback for starting an offline agent.
|
|
92
|
+
*
|
|
93
|
+
* @param callback - Starts the agent and returns true if successful
|
|
94
|
+
*/
|
|
95
|
+
setAgentStartCallback(callback: (sessionName: string) => Promise<boolean>): void;
|
|
96
|
+
/**
|
|
97
|
+
* Set the callback for checking agent idle status.
|
|
98
|
+
*
|
|
99
|
+
* @param callback - Returns idle status and idle duration
|
|
100
|
+
*/
|
|
101
|
+
setIdleCheckCallback(callback: (sessionName: string) => Promise<{
|
|
102
|
+
idle: boolean;
|
|
103
|
+
idleMinutes: number;
|
|
104
|
+
}>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Start the scheduler evaluation loop.
|
|
107
|
+
*
|
|
108
|
+
* Loads persisted schedules from disk and begins the 60-second
|
|
109
|
+
* evaluation cycle.
|
|
110
|
+
*/
|
|
111
|
+
start(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Stop the scheduler evaluation loop.
|
|
114
|
+
*/
|
|
115
|
+
stop(): void;
|
|
116
|
+
/**
|
|
117
|
+
* Create a new schedule.
|
|
118
|
+
*
|
|
119
|
+
* @param request - Schedule creation request
|
|
120
|
+
* @returns Created schedule
|
|
121
|
+
* @throws Error if validation fails
|
|
122
|
+
*/
|
|
123
|
+
create(request: CreateScheduleRequest): UnifiedSchedule;
|
|
124
|
+
/**
|
|
125
|
+
* Get a schedule by ID.
|
|
126
|
+
*
|
|
127
|
+
* @param id - Schedule ID
|
|
128
|
+
* @returns Schedule or null
|
|
129
|
+
*/
|
|
130
|
+
get(id: string): UnifiedSchedule | null;
|
|
131
|
+
/**
|
|
132
|
+
* List all schedules, optionally filtered by target session.
|
|
133
|
+
*
|
|
134
|
+
* @param target - Optional session name filter
|
|
135
|
+
* @returns Array of schedules
|
|
136
|
+
*/
|
|
137
|
+
list(target?: string): UnifiedSchedule[];
|
|
138
|
+
/**
|
|
139
|
+
* Update an existing schedule.
|
|
140
|
+
*
|
|
141
|
+
* @param id - Schedule ID
|
|
142
|
+
* @param updates - Partial update fields
|
|
143
|
+
* @returns Updated schedule or null if not found
|
|
144
|
+
*/
|
|
145
|
+
update(id: string, updates: UpdateScheduleRequest): UnifiedSchedule | null;
|
|
146
|
+
/**
|
|
147
|
+
* Delete a schedule.
|
|
148
|
+
*
|
|
149
|
+
* @param id - Schedule ID
|
|
150
|
+
* @returns True if deleted
|
|
151
|
+
*/
|
|
152
|
+
delete(id: string): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Pause a schedule.
|
|
155
|
+
*
|
|
156
|
+
* @param id - Schedule ID
|
|
157
|
+
* @returns Updated schedule or null
|
|
158
|
+
*/
|
|
159
|
+
pause(id: string): UnifiedSchedule | null;
|
|
160
|
+
/**
|
|
161
|
+
* Resume a paused schedule.
|
|
162
|
+
*
|
|
163
|
+
* @param id - Schedule ID
|
|
164
|
+
* @returns Updated schedule or null
|
|
165
|
+
*/
|
|
166
|
+
resume(id: string): UnifiedSchedule | null;
|
|
167
|
+
/**
|
|
168
|
+
* Manually trigger a schedule now, regardless of timing.
|
|
169
|
+
*
|
|
170
|
+
* @param id - Schedule ID
|
|
171
|
+
* @returns True if triggered successfully
|
|
172
|
+
*/
|
|
173
|
+
triggerNow(id: string): Promise<boolean>;
|
|
174
|
+
/**
|
|
175
|
+
* Get scheduler health and statistics.
|
|
176
|
+
*
|
|
177
|
+
* @returns Health stats
|
|
178
|
+
*/
|
|
179
|
+
getHealth(): SchedulerHealth;
|
|
180
|
+
/**
|
|
181
|
+
* Evaluate all active schedules and trigger those that are due.
|
|
182
|
+
*
|
|
183
|
+
* Called every 60 seconds by the eval timer. Checks each schedule
|
|
184
|
+
* type against its timing criteria and executes if due.
|
|
185
|
+
*/
|
|
186
|
+
evaluate(): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Check if a schedule is due for triggering.
|
|
189
|
+
*
|
|
190
|
+
* @param schedule - Schedule to check
|
|
191
|
+
* @returns True if the schedule should trigger now
|
|
192
|
+
*/
|
|
193
|
+
private isScheduleDue;
|
|
194
|
+
/**
|
|
195
|
+
* Check if a cron schedule is due (within the current evaluation window).
|
|
196
|
+
*
|
|
197
|
+
* Compares the cron expression against the current time.
|
|
198
|
+
* Uses minute-level granularity (matches eval loop frequency).
|
|
199
|
+
*
|
|
200
|
+
* @param schedule - Cron schedule to check
|
|
201
|
+
* @returns True if the current minute matches the cron expression
|
|
202
|
+
*/
|
|
203
|
+
private isCronDue;
|
|
204
|
+
/**
|
|
205
|
+
* Execute a schedule: send message to agent, handle auto-start,
|
|
206
|
+
* track consecutive triggers, and auto-pause if limit reached.
|
|
207
|
+
*
|
|
208
|
+
* @param schedule - Schedule to execute
|
|
209
|
+
* @returns True if message was delivered successfully
|
|
210
|
+
*/
|
|
211
|
+
private executeSchedule;
|
|
212
|
+
/**
|
|
213
|
+
* Reset the consecutive trigger counter for a target agent.
|
|
214
|
+
*
|
|
215
|
+
* Called when an agent shows activity (e.g., sends a message, completes a task).
|
|
216
|
+
* This prevents auto-pausing schedules for responsive agents.
|
|
217
|
+
*
|
|
218
|
+
* @param target - Agent session name
|
|
219
|
+
*/
|
|
220
|
+
resetConsecutiveTriggers(target: string): void;
|
|
221
|
+
/**
|
|
222
|
+
* Save all schedules to disk.
|
|
223
|
+
*/
|
|
224
|
+
private persist;
|
|
225
|
+
/**
|
|
226
|
+
* Load schedules from disk.
|
|
227
|
+
*/
|
|
228
|
+
private loadFromDisk;
|
|
229
|
+
/**
|
|
230
|
+
* Get count of active schedules.
|
|
231
|
+
*/
|
|
232
|
+
private getActiveCount;
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=unified-scheduler.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unified-scheduler.service.d.ts","sourceRoot":"","sources":["../../../../../../backend/src/services/workflow/unified-scheduler.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAMtC,OAAO,KAAK,EACV,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EAErB,eAAe,EAEhB,MAAM,8BAA8B,CAAC;AAUtC;;;;;;GAMG;AACH,qBAAa,uB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