-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathshared.coffee
1282 lines (1123 loc) · 34.7 KB
/
shared.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############################################################################
# Copyright (C) 2014-2017 by Vaughn Iverson
# job-collection is free software released under the MIT/X11 license.
# See included LICENSE file for details.
############################################################################
_validNumGTEZero = (v) ->
Match.test(v, Number) and v >= 0.0
_validNumGTZero = (v) ->
Match.test(v, Number) and v > 0.0
_validNumGTEOne = (v) ->
Match.test(v, Number) and v >= 1.0
_validIntGTEZero = (v) ->
_validNumGTEZero(v) and Math.floor(v) is v
_validIntGTEOne = (v) ->
_validNumGTEOne(v) and Math.floor(v) is v
_validStatus = (v) ->
Match.test(v, String) and v in Job.jobStatuses
_validLogLevel = (v) ->
Match.test(v, String) and v in Job.jobLogLevels
_validRetryBackoff = (v) ->
Match.test(v, String) and v in Job.jobRetryBackoffMethods
_validId = (v) ->
Match.test(v, Match.OneOf(String, Mongo.Collection.ObjectID))
_validLog = () ->
[{
time: Date
runId: Match.OneOf(Match.Where(_validId), null)
level: Match.Where(_validLogLevel)
message: String
data: Match.Optional Object
}]
_validProgress = () ->
completed: Match.Where(_validNumGTEZero)
total: Match.Where(_validNumGTEZero)
percent: Match.Where(_validNumGTEZero)
_validLaterJSObj = () ->
schedules: [ Object ]
exceptions: Match.Optional [ Object ]
_validJobDoc = () ->
_id: Match.Optional Match.OneOf(Match.Where(_validId), null)
runId: Match.OneOf(Match.Where(_validId), null)
type: String
status: Match.Where _validStatus
data: Object
result: Match.Optional Object
failures: Match.Optional [ Object ]
priority: Match.Integer
depends: [ Match.Where(_validId) ]
resolved: [ Match.Where(_validId) ]
after: Date
updated: Date
workTimeout: Match.Optional Match.Where(_validIntGTEOne)
expiresAfter: Match.Optional Date
log: Match.Optional _validLog()
progress: _validProgress()
retries: Match.Where _validIntGTEZero
retried: Match.Where _validIntGTEZero
repeatRetries: Match.Optional Match.Where _validIntGTEZero
retryUntil: Date
retryWait: Match.Where _validIntGTEZero
retryBackoff: Match.Where _validRetryBackoff
repeats: Match.Where _validIntGTEZero
repeated: Match.Where _validIntGTEZero
repeatUntil: Date
repeatWait: Match.OneOf(Match.Where(_validIntGTEZero), Match.Where(_validLaterJSObj))
created: Date
class JobCollectionBase extends Mongo.Collection
constructor: (@root = 'queue', options = {}) ->
unless @ instanceof JobCollectionBase
return new JobCollectionBase(@root, options)
unless @ instanceof Mongo.Collection
throw new Meteor.Error 'The global definition of Mongo.Collection has changed since the job-collection package was loaded. Please ensure that any packages that redefine Mongo.Collection are loaded before job-collection.'
unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'
@later = later # later object, for convenience
options.noCollectionSuffix ?= false
collectionName = @root
unless options.noCollectionSuffix
collectionName += '.jobs'
# Remove non-standard options before
# calling Mongo.Collection constructor
delete options.noCollectionSuffix
Job.setDDP(options.connection, @root)
@_createLogEntry = (message = '', runId = null, level = 'info', time = new Date(), data = null) ->
l = { time: time, runId: runId, message: message, level: level }
return l
@_logMessage =
'readied': (() -> @_createLogEntry "Promoted to ready").bind(@)
'forced': ((id) -> @_createLogEntry "Dependencies force resolved", null, 'warning').bind(@)
'rerun': ((id, runId) -> @_createLogEntry "Rerunning job", null, 'info', new Date(), {previousJob:{id:id,runId:runId}}).bind(@)
'running': ((runId) -> @_createLogEntry "Job Running", runId).bind(@)
'paused': (() -> @_createLogEntry "Job Paused").bind(@)
'resumed': (() -> @_createLogEntry "Job Resumed").bind(@)
'cancelled': (() -> @_createLogEntry "Job Cancelled", null, 'warning').bind(@)
'restarted': (() -> @_createLogEntry "Job Restarted").bind(@)
'resubmitted': (() -> @_createLogEntry "Job Resubmitted").bind(@)
'submitted': (() -> @_createLogEntry "Job Submitted").bind(@)
'completed': ((runId) -> @_createLogEntry "Job Completed", runId, 'success').bind(@)
'resolved': ((id, runId) -> @_createLogEntry "Dependency resolved", null, 'info', new Date(), {dependency:{id:id,runId:runId}}).bind(@)
'failed': ((runId, fatal, err) ->
value = err.value
msg = "Job Failed with#{if fatal then ' Fatal' else ''} Error#{if value? and typeof value is 'string' then ': ' + value else ''}."
level = if fatal then 'danger' else 'warning'
@_createLogEntry msg, runId, level).bind(@)
# Call super's constructor
super collectionName, options
_validNumGTEZero: _validNumGTEZero
_validNumGTZero: _validNumGTZero
_validNumGTEOne: _validNumGTEOne
_validIntGTEZero: _validIntGTEZero
_validIntGTEOne: _validIntGTEOne
_validStatus: _validStatus
_validLogLevel: _validLogLevel
_validRetryBackoff: _validRetryBackoff
_validId: _validId
_validLog: _validLog
_validProgress: _validProgress
_validJobDoc: _validJobDoc
jobLogLevels: Job.jobLogLevels
jobPriorities: Job.jobPriorities
jobStatuses: Job.jobStatuses
jobStatusCancellable: Job.jobStatusCancellable
jobStatusPausable: Job.jobStatusPausable
jobStatusRemovable: Job.jobStatusRemovable
jobStatusRestartable: Job.jobStatusRestartable
forever: Job.forever
foreverDate: Job.foreverDate
ddpMethods: Job.ddpMethods
ddpPermissionLevels: Job.ddpPermissionLevels
ddpMethodPermissions: Job.ddpMethodPermissions
processJobs: (params...) -> new Job.processJobs @root, params...
getJob: (params...) -> Job.getJob @root, params...
getWork: (params...) -> Job.getWork @root, params...
getJobs: (params...) -> Job.getJobs @root, params...
readyJobs: (params...) -> Job.readyJobs @root, params...
cancelJobs: (params...) -> Job.cancelJobs @root, params...
pauseJobs: (params...) -> Job.pauseJobs @root, params...
resumeJobs: (params...) -> Job.resumeJobs @root, params...
restartJobs: (params...) -> Job.restartJobs @root, params...
removeJobs: (params...) -> Job.removeJobs @root, params...
setDDP: (params...) -> Job.setDDP params...
startJobServer: (params...) -> Job.startJobServer @root, params...
shutdownJobServer: (params...) -> Job.shutdownJobServer @root, params...
# These are deprecated and will be removed
startJobs: (params...) -> Job.startJobs @root, params...
stopJobs: (params...) -> Job.stopJobs @root, params...
jobDocPattern: _validJobDoc()
# Warning Stubs for server-only calls
allow: () -> throw new Error "Server-only function jc.allow() invoked on client."
deny: () -> throw new Error "Server-only function jc.deny() invoked on client."
promote: () -> throw new Error "Server-only function jc.promote() invoked on client."
setLogStream: () -> throw new Error "Server-only function jc.setLogStream() invoked on client."
# Warning Stubs for client-only calls
logConsole: () -> throw new Error "Client-only function jc.logConsole() invoked on server."
# Deprecated. Remove in next major version
makeJob: do () ->
dep = false
(params...) ->
unless dep
dep = true
console.warn "WARNING: jc.makeJob() has been deprecated. Use new Job(jc, doc) instead."
new Job @root, params...
# Deprecated. Remove in next major version
createJob: do () ->
dep = false
(params...) ->
unless dep
dep = true
console.warn "WARNING: jc.createJob() has been deprecated. Use new Job(jc, type, data) instead."
new Job @root, params...
_methodWrapper: (method, func) ->
toLog = @_toLog
unblockDDPMethods = @_unblockDDPMethods ? false
# Return the wrapper function that the Meteor method will actually invoke
return (params...) ->
user = this.userId ? "[UNAUTHENTICATED]"
toLog user, method, "params: " + JSON.stringify(params)
this.unblock() if unblockDDPMethods
retval = func(params...)
toLog user, method, "returned: " + JSON.stringify(retval)
return retval
_generateMethods: () ->
methodsOut = {}
methodPrefix = '_DDPMethod_'
for methodName, methodFunc of @ when methodName[0...methodPrefix.length] is methodPrefix
baseMethodName = methodName[methodPrefix.length..]
methodsOut["#{@root}_#{baseMethodName}"] = @_methodWrapper(baseMethodName, methodFunc.bind(@))
return methodsOut
_idsOfDeps: (ids, antecedents, dependents, jobStatuses) ->
# Cancel the entire tree of antecedents and/or dependents
# Dependents: jobs that list one of the ids in their depends list
# Antecedents: jobs with an id listed in the depends list of one of the jobs in ids
dependsQuery = []
dependsIds = []
if dependents
dependsQuery.push
depends:
$elemMatch:
$in: ids
if antecedents
antsArray = []
@find(
{
_id:
$in: ids
}
{
fields:
depends: 1
transform: null
}
).forEach (d) -> antsArray.push(i) for i in d.depends unless i in antsArray
if antsArray.length > 0
dependsQuery.push
_id:
$in: antsArray
if dependsQuery.length > 0
@find(
{
status:
$in: jobStatuses
$or: dependsQuery
}
{
fields:
_id: 1
transform: null
}
).forEach (d) ->
dependsIds.push d._id unless d._id in dependsIds
return dependsIds
_rerun_job: (doc, repeats = doc.repeats - 1, wait = doc.repeatWait, repeatUntil = doc.repeatUntil) ->
# Repeat? if so, make a new job from the old one
id = doc._id
runId = doc.runId
time = new Date()
delete doc._id
delete doc.result
delete doc.failures
delete doc.expiresAfter
delete doc.workTimeout
doc.runId = null
doc.status = "waiting"
doc.repeatRetries = if doc.repeatRetries? then doc.repeatRetries else doc.retries + doc.retried
doc.retries = doc.repeatRetries
doc.retries = @forever if doc.retries > @forever
doc.retryUntil = repeatUntil
doc.retried = 0
doc.repeats = repeats
doc.repeats = @forever if doc.repeats > @forever
doc.repeatUntil = repeatUntil
doc.repeated = doc.repeated + 1
doc.updated = time
doc.created = time
doc.progress =
completed: 0
total: 1
percent: 0
if logObj = @_logMessage.rerun id, runId
doc.log = [logObj]
else
doc.log = []
doc.after = new Date(time.valueOf() + wait)
if jobId = @insert doc
@_DDPMethod_jobReady jobId
return jobId
else
console.warn "Job rerun/repeat failed to reschedule!", id, runId
return null
_checkDeps: (job, dryRun = true) ->
cancel = false
resolved = []
failed = []
cancelled = []
removed = []
log = []
if job.depends.length > 0
deps = @find({_id: { $in: job.depends }},{ fields: { _id: 1, runId: 1, status: 1 } }).fetch()
if deps.length isnt job.depends.length
foundIds = deps.map (d) -> d._id
for j in job.depends when not (j in foundIds)
@_DDPMethod_jobLog job._id, null, "Antecedent job #{j} missing at save" unless dryRun
removed.push j
cancel = true
for depJob in deps
unless depJob.status in @jobStatusCancellable
switch depJob.status
when "completed"
resolved.push depJob._id
log.push @_logMessage.resolved depJob._id, depJob.runId
when "failed"
cancel = true
failed.push depJob._id
@_DDPMethod_jobLog job._id, null, "Antecedent job failed before save" unless dryRun
when "cancelled"
cancel = true
cancelled.push depJob._id
@_DDPMethod_jobLog job._id, null, "Antecedent job cancelled before save" unless dryRun
else # Unknown status
throw new Meteor.Error "Unknown status in jobSave Dependency check"
unless resolved.length is 0 or dryRun
mods =
$pull:
depends:
$in: resolved
$push:
resolved:
$each: resolved
log:
$each: log
n = @update(
{
_id: job._id
status: 'waiting'
}
mods
)
unless n
console.warn "Update for job #{job._id} during dependency check failed."
if cancel and not dryRun
@_DDPMethod_jobCancel job._id
return false
if dryRun
if cancel or resolved.length > 0
return {
jobId: job._id
resolved: resolved
failed: failed
cancelled: cancelled
removed: removed
}
else
return false
else
return true
_DDPMethod_startJobServer: (options) ->
check options, Match.Optional {}
options ?= {}
# The client can't actually do this, so skip it
unless @isSimulation
Meteor.clearTimeout(@stopped) if @stopped and @stopped isnt true
@stopped = false
return true
_DDPMethod_startJobs: do () =>
depFlag = false
(options) ->
unless depFlag
depFlag = true
console.warn "Deprecation Warning: jc.startJobs() has been renamed to jc.startJobServer()"
return @_DDPMethod_startJobServer options
_DDPMethod_shutdownJobServer: (options) ->
check options, Match.Optional
timeout: Match.Optional(Match.Where _validIntGTEOne)
options ?= {}
options.timeout ?= 60*1000
# The client can't actually do any of this, so skip it
unless @isSimulation
Meteor.clearTimeout(@stopped) if @stopped and @stopped isnt true
@stopped = Meteor.setTimeout(
() =>
cursor = @find(
{
status: 'running'
},
{
transform: null
}
)
failedJobs = cursor.count()
console.warn "Failing #{failedJobs} jobs on queue stop." if failedJobs isnt 0
cursor.forEach (d) => @_DDPMethod_jobFail d._id, d.runId, "Running at Job Server shutdown."
if @logStream? # Shutting down closes the logStream!
@logStream.end()
@logStream = null
options.timeout
)
return true
_DDPMethod_stopJobs: do () =>
depFlag = false
(options) ->
unless depFlag
depFlag = true
console.warn "Deprecation Warning: jc.stopJobs() has been renamed to jc.shutdownJobServer()"
return @_DDPMethod_shutdownJobServer options
_DDPMethod_getJob: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional
getLog: Match.Optional Boolean
getFailures: Match.Optional Boolean
options ?= {}
options.getLog ?= false
options.getFailures ?= false
single = false
if _validId(ids)
ids = [ids]
single = true
return null if ids.length is 0
fields = {_private:0}
fields.log = 0 if !options.getLog
fields.failures = 0 if !options.getFailures
docs = @find(
{
_id:
$in: ids
}
{
fields: fields
transform: null
}
).fetch()
if docs?.length
if @scrub?
docs = (@scrub d for d in docs)
check docs, [_validJobDoc()]
if single
return docs[0]
else
return docs
return null
_DDPMethod_getWork: (type, options) ->
check type, Match.OneOf String, [ String ]
check options, Match.Optional
maxJobs: Match.Optional(Match.Where _validIntGTEOne)
workTimeout: Match.Optional(Match.Where _validIntGTEOne)
# Don't simulate getWork!
if @isSimulation
return
options ?= {}
options.maxJobs ?= 1
# Don't put out any more jobs while shutting down
if @stopped
return []
# Support string types or arrays of string types
if typeof type is 'string'
type = [ type ]
time = new Date()
docs = []
runId = @_makeNewID() # This is meteor internal, but it will fail hard if it goes away.
while docs.length < options.maxJobs
ids = @find(
{
type:
$in: type
status: 'ready'
runId: null
}
{
sort:
priority: 1
retryUntil: 1
after: 1
limit: options.maxJobs - docs.length # never ask for more than is needed
fields:
_id: 1
transform: null
}).map (d) -> d._id
unless ids?.length > 0
break # Don't keep looping when there's no available work
mods =
$set:
status: 'running'
runId: runId
updated: time
$inc:
retries: -1
retried: 1
if logObj = @_logMessage.running runId
mods.$push =
log: logObj
if options.workTimeout?
mods.$set.workTimeout = options.workTimeout
mods.$set.expiresAfter = new Date(time.valueOf() + options.workTimeout)
else
mods.$unset ?= {}
mods.$unset.workTimeout = ""
mods.$unset.expiresAfter = ""
num = @update(
{
_id:
$in: ids
status: 'ready'
runId: null
}
mods
{
multi: true
}
)
if num > 0
foundDocs = @find(
{
_id:
$in: ids
runId: runId
}
{
fields:
log: 0
failures: 0
_private: 0
transform: null
}
).fetch()
if foundDocs?.length > 0
if @scrub?
foundDocs = (@scrub d for d in foundDocs)
check docs, [ _validJobDoc() ]
docs = docs.concat foundDocs
# else
# console.warn "getWork: find after update failed"
return docs
_DDPMethod_jobRemove: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional {}
options ?= {}
if _validId(ids)
ids = [ids]
return false if ids.length is 0
num = @remove(
{
_id:
$in: ids
status:
$in: @jobStatusRemovable
}
)
if num > 0
return true
else
console.warn "jobRemove failed"
return false
_DDPMethod_jobPause: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional {}
options ?= {}
if _validId(ids)
ids = [ids]
return false if ids.length is 0
time = new Date()
mods =
$set:
status: "paused"
updated: time
if logObj = @_logMessage.paused()
mods.$push =
log: logObj
num = @update(
{
_id:
$in: ids
status:
$in: @jobStatusPausable
}
mods
{
multi: true
}
)
if num > 0
return true
else
console.warn "jobPause failed"
return false
_DDPMethod_jobResume: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional {}
options ?= {}
if _validId(ids)
ids = [ids]
return false if ids.length is 0
time = new Date()
mods =
$set:
status: "waiting"
updated: time
if logObj = @_logMessage.resumed()
mods.$push =
log: logObj
num = @update(
{
_id:
$in: ids
status: "paused"
updated:
$ne: time
}
mods
{
multi: true
}
)
if num > 0
@_DDPMethod_jobReady ids
return true
else
console.warn "jobResume failed"
return false
_DDPMethod_jobReady: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional
force: Match.Optional Boolean
time: Match.Optional Date
# Don't simulate jobReady. It has a strong chance of causing issues with
# Meteor on the client, particularly if an observeChanges() is triggering
# a processJobs queue (which in turn sets timers.)
if @isSimulation
return
now = new Date()
options ?= {}
options.force ?= false
options.time ?= now
if _validId(ids)
ids = [ids]
query =
status: "waiting"
after:
$lte: options.time
mods =
$set:
status: "ready"
updated: now
if ids.length > 0
query._id =
$in: ids
mods.$set.after = now
logObj = []
if options.force
mods.$set.depends = [] # Don't move to resolved, because they weren't!
l = @_logMessage.forced()
logObj.push l if l
else
query.depends =
$size: 0
l = @_logMessage.readied()
logObj.push l if l
if logObj.length > 0
mods.$push =
log:
$each: logObj
num = @update(
query
mods
{
multi: true
}
)
if num > 0
return true
else
return false
_DDPMethod_jobCancel: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional
antecedents: Match.Optional Boolean
dependents: Match.Optional Boolean
options ?= {}
options.antecedents ?= false
options.dependents ?= true
if _validId(ids)
ids = [ids]
return false if ids.length is 0
time = new Date()
mods =
$set:
status: "cancelled"
runId: null
progress:
completed: 0
total: 1
percent: 0
updated: time
if logObj = @_logMessage.cancelled()
mods.$push =
log: logObj
num = @update(
{
_id:
$in: ids
status:
$in: @jobStatusCancellable
}
mods
{
multi: true
}
)
# Cancel the entire tree of dependents
cancelIds = @_idsOfDeps ids, options.antecedents, options.dependents, @jobStatusCancellable
depsCancelled = false
if cancelIds.length > 0
depsCancelled = @_DDPMethod_jobCancel cancelIds, options
if num > 0 or depsCancelled
return true
else
console.warn "jobCancel failed"
return false
_DDPMethod_jobRestart: (ids, options) ->
check ids, Match.OneOf(Match.Where(_validId), [ Match.Where(_validId) ])
check options, Match.Optional
retries: Match.Optional(Match.Where _validIntGTEZero)
until: Match.Optional Date
antecedents: Match.Optional Boolean
dependents: Match.Optional Boolean
options ?= {}
options.retries ?= 1
options.retries = @forever if options.retries > @forever
options.dependents ?= false
options.antecedents ?= true
if _validId(ids)
ids = [ids]
return false if ids.length is 0
time = new Date()
query =
_id:
$in: ids
status:
$in: @jobStatusRestartable
mods =
$set:
status: "waiting"
progress:
completed: 0
total: 1
percent: 0
updated: time
$inc:
retries: options.retries
if logObj = @_logMessage.restarted()
mods.$push =
log: logObj
if options.until?
mods.$set.retryUntil = options.until
num = @update query, mods, {multi: true}
# Restart the entire tree of dependents
restartIds = @_idsOfDeps ids, options.antecedents, options.dependents, @jobStatusRestartable
depsRestarted = false
if restartIds.length > 0
depsRestarted = @_DDPMethod_jobRestart restartIds, options
if num > 0 or depsRestarted
@_DDPMethod_jobReady ids
return true
else
console.warn "jobRestart failed"
return false
# Job creator methods
_DDPMethod_jobSave: (doc, options) ->
check doc, _validJobDoc()
check options, Match.Optional
cancelRepeats: Match.Optional Boolean
check doc.status, Match.Where (v) ->
Match.test(v, String) and v in [ 'waiting', 'paused' ]
options ?= {}
options.cancelRepeats ?= false
doc.repeats = @forever if doc.repeats > @forever
doc.retries = @forever if doc.retries > @forever
time = new Date()
# This enables the default case of "run immediately" to
# not be impacted by a client's clock
doc.after = time if doc.after < time
doc.retryUntil = time if doc.retryUntil < time
doc.repeatUntil = time if doc.repeatUntil < time
# If doc.repeatWait is a later.js object, then don't run before
# the first valid scheduled time that occurs after doc.after
if @later? and typeof doc.repeatWait isnt 'number'
# Using a workaround to find next time after doc.after.
# See: https://github.com/vsivsi/meteor-job-collection/issues/217
schedule = @later?.schedule(doc.repeatWait)
unless schedule and next = schedule.next(2, schedule.prev(1, doc.after))[1]
console.warn "No valid available later.js times in schedule after #{doc.after}"
return null
nextDate = new Date(next)
unless nextDate <= doc.repeatUntil
console.warn "No valid available later.js times in schedule before #{doc.repeatUntil}"
return null
doc.after = nextDate
else if not @later? and doc.repeatWait isnt 'number'
console.warn "Later.js not loaded..."
return null
if doc._id
mods =
$set:
status: 'waiting'
data: doc.data
retries: doc.retries
repeatRetries: if doc.repeatRetries? then doc.repeatRetries else doc.retries + doc.retried
retryUntil: doc.retryUntil
retryWait: doc.retryWait
retryBackoff: doc.retryBackoff
repeats: doc.repeats
repeatUntil: doc.repeatUntil
repeatWait: doc.repeatWait
depends: doc.depends
priority: doc.priority
after: doc.after
updated: time
if logObj = @_logMessage.resubmitted()
mods.$push =
log: logObj
num = @update(
{
_id: doc._id
status: 'paused'
runId: null
}
mods
)
if num and @_checkDeps doc, false
@_DDPMethod_jobReady doc._id
return doc._id
else
return null
else
if doc.repeats is @forever and options.cancelRepeats
# If this is unlimited repeating job, then cancel any existing jobs of the same type
@find(
{
type: doc.type
status:
$in: @jobStatusCancellable
},
{
transform: null
}
).forEach (d) => @_DDPMethod_jobCancel d._id, {}
doc.created = time
doc.log.push @_logMessage.submitted()
doc._id = @insert doc
if doc._id and @_checkDeps doc, false
@_DDPMethod_jobReady doc._id
return doc._id
else
return null
# Worker methods
_DDPMethod_jobProgress: (id, runId, completed, total, options) ->
check id, Match.Where(_validId)
check runId, Match.Where(_validId)
check completed, Match.Where _validNumGTEZero
check total, Match.Where _validNumGTZero
check options, Match.Optional {}
options ?= {}
# Notify the worker to stop running if we are shutting down
if @stopped
return null
progress =
completed: completed
total: total
percent: 100*completed/total
check progress, Match.Where (v) ->
v.total >= v.completed and 0 <= v.percent <= 100
time = new Date()
job = @findOne { _id: id }, { fields: { workTimeout: 1 } }
mods =
$set:
progress: progress
updated: time
if job?.workTimeout?
mods.$set.expiresAfter = new Date(time.valueOf() + job.workTimeout)
num = @update(
{
_id: id
runId: runId
status: "running"
}
mods
)
if num is 1
return true
else
console.warn "jobProgress failed"
return false
_DDPMethod_jobLog: (id, runId, message, options) ->