Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-job-queue

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-job-queue - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1

2

lib/commonjs/Queue.js

@@ -1,2 +0,2 @@

var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.Queue=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _reactNative=require("react-native");var _Job=require("./models/Job");var _Uuid=require("./utils/Uuid");var _Worker=require("./Worker");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}var Queue=function(){(0,_createClass2.default)(Queue,[{key:"isRunning",get:function get(){return this.isActive;}},{key:"registeredWorkers",get:function get(){return this.workers;}}],[{key:"instance",get:function get(){if(this.queueInstance){return this.queueInstance;}else{this.queueInstance=new Queue();return this.queueInstance;}}}]);function Queue(){var _this=this;(0,_classCallCheck2.default)(this,Queue);this.queuedJobExecuter=[];this.resetActiveJob=function _callee(job){return _regenerator.default.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_this.jobStore.updateJob(_objectSpread({},job,{},{active:_Job.FALSE}));case 1:case"end":return _context.stop();}}});};this.runQueue=function _callee3(){var nextJob,nextJobs,processingJobs;return _regenerator.default.async(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:if(_this.isActive){_context3.next=3;break;}_this.finishQueue();return _context3.abrupt("return");case 3:_context3.next=5;return _regenerator.default.awrap(_this.jobStore.getNextJob());case 5:nextJob=_context3.sent;if(!_this.isJobNotEmpty(nextJob)){_context3.next=15;break;}_context3.next=9;return _regenerator.default.awrap(_this.getJobsForWorker(nextJob.workerName));case 9:nextJobs=_context3.sent;processingJobs=nextJobs.map(function _callee2(job){return _regenerator.default.async(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",_this.limitExecution(_this.excuteJob,job));case 1:case"end":return _context2.stop();}}});});_context3.next=13;return _regenerator.default.awrap(Promise.all(processingJobs));case 13:_context3.next=18;break;case 15:if(_this.isExecuting()){_context3.next=18;break;}_this.finishQueue();return _context3.abrupt("return");case 18:_this.scheduleQueue();case 19:case"end":return _context3.stop();}}});};this.limitExecution=function _callee5(executer,rawJob){return _regenerator.default.async(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt("return",new Promise(function _callee4(resolve){return _regenerator.default.async(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.next=2;return _regenerator.default.awrap(_this.enqueueJobExecuter(executer,resolve,rawJob));case 2:return _context4.abrupt("return",_context4.sent);case 3:case"end":return _context4.stop();}}});}));case 1:case"end":return _context5.stop();}}});};this.enqueueJobExecuter=function _callee6(executer,resolve,rawJob){return _regenerator.default.async(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:if(!_this.isExecuterAvailable()){_context6.next=5;break;}_context6.next=3;return _regenerator.default.awrap(_this.runExecuter(executer,resolve,rawJob));case 3:_context6.next=6;break;case 5:_this.queuedJobExecuter.push(_this.runExecuter.bind(null,executer,resolve,rawJob));case 6:case"end":return _context6.stop();}}});};this.runExecuter=function _callee7(executer,resolve,rawJob){return _regenerator.default.async(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:_context7.prev=0;_context7.next=3;return _regenerator.default.awrap(executer(rawJob));case 3:_context7.prev=3;resolve();if(!(_this.queuedJobExecuter.length>0&&_this.isExecuterAvailable())){_context7.next=8;break;}_context7.next=8;return _regenerator.default.awrap(_this.queuedJobExecuter.shift()());case 8:return _context7.finish(3);case 9:case"end":return _context7.stop();}}},null,null,[[0,,3,9]]);};this.excuteJob=function _callee8(rawJob){var worker,payload,job,promise,attempts,_JSON$parse,errors,failedAttempts,failed,metaData;return _regenerator.default.async(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:worker=_this.workers[rawJob.workerName];payload=JSON.parse(rawJob.payload);job=_objectSpread({},rawJob,{},{payload:payload});_context8.prev=3;_this.activeJobCount++;if(_this.workers[rawJob.workerName]){_context8.next=7;break;}throw new Error("Missing worker with name "+rawJob.workerName);case 7:promise=worker.execute(rawJob);_this.runningJobPromises[rawJob.id]=promise;_context8.next=11;return _regenerator.default.awrap(promise);case 11:worker.triggerSuccess(job);_this.jobStore.removeJob(rawJob);_context8.next=25;break;case 15:_context8.prev=15;_context8.t0=_context8["catch"](3);worker.triggerFailure(job,_context8.t0);attempts=rawJob.attempts;_JSON$parse=JSON.parse(rawJob.metaData),errors=_JSON$parse.errors,failedAttempts=_JSON$parse.failedAttempts;failedAttempts++;failed='';if(failedAttempts>=attempts){failed=new Date().toISOString();}metaData=JSON.stringify({errors:[].concat((0,_toConsumableArray2.default)(errors),[_context8.t0]),failedAttempts:failedAttempts});_this.jobStore.updateJob(_objectSpread({},rawJob,{},{active:_Job.FALSE,metaData:metaData,failed:failed}));case 25:_context8.prev=25;delete _this.runningJobPromises[job.id];worker.decreaseExecutionCount();worker.triggerCompletion(job);_this.executedJobs.push(rawJob);_this.activeJobCount--;return _context8.finish(25);case 32:case"end":return _context8.stop();}}},null,null,[[3,15,25,32]]);};this.jobStore=_reactNative.NativeModules.JobQueue;this.workers={};this.runningJobPromises={};this.isActive=false;this.timeoutId=0;this.executedJobs=[];this.activeJobCount=0;this.updateInterval=10;this.onQueueFinish=function(executedJobs){};this.concurrency=-1;}(0,_createClass2.default)(Queue,[{key:"getJobs",value:function getJobs(){return _regenerator.default.async(function getJobs$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:_context9.next=2;return _regenerator.default.awrap(this.jobStore.getJobs());case 2:return _context9.abrupt("return",_context9.sent);case 3:case"end":return _context9.stop();}}},null,this);}},{key:"removeJob",value:function removeJob(job){return _regenerator.default.async(function removeJob$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:_context10.next=2;return _regenerator.default.awrap(this.jobStore.removeJob(job));case 2:return _context10.abrupt("return",_context10.sent);case 3:case"end":return _context10.stop();}}},null,this);}},{key:"requeueJob",value:function requeueJob(job){return _regenerator.default.async(function requeueJob$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:_context11.next=2;return _regenerator.default.awrap(this.jobStore.updateJob(_objectSpread({},job,{failed:''})));case 2:return _context11.abrupt("return",_context11.sent);case 3:case"end":return _context11.stop();}}},null,this);}},{key:"configure",value:function configure(options){var _options$onQueueFinis=options.onQueueFinish,onQueueFinish=_options$onQueueFinis===void 0?function(executedJobs){}:_options$onQueueFinis,_options$updateInterv=options.updateInterval,updateInterval=_options$updateInterv===void 0?10:_options$updateInterv,_options$concurrency=options.concurrency,concurrency=_options$concurrency===void 0?-1:_options$concurrency;this.onQueueFinish=onQueueFinish;this.updateInterval=updateInterval;this.concurrency=concurrency;}},{key:"addWorker",value:function addWorker(worker){if(this.workers[worker.name]){throw new Error("Worker \""+worker.name+"\" already exists.");}this.workers[worker.name]=worker;}},{key:"removeWorker",value:function removeWorker(name){var deleteRelatedJobs=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;delete this.workers[name];if(deleteRelatedJobs){this.jobStore.removeJobsByWorkerName(name);}}},{key:"addJob",value:function addJob(workerName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{attempts:0,timeout:0,priority:0};var startQueue=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;var _options$attempts=options.attempts,attempts=_options$attempts===void 0?0:_options$attempts,_options$timeout=options.timeout,timeout=_options$timeout===void 0?0:_options$timeout,_options$priority=options.priority,priority=_options$priority===void 0?0:_options$priority;var id=_Uuid.Uuid.v4();var job={id:id,payload:JSON.stringify(payload||{}),metaData:JSON.stringify({failedAttempts:0,errors:[]}),active:_Job.FALSE,created:new Date().toISOString(),failed:'',workerName:workerName,attempts:attempts,timeout:timeout,priority:priority};if(!this.workers[job.workerName]){throw new Error("Missing worker with name "+job.workerName);}this.jobStore.addJob(job);if(startQueue&&!this.isActive){this.start();}return id;}},{key:"start",value:function start(){return _regenerator.default.async(function start$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:if(this.isActive){_context12.next=6;break;}this.isActive=true;this.executedJobs=[];_context12.next=5;return _regenerator.default.awrap(this.resetActiveJobs());case 5:this.scheduleQueue();case 6:case"end":return _context12.stop();}}},null,this);}},{key:"stop",value:function stop(){this.isActive=false;}},{key:"cancelJob",value:function cancelJob(jobId,exception){var promise=this.runningJobPromises[jobId];if(promise&&typeof promise[_Worker.CANCEL]==='function'){promise[_Worker.CANCEL](exception||new Error("canceled"));}else if(!promise[_Worker.CANCEL]){console.warn("Worker does not have a cancel method implemented");}else{throw new Error("Job with id "+jobId+" not currently running");}}},{key:"resetActiveJobs",value:function resetActiveJobs(){var activeMarkedJobs,resetTasks;return _regenerator.default.async(function resetActiveJobs$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.next=2;return _regenerator.default.awrap(this.jobStore.getActiveMarkedJobs());case 2:activeMarkedJobs=_context13.sent;resetTasks=activeMarkedJobs.map(this.resetActiveJob);_context13.next=6;return _regenerator.default.awrap(Promise.all(resetTasks));case 6:case"end":return _context13.stop();}}},null,this);}},{key:"scheduleQueue",value:function scheduleQueue(){this.timeoutId=setTimeout(this.runQueue,this.updateInterval);}},{key:"isJobNotEmpty",value:function isJobNotEmpty(rawJob){return Object.keys(rawJob).length>0;}},{key:"isExecuterAvailable",value:function isExecuterAvailable(){return this.concurrency<=0||this.activeJobCount<this.concurrency;}},{key:"isExecuting",value:function isExecuting(){return this.activeJobCount>0;}},{key:"finishQueue",value:function finishQueue(){this.onQueueFinish(this.executedJobs);this.isActive=false;clearTimeout(this.timeoutId);}},{key:"getJobsForWorker",value:function getJobsForWorker(workerName){var _this$workers$workerN,isBusy,availableExecuters;return _regenerator.default.async(function getJobsForWorker$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_this$workers$workerN=this.workers[workerName],isBusy=_this$workers$workerN.isBusy,availableExecuters=_this$workers$workerN.availableExecuters;if(isBusy){_context14.next=7;break;}_context14.next=4;return _regenerator.default.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 4:return _context14.abrupt("return",_context14.sent);case 7:_context14.next=9;return _regenerator.default.awrap(this.getJobsForAlternateWorker());case 9:return _context14.abrupt("return",_context14.sent);case 10:case"end":return _context14.stop();}}},null,this);}},{key:"getJobsForAlternateWorker",value:function getJobsForAlternateWorker(){var _i,_Object$keys,workerName,_this$workers$workerN2,isBusy,availableExecuters,nextJobs;return _regenerator.default.async(function getJobsForAlternateWorker$(_context15){while(1){switch(_context15.prev=_context15.next){case 0:_i=0,_Object$keys=Object.keys(this.workers);case 1:if(!(_i<_Object$keys.length)){_context15.next=14;break;}workerName=_Object$keys[_i];_this$workers$workerN2=this.workers[workerName],isBusy=_this$workers$workerN2.isBusy,availableExecuters=_this$workers$workerN2.availableExecuters;nextJobs=[];if(isBusy){_context15.next=9;break;}_context15.next=8;return _regenerator.default.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 8:nextJobs=_context15.sent;case 9:if(!(nextJobs.length>0)){_context15.next=11;break;}return _context15.abrupt("return",nextJobs);case 11:_i++;_context15.next=1;break;case 14:return _context15.abrupt("return",[]);case 15:case"end":return _context15.stop();}}},null,this);}}]);return Queue;}();exports.Queue=Queue;var _default=Queue.instance;exports.default=_default;
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.Queue=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _reactNative=require("react-native");var _Job=require("./models/Job");var _Uuid=require("./utils/Uuid");var _Worker=require("./Worker");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}var Queue=function(){(0,_createClass2.default)(Queue,[{key:"isRunning",get:function get(){return this.isActive;}},{key:"registeredWorkers",get:function get(){return this.workers;}}],[{key:"instance",get:function get(){if(this.queueInstance){return this.queueInstance;}else{this.queueInstance=new Queue();return this.queueInstance;}}}]);function Queue(){var _this=this;(0,_classCallCheck2.default)(this,Queue);this.queuedJobExecuter=[];this.resetActiveJob=function _callee(job){return _regenerator.default.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_this.jobStore.updateJob(_objectSpread({},job,{},{active:_Job.FALSE}));case 1:case"end":return _context.stop();}}});};this.runQueue=function _callee3(){var nextJob,nextJobs,processingJobs;return _regenerator.default.async(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:if(_this.isActive){_context3.next=3;break;}_this.finishQueue();return _context3.abrupt("return");case 3:_context3.next=5;return _regenerator.default.awrap(_this.jobStore.getNextJob());case 5:nextJob=_context3.sent;if(!_this.isJobNotEmpty(nextJob)){_context3.next=15;break;}_context3.next=9;return _regenerator.default.awrap(_this.getJobsForWorker(nextJob.workerName));case 9:nextJobs=_context3.sent;processingJobs=nextJobs.map(function _callee2(job){return _regenerator.default.async(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",_this.limitExecution(_this.excuteJob,job));case 1:case"end":return _context2.stop();}}});});_context3.next=13;return _regenerator.default.awrap(Promise.all(processingJobs));case 13:_context3.next=18;break;case 15:if(_this.isExecuting()){_context3.next=18;break;}_this.finishQueue();return _context3.abrupt("return");case 18:_this.scheduleQueue();case 19:case"end":return _context3.stop();}}});};this.limitExecution=function _callee5(executer,rawJob){return _regenerator.default.async(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt("return",new Promise(function _callee4(resolve){return _regenerator.default.async(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.next=2;return _regenerator.default.awrap(_this.enqueueJobExecuter(executer,resolve,rawJob));case 2:return _context4.abrupt("return",_context4.sent);case 3:case"end":return _context4.stop();}}});}));case 1:case"end":return _context5.stop();}}});};this.enqueueJobExecuter=function _callee6(executer,resolve,rawJob){return _regenerator.default.async(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:if(!_this.isExecuterAvailable()){_context6.next=5;break;}_context6.next=3;return _regenerator.default.awrap(_this.runExecuter(executer,resolve,rawJob));case 3:_context6.next=6;break;case 5:_this.queuedJobExecuter.push(_this.runExecuter.bind(null,executer,resolve,rawJob));case 6:case"end":return _context6.stop();}}});};this.runExecuter=function _callee7(executer,resolve,rawJob){return _regenerator.default.async(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:_context7.prev=0;_context7.next=3;return _regenerator.default.awrap(executer(rawJob));case 3:_context7.prev=3;resolve();if(!(_this.queuedJobExecuter.length>0&&_this.isExecuterAvailable())){_context7.next=8;break;}_context7.next=8;return _regenerator.default.awrap(_this.queuedJobExecuter.shift()());case 8:return _context7.finish(3);case 9:case"end":return _context7.stop();}}},null,null,[[0,,3,9]]);};this.excuteJob=function _callee8(rawJob){var worker,payload,job,promise,attempts,_JSON$parse,errors,failedAttempts,failed,metaData;return _regenerator.default.async(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:worker=_this.workers[rawJob.workerName];payload=JSON.parse(rawJob.payload);job=_objectSpread({},rawJob,{},{payload:payload});_context8.prev=3;_this.activeJobCount++;if(_this.workers[rawJob.workerName]){_context8.next=7;break;}throw new Error("Missing worker with name "+rawJob.workerName);case 7:promise=worker.execute(rawJob);_this.runningJobPromises[rawJob.id]=promise;_context8.next=11;return _regenerator.default.awrap(promise);case 11:worker.triggerSuccess(job);_this.jobStore.removeJob(rawJob);_context8.next=25;break;case 15:_context8.prev=15;_context8.t0=_context8["catch"](3);attempts=rawJob.attempts;_JSON$parse=JSON.parse(rawJob.metaData),errors=_JSON$parse.errors,failedAttempts=_JSON$parse.failedAttempts;failedAttempts++;failed='';if(failedAttempts>=attempts){failed=new Date().toISOString();}metaData=JSON.stringify({errors:[].concat((0,_toConsumableArray2.default)(errors),[_context8.t0]),failedAttempts:failedAttempts});worker.triggerFailure(_objectSpread({},job,{metaData:metaData,failed:failed}),_context8.t0);_this.jobStore.updateJob(_objectSpread({},rawJob,{},{active:_Job.FALSE,metaData:metaData,failed:failed}));case 25:_context8.prev=25;delete _this.runningJobPromises[job.id];worker.decreaseExecutionCount();worker.triggerCompletion(job);_this.executedJobs.push(rawJob);_this.activeJobCount--;return _context8.finish(25);case 32:case"end":return _context8.stop();}}},null,null,[[3,15,25,32]]);};this.jobStore=_reactNative.NativeModules.JobQueue;this.workers={};this.runningJobPromises={};this.isActive=false;this.timeoutId=0;this.executedJobs=[];this.activeJobCount=0;this.updateInterval=10;this.onQueueFinish=function(executedJobs){};this.concurrency=-1;}(0,_createClass2.default)(Queue,[{key:"getJobs",value:function getJobs(){return _regenerator.default.async(function getJobs$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:_context9.next=2;return _regenerator.default.awrap(this.jobStore.getJobs());case 2:return _context9.abrupt("return",_context9.sent);case 3:case"end":return _context9.stop();}}},null,this);}},{key:"removeJob",value:function removeJob(job){return _regenerator.default.async(function removeJob$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:_context10.next=2;return _regenerator.default.awrap(this.jobStore.removeJob(job));case 2:return _context10.abrupt("return",_context10.sent);case 3:case"end":return _context10.stop();}}},null,this);}},{key:"requeueJob",value:function requeueJob(job){return _regenerator.default.async(function requeueJob$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:_context11.next=2;return _regenerator.default.awrap(this.jobStore.updateJob(_objectSpread({},job,{failed:''})));case 2:return _context11.abrupt("return",_context11.sent);case 3:case"end":return _context11.stop();}}},null,this);}},{key:"configure",value:function configure(options){var _options$onQueueFinis=options.onQueueFinish,onQueueFinish=_options$onQueueFinis===void 0?function(executedJobs){}:_options$onQueueFinis,_options$updateInterv=options.updateInterval,updateInterval=_options$updateInterv===void 0?10:_options$updateInterv,_options$concurrency=options.concurrency,concurrency=_options$concurrency===void 0?-1:_options$concurrency;this.onQueueFinish=onQueueFinish;this.updateInterval=updateInterval;this.concurrency=concurrency;}},{key:"addWorker",value:function addWorker(worker){if(this.workers[worker.name]){throw new Error("Worker \""+worker.name+"\" already exists.");}this.workers[worker.name]=worker;}},{key:"removeWorker",value:function removeWorker(name){var deleteRelatedJobs=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;delete this.workers[name];if(deleteRelatedJobs){this.jobStore.removeJobsByWorkerName(name);}}},{key:"addJob",value:function addJob(workerName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{attempts:0,timeout:0,priority:0};var startQueue=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;var _options$attempts=options.attempts,attempts=_options$attempts===void 0?0:_options$attempts,_options$timeout=options.timeout,timeout=_options$timeout===void 0?0:_options$timeout,_options$priority=options.priority,priority=_options$priority===void 0?0:_options$priority;var id=_Uuid.Uuid.v4();var job={id:id,payload:JSON.stringify(payload||{}),metaData:JSON.stringify({failedAttempts:0,errors:[]}),active:_Job.FALSE,created:new Date().toISOString(),failed:'',workerName:workerName,attempts:attempts,timeout:timeout,priority:priority};if(!this.workers[job.workerName]){throw new Error("Missing worker with name "+job.workerName);}this.jobStore.addJob(job);if(startQueue&&!this.isActive){this.start();}return id;}},{key:"start",value:function start(){return _regenerator.default.async(function start$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:if(this.isActive){_context12.next=6;break;}this.isActive=true;this.executedJobs=[];_context12.next=5;return _regenerator.default.awrap(this.resetActiveJobs());case 5:this.scheduleQueue();case 6:case"end":return _context12.stop();}}},null,this);}},{key:"stop",value:function stop(){this.isActive=false;}},{key:"cancelJob",value:function cancelJob(jobId,exception){var promise=this.runningJobPromises[jobId];if(promise&&typeof promise[_Worker.CANCEL]==='function'){promise[_Worker.CANCEL](exception||new Error("canceled"));}else if(!promise[_Worker.CANCEL]){console.warn("Worker does not have a cancel method implemented");}else{throw new Error("Job with id "+jobId+" not currently running");}}},{key:"resetActiveJobs",value:function resetActiveJobs(){var activeMarkedJobs,resetTasks;return _regenerator.default.async(function resetActiveJobs$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.next=2;return _regenerator.default.awrap(this.jobStore.getActiveMarkedJobs());case 2:activeMarkedJobs=_context13.sent;resetTasks=activeMarkedJobs.map(this.resetActiveJob);_context13.next=6;return _regenerator.default.awrap(Promise.all(resetTasks));case 6:case"end":return _context13.stop();}}},null,this);}},{key:"scheduleQueue",value:function scheduleQueue(){this.timeoutId=setTimeout(this.runQueue,this.updateInterval);}},{key:"isJobNotEmpty",value:function isJobNotEmpty(rawJob){return Object.keys(rawJob).length>0;}},{key:"isExecuterAvailable",value:function isExecuterAvailable(){return this.concurrency<=0||this.activeJobCount<this.concurrency;}},{key:"isExecuting",value:function isExecuting(){return this.activeJobCount>0;}},{key:"finishQueue",value:function finishQueue(){this.onQueueFinish(this.executedJobs);this.isActive=false;clearTimeout(this.timeoutId);}},{key:"getJobsForWorker",value:function getJobsForWorker(workerName){var _this$workers$workerN,isBusy,availableExecuters;return _regenerator.default.async(function getJobsForWorker$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_this$workers$workerN=this.workers[workerName],isBusy=_this$workers$workerN.isBusy,availableExecuters=_this$workers$workerN.availableExecuters;if(isBusy){_context14.next=7;break;}_context14.next=4;return _regenerator.default.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 4:return _context14.abrupt("return",_context14.sent);case 7:_context14.next=9;return _regenerator.default.awrap(this.getJobsForAlternateWorker());case 9:return _context14.abrupt("return",_context14.sent);case 10:case"end":return _context14.stop();}}},null,this);}},{key:"getJobsForAlternateWorker",value:function getJobsForAlternateWorker(){var _i,_Object$keys,workerName,_this$workers$workerN2,isBusy,availableExecuters,nextJobs;return _regenerator.default.async(function getJobsForAlternateWorker$(_context15){while(1){switch(_context15.prev=_context15.next){case 0:_i=0,_Object$keys=Object.keys(this.workers);case 1:if(!(_i<_Object$keys.length)){_context15.next=14;break;}workerName=_Object$keys[_i];_this$workers$workerN2=this.workers[workerName],isBusy=_this$workers$workerN2.isBusy,availableExecuters=_this$workers$workerN2.availableExecuters;nextJobs=[];if(isBusy){_context15.next=9;break;}_context15.next=8;return _regenerator.default.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 8:nextJobs=_context15.sent;case 9:if(!(nextJobs.length>0)){_context15.next=11;break;}return _context15.abrupt("return",nextJobs);case 11:_i++;_context15.next=1;break;case 14:return _context15.abrupt("return",[]);case 15:case"end":return _context15.stop();}}},null,this);}}]);return Queue;}();exports.Queue=Queue;var _default=Queue.instance;exports.default=_default;
//# sourceMappingURL=Queue.js.map

