wirejs-scripts 3.0.167 → 3.0.169
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/bin.js +61 -1
- package/package.json +3 -2
package/bin.js
CHANGED
|
@@ -20,10 +20,12 @@ import {
|
|
|
20
20
|
requiresContext,
|
|
21
21
|
Context,
|
|
22
22
|
CookieJar,
|
|
23
|
+
CronJob,
|
|
23
24
|
Endpoint,
|
|
24
25
|
SystemAttribute,
|
|
25
26
|
} from 'wirejs-resources';
|
|
26
27
|
import { prebuildApi } from 'wirejs-resources/internal';
|
|
28
|
+
import nodeCron from 'node-cron';
|
|
27
29
|
|
|
28
30
|
import * as DefaultGateway from 'default-gateway';
|
|
29
31
|
import * as NatPmp from 'nat-pmp';
|
|
@@ -40,6 +42,9 @@ let networkIp = null;
|
|
|
40
42
|
let httpPort = 3000;
|
|
41
43
|
let wsPort = 3001;
|
|
42
44
|
|
|
45
|
+
/** Tracks active node-cron tasks for CronJobs, keyed by absoluteId. */
|
|
46
|
+
const activeCronTasks = new Map();
|
|
47
|
+
|
|
43
48
|
const logger = {
|
|
44
49
|
log(...items) {
|
|
45
50
|
console.log('wirejs', ...items);
|
|
@@ -68,6 +73,58 @@ globalThis.fetch = (url, ...args) => {
|
|
|
68
73
|
}
|
|
69
74
|
}
|
|
70
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Clear all active local cron schedules and re-register them from the API module.
|
|
78
|
+
* Called after each API rebuild to ensure hot reloads pick up new schedules.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} apiDir
|
|
81
|
+
*/
|
|
82
|
+
async function refreshCronSchedules(apiDir) {
|
|
83
|
+
// Stop and remove all currently active cron tasks.
|
|
84
|
+
for (const task of activeCronTasks.values()) {
|
|
85
|
+
task.stop();
|
|
86
|
+
}
|
|
87
|
+
activeCronTasks.clear();
|
|
88
|
+
|
|
89
|
+
// Clear the registered jobs map so a fresh import can re-populate it.
|
|
90
|
+
CronJob.registeredJobs.clear();
|
|
91
|
+
|
|
92
|
+
// Import the freshly built API module to trigger CronJob registrations.
|
|
93
|
+
const apiPath = path.join(apiDir, 'dist', 'index.js');
|
|
94
|
+
try {
|
|
95
|
+
await import(`${apiPath}?cron-refresh=${Date.now()}`);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
logger.error('Failed to import API module for cron scheduling:', error);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Schedule each registered CronJob using node-cron.
|
|
102
|
+
for (const [absoluteId, { schedule, handler }] of CronJob.registeredJobs) {
|
|
103
|
+
if (!nodeCron.validate(schedule)) {
|
|
104
|
+
logger.warn(`CronJob "${absoluteId}" has invalid cron expression: "${schedule}". Skipping.`);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
logger.log(`Scheduling CronJob "${absoluteId}" with schedule "${schedule}"`);
|
|
108
|
+
const task = nodeCron.schedule(schedule, async () => {
|
|
109
|
+
logger.log(`Running CronJob "${absoluteId}"`);
|
|
110
|
+
try {
|
|
111
|
+
// Use the latest handler from the registry in case of hot reload.
|
|
112
|
+
const current = CronJob.registeredJobs.get(absoluteId);
|
|
113
|
+
if (current) {
|
|
114
|
+
await current.handler();
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
logger.error(`CronJob "${absoluteId}" failed:`, error);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
activeCronTasks.set(absoluteId, task);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (activeCronTasks.size > 0) {
|
|
124
|
+
logger.log(`${activeCronTasks.size} cron job(s) scheduled.`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
71
128
|
function isWSL() {
|
|
72
129
|
if (os.platform() !== 'linux') return false;
|
|
73
130
|
if (process.env.WSL_DISTRO_NAME) return true;
|
|
@@ -614,7 +671,10 @@ async function compile(watch = false) {
|
|
|
614
671
|
plugins: [{
|
|
615
672
|
name: 'post-build-rebuild-api-client',
|
|
616
673
|
setup(build) {
|
|
617
|
-
build.onEnd(() =>
|
|
674
|
+
build.onEnd(async () => {
|
|
675
|
+
await prebuildApi(apiDir);
|
|
676
|
+
await refreshCronSchedules(apiDir);
|
|
677
|
+
})
|
|
618
678
|
}
|
|
619
679
|
}]
|
|
620
680
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-scripts",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.169",
|
|
4
4
|
"description": "Basic build and start commands for wirejs apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -32,11 +32,12 @@
|
|
|
32
32
|
"marked": "^2.0.1",
|
|
33
33
|
"mime-types": "^2.1.35",
|
|
34
34
|
"nat-pmp": "^1.0.0",
|
|
35
|
+
"node-cron": "^4.2.1",
|
|
35
36
|
"raw-loader": "^4.0.2",
|
|
36
37
|
"rimraf": "^6.0.1",
|
|
37
38
|
"style-loader": "^2.0.0",
|
|
38
39
|
"webpack": "^5.97.1",
|
|
39
40
|
"wirejs-dom": "^1.0.44",
|
|
40
|
-
"wirejs-resources": "^0.1.
|
|
41
|
+
"wirejs-resources": "^0.1.171"
|
|
41
42
|
}
|
|
42
43
|
}
|