@memberjunction/scheduling-engine 4.0.0 → 4.1.0
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/README.md +158 -0
- package/package.json +11 -11
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# @memberjunction/scheduling-engine
|
|
2
|
+
|
|
3
|
+
Server-side execution engine for MemberJunction scheduled jobs. Extends `SchedulingEngineBase` with cron evaluation, plugin-based job execution, distributed locking, notification management, and polling lifecycle control.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
subgraph "@memberjunction/scheduling-engine"
|
|
10
|
+
A[SchedulingEngine] --> B[CronExpressionHelper]
|
|
11
|
+
A --> C[NotificationManager]
|
|
12
|
+
A --> D[Plugin System]
|
|
13
|
+
D --> E[BaseScheduledJob]
|
|
14
|
+
E --> F[AgentScheduledJobDriver]
|
|
15
|
+
E --> G[ActionScheduledJobDriver]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
subgraph "Execution Flow"
|
|
19
|
+
H[Polling Timer] --> I{Jobs Due?}
|
|
20
|
+
I -->|Yes| J[Acquire Lock]
|
|
21
|
+
J --> K[Execute Plugin]
|
|
22
|
+
K --> L[Record Run]
|
|
23
|
+
L --> M[Send Notifications]
|
|
24
|
+
M --> N[Update Stats]
|
|
25
|
+
N --> O[Release Lock]
|
|
26
|
+
I -->|No| H
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
subgraph "Base Layer"
|
|
30
|
+
P["SchedulingEngineBase<br/>(Metadata Cache)"]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
A -->|extends| P
|
|
34
|
+
|
|
35
|
+
style A fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
36
|
+
style B fill:#7c5295,stroke:#563a6b,color:#fff
|
|
37
|
+
style C fill:#7c5295,stroke:#563a6b,color:#fff
|
|
38
|
+
style E fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
39
|
+
style F fill:#b8762f,stroke:#8a5722,color:#fff
|
|
40
|
+
style G fill:#b8762f,stroke:#8a5722,color:#fff
|
|
41
|
+
style P fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Overview
|
|
45
|
+
|
|
46
|
+
**Server-side only.** This engine handles the complete lifecycle of scheduled job execution:
|
|
47
|
+
|
|
48
|
+
- **Cron Evaluation**: Parses cron expressions to determine which jobs are due for execution
|
|
49
|
+
- **Plugin Architecture**: Each job type has a registered `BaseScheduledJob` driver class
|
|
50
|
+
- **Distributed Locking**: Token-based locking for multi-server environments with stale lock detection
|
|
51
|
+
- **Concurrency Modes**: `Concurrent`, `Queue`, or `Skip` for overlapping executions
|
|
52
|
+
- **Polling Lifecycle**: Adaptive polling interval with `StartPolling()` / `StopPolling()`
|
|
53
|
+
- **Notifications**: Configurable notifications on success, failure, or both
|
|
54
|
+
- **Statistics Tracking**: Automatic update of RunCount, SuccessCount, FailureCount, and timing metrics
|
|
55
|
+
- **Built-in Drivers**: `AgentScheduledJobDriver` for AI agents, `ActionScheduledJobDriver` for MJ Actions
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install @memberjunction/scheduling-engine
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
### Starting the Scheduler
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { SchedulingEngine } from '@memberjunction/scheduling-engine';
|
|
69
|
+
|
|
70
|
+
const engine = SchedulingEngine.Instance;
|
|
71
|
+
await engine.Config(false, contextUser);
|
|
72
|
+
|
|
73
|
+
// Start continuous polling
|
|
74
|
+
await engine.StartPolling(contextUser);
|
|
75
|
+
|
|
76
|
+
// Stop polling (e.g., on server shutdown)
|
|
77
|
+
engine.StopPolling();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Manual Job Execution
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Execute all due jobs
|
|
84
|
+
const runs = await engine.ExecuteScheduledJobs(contextUser);
|
|
85
|
+
console.log(`Executed ${runs.length} scheduled jobs`);
|
|
86
|
+
|
|
87
|
+
// Execute a specific job immediately
|
|
88
|
+
const run = await engine.ExecuteJob(jobId, contextUser);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Creating Custom Job Drivers
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { BaseScheduledJob, ScheduledJobExecutionContext } from '@memberjunction/scheduling-engine';
|
|
95
|
+
import { ScheduledJobResult } from '@memberjunction/scheduling-base-types';
|
|
96
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
97
|
+
|
|
98
|
+
@RegisterClass(BaseScheduledJob, 'ScheduledJobCustom')
|
|
99
|
+
export class CustomScheduledJobDriver extends BaseScheduledJob {
|
|
100
|
+
async Execute(context: ScheduledJobExecutionContext): Promise<ScheduledJobResult> {
|
|
101
|
+
const config = this.parseConfiguration(context.Schedule);
|
|
102
|
+
|
|
103
|
+
// Custom job logic here
|
|
104
|
+
await this.doWork(config);
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
Success: true,
|
|
108
|
+
Details: { processedItems: 42 }
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Components
|
|
115
|
+
|
|
116
|
+
### SchedulingEngine
|
|
117
|
+
|
|
118
|
+
The main singleton engine that extends `SchedulingEngineBase` with execution capabilities.
|
|
119
|
+
|
|
120
|
+
### BaseScheduledJob
|
|
121
|
+
|
|
122
|
+
Abstract base class for all job drivers. Provides:
|
|
123
|
+
- `Execute()` -- abstract method for job-specific logic
|
|
124
|
+
- `parseConfiguration()` -- type-safe configuration parsing from JSON
|
|
125
|
+
- `FormatNotification()` -- customizable notification content generation
|
|
126
|
+
- `ValidateConfiguration()` -- configuration validation before execution
|
|
127
|
+
|
|
128
|
+
### Built-in Drivers
|
|
129
|
+
|
|
130
|
+
| Driver | Job Type | Description |
|
|
131
|
+
|--------|----------|-------------|
|
|
132
|
+
| `AgentScheduledJobDriver` | Agent | Executes AI agents with configurable conversations and payloads |
|
|
133
|
+
| `ActionScheduledJobDriver` | Action | Executes MJ Actions with static or SQL-based parameter values |
|
|
134
|
+
|
|
135
|
+
### CronExpressionHelper
|
|
136
|
+
|
|
137
|
+
Utility for parsing and evaluating cron expressions using `cron-parser`.
|
|
138
|
+
|
|
139
|
+
### NotificationManager
|
|
140
|
+
|
|
141
|
+
Manages notification delivery based on job configuration (on success, failure, or both).
|
|
142
|
+
|
|
143
|
+
## Dependencies
|
|
144
|
+
|
|
145
|
+
| Package | Purpose |
|
|
146
|
+
|---------|---------|
|
|
147
|
+
| `@memberjunction/scheduling-engine-base` | Base engine with metadata caching |
|
|
148
|
+
| `@memberjunction/scheduling-base-types` | Shared type definitions |
|
|
149
|
+
| `@memberjunction/core` | Metadata, RunView, logging |
|
|
150
|
+
| `@memberjunction/core-entities` | Entity types |
|
|
151
|
+
| `@memberjunction/global` | Class factory, RegisterClass |
|
|
152
|
+
| `@memberjunction/ai-agents` | AI agent execution |
|
|
153
|
+
| `@memberjunction/actions` | MJ Action execution |
|
|
154
|
+
| `cron-parser` | Cron expression evaluation |
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
ISC
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/scheduling-engine",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.1.0",
|
|
5
5
|
"description": "MemberJunction: Scheduling Engine - core engine for executing scheduled jobs with plugin architecture",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -15,16 +15,16 @@
|
|
|
15
15
|
"author": "MemberJunction.com",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@memberjunction/actions": "4.
|
|
19
|
-
"@memberjunction/actions-base": "4.
|
|
20
|
-
"@memberjunction/ai-core-plus": "4.
|
|
21
|
-
"@memberjunction/ai-agents": "4.
|
|
22
|
-
"@memberjunction/core": "4.
|
|
23
|
-
"@memberjunction/core-entities": "4.
|
|
24
|
-
"@memberjunction/global": "4.
|
|
25
|
-
"@memberjunction/scheduling-base-types": "4.
|
|
26
|
-
"@memberjunction/scheduling-engine-base": "4.
|
|
27
|
-
"@memberjunction/sqlserver-dataprovider": "4.
|
|
18
|
+
"@memberjunction/actions": "4.1.0",
|
|
19
|
+
"@memberjunction/actions-base": "4.1.0",
|
|
20
|
+
"@memberjunction/ai-core-plus": "4.1.0",
|
|
21
|
+
"@memberjunction/ai-agents": "4.1.0",
|
|
22
|
+
"@memberjunction/core": "4.1.0",
|
|
23
|
+
"@memberjunction/core-entities": "4.1.0",
|
|
24
|
+
"@memberjunction/global": "4.1.0",
|
|
25
|
+
"@memberjunction/scheduling-base-types": "4.1.0",
|
|
26
|
+
"@memberjunction/scheduling-engine-base": "4.1.0",
|
|
27
|
+
"@memberjunction/sqlserver-dataprovider": "4.1.0",
|
|
28
28
|
"cron-parser": "^4.9.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|