@@ -1,2 +0,2 @@

import _toConsumableArray from"@babel/runtime/helpers/toConsumableArray";import _regeneratorRuntime from"@babel/runtime/regenerator";import _defineProperty from"@babel/runtime/helpers/defineProperty";import _classCallCheck from"@babel/runtime/helpers/classCallCheck";import _createClass from"@babel/runtime/helpers/createClass";function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){_defineProperty(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}import{NativeModules}from'react-native';import{FALSE}from'./models/Job';import{Uuid}from'./utils/Uuid';import{CANCEL}from'./Worker';export var Queue=function(){_createClass(Queue,[{key:"isRunning",get:function get(){return this.isActive;}},{key:"registeredWorkers",get:function get(){return this.workers;}}],[{key:"instance",get:function get(){if(this.queueInstance){return this.queueInstance;}else{this.queueInstance=new Queue();return this.queueInstance;}}}]);function Queue(){var _this=this;_classCallCheck(this,Queue);this.queuedJobExecuter=[];this.resetActiveJob=function _callee(job){return _regeneratorRuntime.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_this.jobStore.updateJob(_objectSpread({},job,{},{active:FALSE}));case 1:case"end":return _context.stop();}}});};this.runQueue=function _callee3(){var nextJob,nextJobs,processingJobs;return _regeneratorRuntime.async(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:if(_this.isActive){_context3.next=3;break;}_this.finishQueue();return _context3.abrupt("return");case 3:_context3.next=5;return _regeneratorRuntime.awrap(_this.jobStore.getNextJob());case 5:nextJob=_context3.sent;if(!_this.isJobNotEmpty(nextJob)){_context3.next=15;break;}_context3.next=9;return _regeneratorRuntime.awrap(_this.getJobsForWorker(nextJob.workerName));case 9:nextJobs=_context3.sent;processingJobs=nextJobs.map(function _callee2(job){return _regeneratorRuntime.async(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",_this.limitExecution(_this.excuteJob,job));case 1:case"end":return _context2.stop();}}});});_context3.next=13;return _regeneratorRuntime.awrap(Promise.all(processingJobs));case 13:_context3.next=18;break;case 15:if(_this.isExecuting()){_context3.next=18;break;}_this.finishQueue();return _context3.abrupt("return");case 18:_this.scheduleQueue();case 19:case"end":return _context3.stop();}}});};this.limitExecution=function _callee5(executer,rawJob){return _regeneratorRuntime.async(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt("return",new Promise(function _callee4(resolve){return _regeneratorRuntime.async(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.next=2;return _regeneratorRuntime.awrap(_this.enqueueJobExecuter(executer,resolve,rawJob));case 2:return _context4.abrupt("return",_context4.sent);case 3:case"end":return _context4.stop();}}});}));case 1:case"end":return _context5.stop();}}});};this.enqueueJobExecuter=function _callee6(executer,resolve,rawJob){return _regeneratorRuntime.async(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:if(!_this.isExecuterAvailable()){_context6.next=5;break;}_context6.next=3;return _regeneratorRuntime.awrap(_this.runExecuter(executer,resolve,rawJob));case 3:_context6.next=6;break;case 5:_this.queuedJobExecuter.push(_this.runExecuter.bind(null,executer,resolve,rawJob));case 6:case"end":return _context6.stop();}}});};this.runExecuter=function _callee7(executer,resolve,rawJob){return _regeneratorRuntime.async(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:_context7.prev=0;_context7.next=3;return _regeneratorRuntime.awrap(executer(rawJob));case 3:_context7.prev=3;resolve();if(!(_this.queuedJobExecuter.length>0&&_this.isExecuterAvailable())){_context7.next=8;break;}_context7.next=8;return _regeneratorRuntime.awrap(_this.queuedJobExecuter.shift()());case 8:return _context7.finish(3);case 9:case"end":return _context7.stop();}}},null,null,[[0,,3,9]]);};this.excuteJob=function _callee8(rawJob){var worker,payload,job,promise,attempts,_JSON$parse,errors,failedAttempts,failed,metaData;return _regeneratorRuntime.async(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:worker=_this.workers[rawJob.workerName];payload=JSON.parse(rawJob.payload);job=_objectSpread({},rawJob,{},{payload:payload});_context8.prev=3;_this.activeJobCount++;if(_this.workers[rawJob.workerName]){_context8.next=7;break;}throw new Error("Missing worker with name "+rawJob.workerName);case 7:promise=worker.execute(rawJob);_this.runningJobPromises[rawJob.id]=promise;_context8.next=11;return _regeneratorRuntime.awrap(promise);case 11:worker.triggerSuccess(job);_this.jobStore.removeJob(rawJob);_context8.next=25;break;case 15:_context8.prev=15;_context8.t0=_context8["catch"](3);worker.triggerFailure(job,_context8.t0);attempts=rawJob.attempts;_JSON$parse=JSON.parse(rawJob.metaData),errors=_JSON$parse.errors,failedAttempts=_JSON$parse.failedAttempts;failedAttempts++;failed='';if(failedAttempts>=attempts){failed=new Date().toISOString();}metaData=JSON.stringify({errors:[].concat(_toConsumableArray(errors),[_context8.t0]),failedAttempts:failedAttempts});_this.jobStore.updateJob(_objectSpread({},rawJob,{},{active:FALSE,metaData:metaData,failed:failed}));case 25:_context8.prev=25;delete _this.runningJobPromises[job.id];worker.decreaseExecutionCount();worker.triggerCompletion(job);_this.executedJobs.push(rawJob);_this.activeJobCount--;return _context8.finish(25);case 32:case"end":return _context8.stop();}}},null,null,[[3,15,25,32]]);};this.jobStore=NativeModules.JobQueue;this.workers={};this.runningJobPromises={};this.isActive=false;this.timeoutId=0;this.executedJobs=[];this.activeJobCount=0;this.updateInterval=10;this.onQueueFinish=function(executedJobs){};this.concurrency=-1;}_createClass(Queue,[{key:"getJobs",value:function getJobs(){return _regeneratorRuntime.async(function getJobs$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:_context9.next=2;return _regeneratorRuntime.awrap(this.jobStore.getJobs());case 2:return _context9.abrupt("return",_context9.sent);case 3:case"end":return _context9.stop();}}},null,this);}},{key:"removeJob",value:function removeJob(job){return _regeneratorRuntime.async(function removeJob$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:_context10.next=2;return _regeneratorRuntime.awrap(this.jobStore.removeJob(job));case 2:return _context10.abrupt("return",_context10.sent);case 3:case"end":return _context10.stop();}}},null,this);}},{key:"requeueJob",value:function requeueJob(job){return _regeneratorRuntime.async(function requeueJob$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:_context11.next=2;return _regeneratorRuntime.awrap(this.jobStore.updateJob(_objectSpread({},job,{failed:''})));case 2:return _context11.abrupt("return",_context11.sent);case 3:case"end":return _context11.stop();}}},null,this);}},{key:"configure",value:function configure(options){var _options$onQueueFinis=options.onQueueFinish,onQueueFinish=_options$onQueueFinis===void 0?function(executedJobs){}:_options$onQueueFinis,_options$updateInterv=options.updateInterval,updateInterval=_options$updateInterv===void 0?10:_options$updateInterv,_options$concurrency=options.concurrency,concurrency=_options$concurrency===void 0?-1:_options$concurrency;this.onQueueFinish=onQueueFinish;this.updateInterval=updateInterval;this.concurrency=concurrency;}},{key:"addWorker",value:function addWorker(worker){if(this.workers[worker.name]){throw new Error("Worker \""+worker.name+"\" already exists.");}this.workers[worker.name]=worker;}},{key:"removeWorker",value:function removeWorker(name){var deleteRelatedJobs=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;delete this.workers[name];if(deleteRelatedJobs){this.jobStore.removeJobsByWorkerName(name);}}},{key:"addJob",value:function addJob(workerName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{attempts:0,timeout:0,priority:0};var startQueue=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;var _options$attempts=options.attempts,attempts=_options$attempts===void 0?0:_options$attempts,_options$timeout=options.timeout,timeout=_options$timeout===void 0?0:_options$timeout,_options$priority=options.priority,priority=_options$priority===void 0?0:_options$priority;var id=Uuid.v4();var job={id:id,payload:JSON.stringify(payload||{}),metaData:JSON.stringify({failedAttempts:0,errors:[]}),active:FALSE,created:new Date().toISOString(),failed:'',workerName:workerName,attempts:attempts,timeout:timeout,priority:priority};if(!this.workers[job.workerName]){throw new Error("Missing worker with name "+job.workerName);}this.jobStore.addJob(job);if(startQueue&&!this.isActive){this.start();}return id;}},{key:"start",value:function start(){return _regeneratorRuntime.async(function start$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:if(this.isActive){_context12.next=6;break;}this.isActive=true;this.executedJobs=[];_context12.next=5;return _regeneratorRuntime.awrap(this.resetActiveJobs());case 5:this.scheduleQueue();case 6:case"end":return _context12.stop();}}},null,this);}},{key:"stop",value:function stop(){this.isActive=false;}},{key:"cancelJob",value:function cancelJob(jobId,exception){var promise=this.runningJobPromises[jobId];if(promise&&typeof promise[CANCEL]==='function'){promise[CANCEL](exception||new Error("canceled"));}else if(!promise[CANCEL]){console.warn("Worker does not have a cancel method implemented");}else{throw new Error("Job with id "+jobId+" not currently running");}}},{key:"resetActiveJobs",value:function resetActiveJobs(){var activeMarkedJobs,resetTasks;return _regeneratorRuntime.async(function resetActiveJobs$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.next=2;return _regeneratorRuntime.awrap(this.jobStore.getActiveMarkedJobs());case 2:activeMarkedJobs=_context13.sent;resetTasks=activeMarkedJobs.map(this.resetActiveJob);_context13.next=6;return _regeneratorRuntime.awrap(Promise.all(resetTasks));case 6:case"end":return _context13.stop();}}},null,this);}},{key:"scheduleQueue",value:function scheduleQueue(){this.timeoutId=setTimeout(this.runQueue,this.updateInterval);}},{key:"isJobNotEmpty",value:function isJobNotEmpty(rawJob){return Object.keys(rawJob).length>0;}},{key:"isExecuterAvailable",value:function isExecuterAvailable(){return this.concurrency<=0||this.activeJobCount<this.concurrency;}},{key:"isExecuting",value:function isExecuting(){return this.activeJobCount>0;}},{key:"finishQueue",value:function finishQueue(){this.onQueueFinish(this.executedJobs);this.isActive=false;clearTimeout(this.timeoutId);}},{key:"getJobsForWorker",value:function getJobsForWorker(workerName){var _this$workers$workerN,isBusy,availableExecuters;return _regeneratorRuntime.async(function getJobsForWorker$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_this$workers$workerN=this.workers[workerName],isBusy=_this$workers$workerN.isBusy,availableExecuters=_this$workers$workerN.availableExecuters;if(isBusy){_context14.next=7;break;}_context14.next=4;return _regeneratorRuntime.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 4:return _context14.abrupt("return",_context14.sent);case 7:_context14.next=9;return _regeneratorRuntime.awrap(this.getJobsForAlternateWorker());case 9:return _context14.abrupt("return",_context14.sent);case 10:case"end":return _context14.stop();}}},null,this);}},{key:"getJobsForAlternateWorker",value:function getJobsForAlternateWorker(){var _i,_Object$keys,workerName,_this$workers$workerN2,isBusy,availableExecuters,nextJobs;return _regeneratorRuntime.async(function getJobsForAlternateWorker$(_context15){while(1){switch(_context15.prev=_context15.next){case 0:_i=0,_Object$keys=Object.keys(this.workers);case 1:if(!(_i<_Object$keys.length)){_context15.next=14;break;}workerName=_Object$keys[_i];_this$workers$workerN2=this.workers[workerName],isBusy=_this$workers$workerN2.isBusy,availableExecuters=_this$workers$workerN2.availableExecuters;nextJobs=[];if(isBusy){_context15.next=9;break;}_context15.next=8;return _regeneratorRuntime.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 8:nextJobs=_context15.sent;case 9:if(!(nextJobs.length>0)){_context15.next=11;break;}return _context15.abrupt("return",nextJobs);case 11:_i++;_context15.next=1;break;case 14:return _context15.abrupt("return",[]);case 15:case"end":return _context15.stop();}}},null,this);}}]);return Queue;}();export default Queue.instance;
import _toConsumableArray from"@babel/runtime/helpers/toConsumableArray";import _regeneratorRuntime from"@babel/runtime/regenerator";import _defineProperty from"@babel/runtime/helpers/defineProperty";import _classCallCheck from"@babel/runtime/helpers/classCallCheck";import _createClass from"@babel/runtime/helpers/createClass";function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){_defineProperty(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}import{NativeModules}from'react-native';import{FALSE}from'./models/Job';import{Uuid}from'./utils/Uuid';import{CANCEL}from'./Worker';export var Queue=function(){_createClass(Queue,[{key:"isRunning",get:function get(){return this.isActive;}},{key:"registeredWorkers",get:function get(){return this.workers;}}],[{key:"instance",get:function get(){if(this.queueInstance){return this.queueInstance;}else{this.queueInstance=new Queue();return this.queueInstance;}}}]);function Queue(){var _this=this;_classCallCheck(this,Queue);this.queuedJobExecuter=[];this.resetActiveJob=function _callee(job){return _regeneratorRuntime.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_this.jobStore.updateJob(_objectSpread({},job,{},{active:FALSE}));case 1:case"end":return _context.stop();}}});};this.runQueue=function _callee3(){var nextJob,nextJobs,processingJobs;return _regeneratorRuntime.async(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:if(_this.isActive){_context3.next=3;break;}_this.finishQueue();return _context3.abrupt("return");case 3:_context3.next=5;return _regeneratorRuntime.awrap(_this.jobStore.getNextJob());case 5:nextJob=_context3.sent;if(!_this.isJobNotEmpty(nextJob)){_context3.next=15;break;}_context3.next=9;return _regeneratorRuntime.awrap(_this.getJobsForWorker(nextJob.workerName));case 9:nextJobs=_context3.sent;processingJobs=nextJobs.map(function _callee2(job){return _regeneratorRuntime.async(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",_this.limitExecution(_this.excuteJob,job));case 1:case"end":return _context2.stop();}}});});_context3.next=13;return _regeneratorRuntime.awrap(Promise.all(processingJobs));case 13:_context3.next=18;break;case 15:if(_this.isExecuting()){_context3.next=18;break;}_this.finishQueue();return _context3.abrupt("return");case 18:_this.scheduleQueue();case 19:case"end":return _context3.stop();}}});};this.limitExecution=function _callee5(executer,rawJob){return _regeneratorRuntime.async(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt("return",new Promise(function _callee4(resolve){return _regeneratorRuntime.async(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.next=2;return _regeneratorRuntime.awrap(_this.enqueueJobExecuter(executer,resolve,rawJob));case 2:return _context4.abrupt("return",_context4.sent);case 3:case"end":return _context4.stop();}}});}));case 1:case"end":return _context5.stop();}}});};this.enqueueJobExecuter=function _callee6(executer,resolve,rawJob){return _regeneratorRuntime.async(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:if(!_this.isExecuterAvailable()){_context6.next=5;break;}_context6.next=3;return _regeneratorRuntime.awrap(_this.runExecuter(executer,resolve,rawJob));case 3:_context6.next=6;break;case 5:_this.queuedJobExecuter.push(_this.runExecuter.bind(null,executer,resolve,rawJob));case 6:case"end":return _context6.stop();}}});};this.runExecuter=function _callee7(executer,resolve,rawJob){return _regeneratorRuntime.async(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:_context7.prev=0;_context7.next=3;return _regeneratorRuntime.awrap(executer(rawJob));case 3:_context7.prev=3;resolve();if(!(_this.queuedJobExecuter.length>0&&_this.isExecuterAvailable())){_context7.next=8;break;}_context7.next=8;return _regeneratorRuntime.awrap(_this.queuedJobExecuter.shift()());case 8:return _context7.finish(3);case 9:case"end":return _context7.stop();}}},null,null,[[0,,3,9]]);};this.excuteJob=function _callee8(rawJob){var worker,payload,job,promise,attempts,_JSON$parse,errors,failedAttempts,failed,metaData;return _regeneratorRuntime.async(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:worker=_this.workers[rawJob.workerName];payload=JSON.parse(rawJob.payload);job=_objectSpread({},rawJob,{},{payload:payload});_context8.prev=3;_this.activeJobCount++;if(_this.workers[rawJob.workerName]){_context8.next=7;break;}throw new Error("Missing worker with name "+rawJob.workerName);case 7:promise=worker.execute(rawJob);_this.runningJobPromises[rawJob.id]=promise;_context8.next=11;return _regeneratorRuntime.awrap(promise);case 11:worker.triggerSuccess(job);_this.jobStore.removeJob(rawJob);_context8.next=25;break;case 15:_context8.prev=15;_context8.t0=_context8["catch"](3);attempts=rawJob.attempts;_JSON$parse=JSON.parse(rawJob.metaData),errors=_JSON$parse.errors,failedAttempts=_JSON$parse.failedAttempts;failedAttempts++;failed='';if(failedAttempts>=attempts){failed=new Date().toISOString();}metaData=JSON.stringify({errors:[].concat(_toConsumableArray(errors),[_context8.t0]),failedAttempts:failedAttempts});worker.triggerFailure(_objectSpread({},job,{metaData:metaData,failed:failed}),_context8.t0);_this.jobStore.updateJob(_objectSpread({},rawJob,{},{active:FALSE,metaData:metaData,failed:failed}));case 25:_context8.prev=25;delete _this.runningJobPromises[job.id];worker.decreaseExecutionCount();worker.triggerCompletion(job);_this.executedJobs.push(rawJob);_this.activeJobCount--;return _context8.finish(25);case 32:case"end":return _context8.stop();}}},null,null,[[3,15,25,32]]);};this.jobStore=NativeModules.JobQueue;this.workers={};this.runningJobPromises={};this.isActive=false;this.timeoutId=0;this.executedJobs=[];this.activeJobCount=0;this.updateInterval=10;this.onQueueFinish=function(executedJobs){};this.concurrency=-1;}_createClass(Queue,[{key:"getJobs",value:function getJobs(){return _regeneratorRuntime.async(function getJobs$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:_context9.next=2;return _regeneratorRuntime.awrap(this.jobStore.getJobs());case 2:return _context9.abrupt("return",_context9.sent);case 3:case"end":return _context9.stop();}}},null,this);}},{key:"removeJob",value:function removeJob(job){return _regeneratorRuntime.async(function removeJob$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:_context10.next=2;return _regeneratorRuntime.awrap(this.jobStore.removeJob(job));case 2:return _context10.abrupt("return",_context10.sent);case 3:case"end":return _context10.stop();}}},null,this);}},{key:"requeueJob",value:function requeueJob(job){return _regeneratorRuntime.async(function requeueJob$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:_context11.next=2;return _regeneratorRuntime.awrap(this.jobStore.updateJob(_objectSpread({},job,{failed:''})));case 2:return _context11.abrupt("return",_context11.sent);case 3:case"end":return _context11.stop();}}},null,this);}},{key:"configure",value:function configure(options){var _options$onQueueFinis=options.onQueueFinish,onQueueFinish=_options$onQueueFinis===void 0?function(executedJobs){}:_options$onQueueFinis,_options$updateInterv=options.updateInterval,updateInterval=_options$updateInterv===void 0?10:_options$updateInterv,_options$concurrency=options.concurrency,concurrency=_options$concurrency===void 0?-1:_options$concurrency;this.onQueueFinish=onQueueFinish;this.updateInterval=updateInterval;this.concurrency=concurrency;}},{key:"addWorker",value:function addWorker(worker){if(this.workers[worker.name]){throw new Error("Worker \""+worker.name+"\" already exists.");}this.workers[worker.name]=worker;}},{key:"removeWorker",value:function removeWorker(name){var deleteRelatedJobs=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;delete this.workers[name];if(deleteRelatedJobs){this.jobStore.removeJobsByWorkerName(name);}}},{key:"addJob",value:function addJob(workerName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{attempts:0,timeout:0,priority:0};var startQueue=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;var _options$attempts=options.attempts,attempts=_options$attempts===void 0?0:_options$attempts,_options$timeout=options.timeout,timeout=_options$timeout===void 0?0:_options$timeout,_options$priority=options.priority,priority=_options$priority===void 0?0:_options$priority;var id=Uuid.v4();var job={id:id,payload:JSON.stringify(payload||{}),metaData:JSON.stringify({failedAttempts:0,errors:[]}),active:FALSE,created:new Date().toISOString(),failed:'',workerName:workerName,attempts:attempts,timeout:timeout,priority:priority};if(!this.workers[job.workerName]){throw new Error("Missing worker with name "+job.workerName);}this.jobStore.addJob(job);if(startQueue&&!this.isActive){this.start();}return id;}},{key:"start",value:function start(){return _regeneratorRuntime.async(function start$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:if(this.isActive){_context12.next=6;break;}this.isActive=true;this.executedJobs=[];_context12.next=5;return _regeneratorRuntime.awrap(this.resetActiveJobs());case 5:this.scheduleQueue();case 6:case"end":return _context12.stop();}}},null,this);}},{key:"stop",value:function stop(){this.isActive=false;}},{key:"cancelJob",value:function cancelJob(jobId,exception){var promise=this.runningJobPromises[jobId];if(promise&&typeof promise[CANCEL]==='function'){promise[CANCEL](exception||new Error("canceled"));}else if(!promise[CANCEL]){console.warn("Worker does not have a cancel method implemented");}else{throw new Error("Job with id "+jobId+" not currently running");}}},{key:"resetActiveJobs",value:function resetActiveJobs(){var activeMarkedJobs,resetTasks;return _regeneratorRuntime.async(function resetActiveJobs$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.next=2;return _regeneratorRuntime.awrap(this.jobStore.getActiveMarkedJobs());case 2:activeMarkedJobs=_context13.sent;resetTasks=activeMarkedJobs.map(this.resetActiveJob);_context13.next=6;return _regeneratorRuntime.awrap(Promise.all(resetTasks));case 6:case"end":return _context13.stop();}}},null,this);}},{key:"scheduleQueue",value:function scheduleQueue(){this.timeoutId=setTimeout(this.runQueue,this.updateInterval);}},{key:"isJobNotEmpty",value:function isJobNotEmpty(rawJob){return Object.keys(rawJob).length>0;}},{key:"isExecuterAvailable",value:function isExecuterAvailable(){return this.concurrency<=0||this.activeJobCount<this.concurrency;}},{key:"isExecuting",value:function isExecuting(){return this.activeJobCount>0;}},{key:"finishQueue",value:function finishQueue(){this.onQueueFinish(this.executedJobs);this.isActive=false;clearTimeout(this.timeoutId);}},{key:"getJobsForWorker",value:function getJobsForWorker(workerName){var _this$workers$workerN,isBusy,availableExecuters;return _regeneratorRuntime.async(function getJobsForWorker$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_this$workers$workerN=this.workers[workerName],isBusy=_this$workers$workerN.isBusy,availableExecuters=_this$workers$workerN.availableExecuters;if(isBusy){_context14.next=7;break;}_context14.next=4;return _regeneratorRuntime.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 4:return _context14.abrupt("return",_context14.sent);case 7:_context14.next=9;return _regeneratorRuntime.awrap(this.getJobsForAlternateWorker());case 9:return _context14.abrupt("return",_context14.sent);case 10:case"end":return _context14.stop();}}},null,this);}},{key:"getJobsForAlternateWorker",value:function getJobsForAlternateWorker(){var _i,_Object$keys,workerName,_this$workers$workerN2,isBusy,availableExecuters,nextJobs;return _regeneratorRuntime.async(function getJobsForAlternateWorker$(_context15){while(1){switch(_context15.prev=_context15.next){case 0:_i=0,_Object$keys=Object.keys(this.workers);case 1:if(!(_i<_Object$keys.length)){_context15.next=14;break;}workerName=_Object$keys[_i];_this$workers$workerN2=this.workers[workerName],isBusy=_this$workers$workerN2.isBusy,availableExecuters=_this$workers$workerN2.availableExecuters;nextJobs=[];if(isBusy){_context15.next=9;break;}_context15.next=8;return _regeneratorRuntime.awrap(this.jobStore.getJobsForWorker(workerName,availableExecuters));case 8:nextJobs=_context15.sent;case 9:if(!(nextJobs.length>0)){_context15.next=11;break;}return _context15.abrupt("return",nextJobs);case 11:_i++;_context15.next=1;break;case 14:return _context15.abrupt("return",[]);case 15:case"end":return _context15.stop();}}},null,this);}}]);return Queue;}();export default Queue.instance;
//# sourceMappingURL=Queue.js.map
{
"name": "react-native-job-queue",
"title": "React Native Job Queue",
"version": "0.4.0",
"version": "0.4.1",
"description": "TODO",

@@ -6,0 +6,0 @@ "react-native": "src/index.ts",

@@ -148,8 +148,22 @@ import { Job } from '../models/Job';

queue.configure({ onQueueFinish: onQueueFinish });
queue.addWorker(
new Worker<Payload>('testWorker', executer, { concurrency: 1, onFailure: onError })
);
queue.addWorker(new Worker<Payload>('testWorker', executer, { concurrency: 1, onFailure: onError }));
queue.addJob('testWorker', { test: '1' }, { attempts: 0, timeout: 5, priority: 0 }, false);
queue.start();
});
it('trigger onFailure', (done) => {
const executer = async () => {
throw new Error('This is an error');
};
const onFailure = (job: Job<Payload>, _: Error) => {
try {
expect(job.failed).not.toEqual('');
done();
} catch (error) {
done(error);
}
};
queue.addWorker(new Worker<Payload>('testWorker', executer, { concurrency: 1, onFailure }));
queue.addJob('testWorker', { test: '1' }, { attempts: 0, timeout: 5, priority: 0 }, false);
queue.start();
});
});

@@ -330,3 +330,2 @@ import { NativeModules } from 'react-native';

} catch (error) {
worker.triggerFailure(job, error);
const { attempts } = rawJob;

@@ -341,2 +340,3 @@ // tslint:disable-next-line: prefer-const

const metaData = JSON.stringify({ errors: [...errors, error], failedAttempts });
worker.triggerFailure({ ...job, metaData, failed }, error);
this.jobStore.updateJob({ ...rawJob, ...{ active: FALSE, metaData, failed } });

@@ -343,0 +343,0 @@ } finally {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc