-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to optimize memory usage, step 1
- Loading branch information
Simon Grondin
committed
May 8, 2019
1 parent
a94fbfd
commit 3d76c11
Showing
10 changed files
with
823 additions
and
642 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"use strict"; | ||
|
||
var Job, parser; | ||
parser = require("./parser"); | ||
Job = class Job { | ||
constructor(task, args, options, jobDefaults, Promise, NUM_PRIORITIES, DEFAULT_PRIORITY) { | ||
this.task = task; | ||
this.args = args; | ||
this.Promise = Promise; | ||
this.options = parser.load(options, jobDefaults); | ||
this.options.priority = this._sanitizePriority(this.options.priority); | ||
|
||
if (this.options.id === jobDefaults.id) { | ||
this.options.id = `${this.options.id}-${this._randomIndex()}`; | ||
} | ||
|
||
this.promise = new this.Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
this.retryCount = 0; | ||
} | ||
|
||
_sanitizePriority(priority, NUM_PRIORITIES, DEFAULT_PRIORITY) { | ||
var sProperty; | ||
sProperty = ~~priority !== priority ? DEFAULT_PRIORITY : priority; | ||
|
||
if (sProperty < 0) { | ||
return 0; | ||
} else if (sProperty > NUM_PRIORITIES - 1) { | ||
return NUM_PRIORITIES - 1; | ||
} else { | ||
return sProperty; | ||
} | ||
} | ||
|
||
_randomIndex() { | ||
return Math.random().toString(36).slice(2); | ||
} | ||
|
||
execute(cb) { | ||
var e, returned; | ||
|
||
returned = function () { | ||
try { | ||
return this.task(...this.args); | ||
} catch (error) { | ||
e = error; | ||
return this.Promise.reject(e); | ||
} | ||
}.call(this); | ||
|
||
return (!((returned != null ? returned.then : void 0) != null && typeof returned.then === "function") ? this.Promise.resolve(returned) : returned).then(function (passed) { | ||
return cb(null, passed); | ||
}).catch(function (err) { | ||
return cb(err); | ||
}); | ||
} | ||
|
||
done(err, passed) { | ||
if (err != null) { | ||
return this.reject(err); | ||
} else { | ||
return this.resolve(passed); | ||
} | ||
} | ||
|
||
}; | ||
module.exports = Job; |
Oops, something went wrong.