Skip to content

Commit 02227ed

Browse files
committed
+ Added schema validation to Jobs.run and updated tests
1 parent d5d63a9 commit 02227ed

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

package/server/api.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,48 @@ import './imports/startup'
55

66
const Jobs = {}
77

8+
// Validated Jobs.run configuration schema
9+
10+
const JobRunConfigSchema = {
11+
in: Match.Maybe({
12+
millisecond: Match.Maybe(Number),
13+
milliseconds: Match.Maybe(Number),
14+
second: Match.Maybe(Number),
15+
seconds: Match.Maybe(Number),
16+
minute: Match.Maybe(Number),
17+
minutes: Match.Maybe(Number),
18+
hour: Match.Maybe(Number),
19+
hours: Match.Maybe(Number),
20+
day: Match.Maybe(Number),
21+
days: Match.Maybe(Number),
22+
month: Match.Maybe(Number),
23+
months: Match.Maybe(Number),
24+
year: Match.Maybe(Number),
25+
years: Match.Maybe(Number),
26+
}),
27+
on: Match.Maybe({
28+
millisecond: Match.Maybe(Number),
29+
milliseconds: Match.Maybe(Number),
30+
second: Match.Maybe(Number),
31+
seconds: Match.Maybe(Number),
32+
minute: Match.Maybe(Number),
33+
minutes: Match.Maybe(Number),
34+
hour: Match.Maybe(Number),
35+
hours: Match.Maybe(Number),
36+
day: Match.Maybe(Number),
37+
days: Match.Maybe(Number),
38+
month: Match.Maybe(Number),
39+
months: Match.Maybe(Number),
40+
year: Match.Maybe(Number),
41+
years: Match.Maybe(Number),
42+
}),
43+
date: Match.Maybe(Date),
44+
priority: Match.Maybe(Number),
45+
unique: Match.Maybe(Boolean),
46+
singular: Match.Maybe(Boolean),
47+
callback: Match.Maybe(Function),
48+
};
49+
850
// Configure the package (optional)
951

1052
Jobs.configure = function (config) {
@@ -53,9 +95,13 @@ Jobs.register = function (jobs) {
5395
// Adds a new job to MongoDB
5496

5597
Jobs.run = function () {
56-
check(arguments[0], String)
98+
check(arguments[0], String);
5799

58100
const lastArg = arguments[arguments.length - 1];
101+
const config = (typeof lastArg === "object" && !Array.isArray(lastArg) && !lastArg.remote) ? lastArg : {};
102+
103+
check(config, Match.Optional(JobRunConfigSchema));
104+
59105
const remote = typeof lastArg === "object" && lastArg.remote;
60106

61107
if (Utilities.registry.data[arguments[0]] || remote) {

tests/configSchemaValidation.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
JobsTests4 = function () {
2+
3+
// 0 - Clear the collection
4+
console.log("--- 0 ---")
5+
var clear = Jobs.clear("*")
6+
console.log(clear)
7+
8+
// 1 - Register a Job
9+
console.log("--- 1 ---")
10+
Jobs.register({
11+
"sendReminder": function (email, message) {
12+
console.log("Sending reminder to " + email + " with message: " + message);
13+
this.success();
14+
},
15+
"simpleJob": function () {
16+
console.log("Running simple job with no arguments.");
17+
this.success();
18+
}
19+
})
20+
21+
// 2 - Schedule a job with valid config
22+
console.log("--- 2 ---")
23+
try {
24+
var jobId1 = Jobs.run("sendReminder", "[email protected]", "The future is here!", {
25+
in: {
26+
days: 3
27+
},
28+
priority: 999,
29+
singular: true,
30+
callback: function () {
31+
console.log("Callback executed successfully for jobId1");
32+
}
33+
})
34+
console.log("JobId1 created successfully:", jobId1._id)
35+
} catch (e) {
36+
console.error("Error scheduling job with valid config:", e.message);
37+
}
38+
39+
// 3 - Schedule a job with only the job name (no config or additional arguments)
40+
console.log("--- 3 ---")
41+
try {
42+
var jobId2 = Jobs.run("simpleJob");
43+
console.log("JobId2 created successfully:", jobId2._id)
44+
} catch (e) {
45+
console.error("Error scheduling simple job:", e.message);
46+
}
47+
48+
// 4 - Schedule a job with invalid config (typo in 'priority')
49+
console.log("--- 4 ---")
50+
try {
51+
var jobId3 = Jobs.run("sendReminder", "[email protected]", "Hello again!", {
52+
in: {
53+
days: 1
54+
},
55+
prioirty: 1000, // Intentional typo: should be 'priority'
56+
singular: true,
57+
callback: function () {
58+
console.log("This callback should not be executed for jobId3");
59+
}
60+
})
61+
console.error("JobId3 should not be created due to invalid config.");
62+
} catch (e) {
63+
console.log("Caught expected error for invalid config:", e.message);
64+
}
65+
66+
// 5 - Schedule a job with an unsupported config key
67+
console.log("--- 5 ---")
68+
try {
69+
var jobId4 = Jobs.run("sendReminder", "[email protected]", "Reminder!", {
70+
in: {
71+
hours: 2
72+
},
73+
singular: true,
74+
unsupportedKey: "this should fail" // Unsupported key
75+
})
76+
console.error("JobId4 should not be created due to unsupported config key.");
77+
} catch (e) {
78+
console.log("Caught expected error for unsupported config key:", e.message);
79+
}
80+
81+
// 6 - Log whatever is in the collection
82+
console.log("--- 6 ---")
83+
var allJobDocs = JobsInternal.Utilities.collection.find().fetch();
84+
console.log(allJobDocs);
85+
86+
// 7 - Cancel the successfully created job (jobId1)
87+
console.log("--- 7 ---")
88+
if (jobId1 && jobId1._id) {
89+
var cancel = Jobs.cancel(jobId1._id);
90+
console.log("Cancelled jobId1:", cancel);
91+
92+
// 8 - Verify the job was cancelled
93+
var jobDoc = Jobs.get(jobId1._id);
94+
console.log("--- 8 ---")
95+
console.log("Job doc after cancel:")
96+
console.log(jobDoc)
97+
98+
if (jobDoc.state === "cancelled") {
99+
console.log("Job was successfully cancelled")
100+
} else {
101+
console.log("Job cancel failed")
102+
}
103+
} else {
104+
console.error("jobId1 was not created successfully, skipping cancel test.");
105+
}
106+
107+
// 9 - Try to cancel a job that was created without any config or arguments (jobId2)
108+
console.log("--- 9 ---")
109+
if (jobId2 && jobId2._id) {
110+
var cancel2 = Jobs.cancel(jobId2._id);
111+
console.log("Cancelled jobId2:", cancel2);
112+
113+
// 10 - Verify the job was cancelled
114+
var jobDoc2 = Jobs.get(jobId2._id);
115+
console.log("--- 10 ---")
116+
console.log("Job doc after cancel:")
117+
console.log(jobDoc2)
118+
119+
if (jobDoc2.state === "cancelled") {
120+
console.log("Job was successfully cancelled")
121+
} else {
122+
console.log("Job cancel failed")
123+
}
124+
} else {
125+
console.error("jobId2 was not created successfully, skipping cancel test.");
126+
}
127+
}

0 commit comments

Comments
 (0)