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

pomelo-scheduler

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pomelo-scheduler - npm Package Compare versions

Comparing version 0.3.9 to 0.4.0

182

lib/cronTrigger.js

@@ -13,3 +13,10 @@ /**

var Limit = [[0,59],[0,59],[0,24],[1,31],[0,11],[0,6]];
var Limit = [
[0, 59],
[0, 59],
[0, 24],
[1, 31],
[0, 11],
[0, 6]
];

@@ -20,3 +27,3 @@ /**

*/
var CronTrigger = function(trigger, job){
var CronTrigger = function(trigger, job) {
this.trigger = this.decodeTrigger(trigger);

@@ -34,3 +41,3 @@

*/
pro.excuteTime = function(){
pro.excuteTime = function() {
return this.nextTime;

@@ -44,5 +51,5 @@ };

*/
pro.nextExcuteTime = function(time){
pro.nextExcuteTime = function(time) {
//add 1s to the time so it must be the next time
time = !!time?time:this.nextTime;
time = !!time ? time : this.nextTime;
time += 1000;

@@ -54,15 +61,14 @@

outmost:
while(true){
if(date.getFullYear() > 2999){
outmost: while (true) {
if (date.getFullYear() > 2999) {
logger.error("Can't compute the next time, exceed the limit");
return null;
}
if(!timeMatch(date.getMonth(), cronTrigger[MONTH])){
if (!timeMatch(date.getMonth(), cronTrigger[MONTH])) {
var nextMonth = nextCronTime(date.getMonth(), cronTrigger[MONTH]);
if(nextMonth == null)
if (nextMonth == null)
return null;
if(nextMonth <= date.getMonth()){
if (nextMonth <= date.getMonth()) {
date.setYear(date.getFullYear() + 1);

@@ -76,3 +82,3 @@ date.setMonth(0);

}
date.setDate(1);

@@ -85,12 +91,12 @@ date.setMonth(nextMonth);

if(!timeMatch(date.getDate(), cronTrigger[DOM]) || !timeMatch(date.getDay(), cronTrigger[DOW])){
if (!timeMatch(date.getDate(), cronTrigger[DOM]) || !timeMatch(date.getDay(), cronTrigger[DOW])) {
var domLimit = getDomLimit(date.getFullYear(), date.getMonth());
do{
do {
var nextDom = nextCronTime(date.getDate(), cronTrigger[DOM]);
if(nextDom == null)
if (nextDom == null)
return null;
//If the date is in the next month, add month
if(nextDom <= date.getDate() || nextDom > domLimit){
if (nextDom <= date.getDate() || nextDom > domLimit) {
date.setDate(1);

@@ -104,4 +110,4 @@ date.setMonth(date.getMonth() + 1);

date.setDate(nextDom);
}while(!timeMatch(date.getDay(), cronTrigger[DOW]));
date.setDate(nextDom);
} while (!timeMatch(date.getDay(), cronTrigger[DOW]));

@@ -113,11 +119,11 @@ date.setHours(0);

if(!timeMatch(date.getHours(), cronTrigger[HOUR])){
if (!timeMatch(date.getHours(), cronTrigger[HOUR])) {
var nextHour = nextCronTime(date.getHours(), cronTrigger[HOUR]);
if(nextHour <= date.getHours()){
date.setDate(date.getDate() + 1);
date.setHours(nextHour);
date.setMinutes(0);
date.setSeconds(0);
continue;
if (nextHour <= date.getHours()) {
date.setDate(date.getDate() + 1);
date.setHours(nextHour);
date.setMinutes(0);
date.setSeconds(0);
continue;
}

@@ -130,10 +136,10 @@

if(!timeMatch(date.getMinutes(), cronTrigger[MIN])){
if (!timeMatch(date.getMinutes(), cronTrigger[MIN])) {
var nextMinute = nextCronTime(date.getMinutes(), cronTrigger[MIN]);
if(nextMinute <= date.getMinutes()){
date.setHours(date.getHours() + 1);
date.setMinutes(nextMinute);
date.setSeconds(0);
continue;
if (nextMinute <= date.getMinutes()) {
date.setHours(date.getHours() + 1);
date.setMinutes(nextMinute);
date.setSeconds(0);
continue;
}

@@ -145,9 +151,9 @@

if(!timeMatch(date.getSeconds(), cronTrigger[SECOND])){
if (!timeMatch(date.getSeconds(), cronTrigger[SECOND])) {
var nextSecond = nextCronTime(date.getSeconds(), cronTrigger[SECOND]);
if(nextSecond <= date.getSeconds()){
date.setMinutes(date.getMinutes() + 1);
date.setSeconds(nextSecond);
continue;
if (nextSecond <= date.getSeconds()) {
date.setMinutes(date.getMinutes() + 1);
date.setSeconds(nextSecond);
continue;
}

@@ -170,16 +176,16 @@

*/
function nextCronTime(value, cronTime){
function nextCronTime(value, cronTime) {
value += 1;
if(typeof(cronTime) == 'number'){
if(cronTime == -1)
if (typeof(cronTime) == 'number') {
if (cronTime == -1)
return value;
else
return cronTime;
}else if(typeof(cronTime) == 'object' && cronTime instanceof Array){
if(value <= cronTime[0] || value > cronTime[cronTime.length -1])
} else if (typeof(cronTime) == 'object' && cronTime instanceof Array) {
if (value <= cronTime[0] || value > cronTime[cronTime.length - 1])
return cronTime[0];
for(var i = 0; i < cronTime.length; i++)
if(value <= cronTime[i])
for (var i = 0; i < cronTime.length; i++)
if (value <= cronTime[i])
return cronTime[i];

@@ -198,15 +204,15 @@ }

*/
function timeMatch(value, cronTime){
if(typeof(cronTime) == 'number'){
if(cronTime == -1)
function timeMatch(value, cronTime) {
if (typeof(cronTime) == 'number') {
if (cronTime == -1)
return true;
if(value == cronTime)
if (value == cronTime)
return true;
return false;
}else if(typeof(cronTime) == 'object' && cronTime instanceof Array){
if(value < cronTime[0] || value > cronTime[cronTime.length -1])
} else if (typeof(cronTime) == 'object' && cronTime instanceof Array) {
if (value < cronTime[0] || value > cronTime[cronTime.length - 1])
return false;
for(var i = 0; i < cronTime.length; i++)
if(value == cronTime[i])
for (var i = 0; i < cronTime.length; i++)
if (value == cronTime[i])
return true;

@@ -225,6 +231,7 @@

*/
pro.decodeTrigger = function(cronTimeStr){
pro.decodeTrigger = function(cronTimeStr) {
cronTimeStr = cronTimeStr.trim();
var cronTimes = cronTimeStr.split(/\s+/);
if(cronTimes.length != 6){
if (cronTimes.length != 6) {
console.log('error');

@@ -234,8 +241,11 @@ return null;

for(var i = 0; i < cronTimes.length; i++){
for (var i = 0; i < cronTimes.length; i++) {
cronTimes[i] = (this.decodeTimeStr(cronTimes[i], i));
if(!checkNum(cronTimes[i], Limit[i][0], Limit[i][1])){
if (!checkNum(cronTimes[i], Limit[i][0], Limit[i][1])) {
logger.error('Decode crontime error, value exceed limit!' +
JSON.stringify({cronTime: cronTimes[i], limit:Limit[i]}));
JSON.stringify({
cronTime: cronTimes[i],
limit: Limit[i]
}));
return null;

@@ -253,38 +263,38 @@ }

*/
pro.decodeTimeStr = function(timeStr, type){
pro.decodeTimeStr = function(timeStr, type) {
var result = {};
var arr = [];
if(timeStr=='*'){
if (timeStr == '*') {
return -1;
}else if(timeStr.search(',')>0){
} else if (timeStr.search(',') > 0) {
var timeArr = timeStr.split(',');
for(var i = 0; i < timeArr.length; i++){
for (var i = 0; i < timeArr.length; i++) {
var time = timeArr[i];
if(time.match(/^\d+-\d+$/)){
if (time.match(/^\d+-\d+$/)) {
decodeRangeTime(result, time);
}else if(time.match(/^\d+\/\d+/)){
} else if (time.match(/^\d+\/\d+/)) {
decodePeriodTime(result, time, type);
}else if(!isNaN(time)){
} else if (!isNaN(time)) {
var num = Number(time);
result[num] = num;
}else
} else
return null;
}
}else if(timeStr.match(/^\d+-\d+$/)){
} else if (timeStr.match(/^\d+-\d+$/)) {
decodeRangeTime(result, timeStr);
}else if(timeStr.match(/^\d+\/\d+/)){
} else if (timeStr.match(/^\d+\/\d+/)) {
decodePeriodTime(result, timeStr, type);
}else if(!isNaN(timeStr)){
} else if (!isNaN(timeStr)) {
var num = Number(timeStr);
result[num] = num;
}else{
} else {
return null;
}
for(var key in result){
for (var key in result) {
arr.push(result[key]);
}
arr.sort(function(a, b){
arr.sort(function(a, b) {
return a - b;

@@ -301,3 +311,3 @@ });

*/
function decodeRangeTime(map, timeStr){
function decodeRangeTime(map, timeStr) {
var times = timeStr.split('-');

@@ -307,3 +317,3 @@

times[1] = Number(times[1]);
if(times[0] > times[1]){
if (times[0] > times[1]) {
console.log("Error time range");

@@ -313,3 +323,3 @@ return null;

for(var i = times[0]; i <= times[1]; i++){
for (var i = times[0]; i <= times[1]; i++) {
map[i] = i;

@@ -322,3 +332,3 @@ }

*/
function decodePeriodTime(map, timeStr, type){
function decodePeriodTime(map, timeStr, type) {
var times = timeStr.split('/');

@@ -331,7 +341,7 @@ var min = Limit[type][0];

if(period==0)
if (period == 0)
return;
for(var i = min; i <= max; i++){
if(i%period == remind)
for (var i = min; i <= max; i++) {
if (i % period == remind)
map[i] = i;

@@ -348,11 +358,11 @@ }

*/
function checkNum(nums, min, max){
if(nums == null)
function checkNum(nums, min, max) {
if (nums == null)
return false;
if(nums == -1)
if (nums == -1)
return true;
for(var i = 0; i < nums.length; i++){
if(nums[i]<min || nums[i]>max)
for (var i = 0; i < nums.length; i++) {
if (nums[i] < min || nums[i] > max)
return false;

@@ -370,4 +380,4 @@ }

*/
function getDomLimit(year, month){
var date = new Date(year, month+1, 0);
function getDomLimit(year, month) {
var date = new Date(year, month + 1, 0);

@@ -382,6 +392,6 @@ return date.getDate();

*/
function createTrigger(trigger, job){
function createTrigger(trigger, job) {
return new CronTrigger(trigger, job);
}
module.exports.createTrigger = createTrigger;
module.exports.createTrigger = createTrigger;
{
"name": "pomelo-scheduler",
"version": "0.3.9",
"main": "./lib/schedule",
"author": "XiaogangZhang <zhang0925@gmail.com>",
"dependencies": {
"log4js" : "0.6.7"
}
}
"name": "pomelo-scheduler",
"version": "0.4.0",
"main": "./lib/schedule",
"author": "XiaogangZhang <zhang0925@gmail.com>",
"dependencies": {
"log4js": "0.6.7"
}
}
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