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