Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
v1.0.8
Browse files Browse the repository at this point in the history
 - Fix potential memory leak
 - Second parameter of `setTimeout`, `setInterval` and `setImmediate`
now is optional
 - Use `Meteor.setInterval` instead of `Meteor.setTimeout` in lib
codebase
  • Loading branch information
dr-dimitru committed Nov 25, 2016
1 parent 49cbf06 commit 5b2196d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mongo-id@1.0.5
npm-mongo@1.5.45
observe-sequence@1.0.12
ordered-dict@1.0.8
ostrio:cron-jobs@1.0.7
ostrio:cron-jobs@1.0.8
promise@0.8.3
random@1.0.10
reactive-var@1.0.10
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CRON.setInterval(taskB, 60*60*1000, 'taskB');
CRON.setInterval(task1, 60*60*1000, 'task1');
```

Note: This library uses on function names and its contents, so after deploying new version of your application to server, you need to clean up old tasks:
Note: To cleanup old tasks via MongoDB use next query pattern:
```js
// Run directly in MongoDB console:
db.getCollection('__CRONjobs__').remove({});
Expand Down
49 changes: 18 additions & 31 deletions cron-jobs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ class CRONjob
check resetOnInit, Boolean
check @zombieTime, Number

self = @
@collection = new Mongo.Collection "__CRONjobs__#{prefix}"
@collection._ensureIndex {uid: 1}, {background: true, unique: true}
@collection._ensureIndex {uid: 1, inProgress: 1}
@collection._ensureIndex {executeAt: 1, inProgress: 1}, {background: true}

if resetOnInit
@collection.update {}, {$set: inProgress: false}, NoOp
@collection.remove {isInterval: false}, NoOp
@tasks = {}
@__poll()

__poll: ->
self = @
Meteor.setTimeout ->
Meteor.setInterval ->
try
cursor = self.collection.find
$or: [{
Expand All @@ -30,32 +29,23 @@ class CRONjob
inProgress: true
}]

count = cursor.count()
if count > 0
i = 0

cursor.forEach (task) ->
++i
if self.tasks?[task.uid]
process.nextTick ->
bound ->
self.__execute task
return
cursor.forEach (task) ->
if self.tasks?[task.uid]
process.nextTick ->
bound ->
self.__execute task
return
if i is count
self.__poll()
return
else
self.__poll()
return
return
catch
self.__poll()
return
return
, Math.random() * (850) + 150
, Math.random() * (500) + 50

setInterval: (func, delay, uid) ->
check func, Function
check delay, Number
check uid, String
check uid, Match.Optional String

throw new Meteor.Error 500, '[ostrio:cron-jobs] [setInterval] delay must be positive Number!' if delay < 0

Expand All @@ -71,7 +61,7 @@ class CRONjob
setTimeout: (func, delay, uid) ->
check func, Function
check delay, Number
check uid, String
check uid, Match.Optional String

throw new Meteor.Error 500, '[ostrio:cron-jobs] [setTimeout] delay must be positive Number!' if delay < 0

Expand All @@ -86,7 +76,7 @@ class CRONjob

setImmediate: (func, uid) ->
check func, Function
check uid, String
check uid, Match.Optional String

if uid
uid += 'setImmediate'
Expand All @@ -97,11 +87,8 @@ class CRONjob
@__addTask uid, false, 0
return uid

clearInterval: ->
@__clear.apply @, arguments

clearTimeout: ->
@__clear.apply @, arguments
clearInterval: -> @__clear.apply @, arguments
clearTimeout: -> @__clear.apply @, arguments

__clear: (uid) ->
check uid, String
Expand Down Expand Up @@ -146,7 +133,7 @@ class CRONjob

__execute: (task) ->
self = @
@collection.update {uid: task.uid}, {$set: inProgress: true}, ->
@collection.update {uid: task.uid, inProgress: false}, {$set: inProgress: true}, ->
if self.tasks?[task.uid]
ready = ->
if task.isInterval is true
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'ostrio:cron-jobs',
version: '1.0.7',
version: '1.0.8',
summary: 'Task scheduler. With support of cluster or multiple NodeJS instances.',
git: 'https://github.com/VeliovGroup/Meteor-CRON-jobs',
documentation: 'README.md'
Expand Down

0 comments on commit 5b2196d

Please sign in to comment.