UNPKG

handow-shm

Version:

E2E test server with Handow engine

1 lines 2.08 kB
"use strict";const e=require("moment"),n=require("path"),r=require("app-root-path"),t=require("deep-extend"),i=require("glob"),o=require("fs"),l=require("handow"),s=require(n.join(`${r}`,"/config.js")),a=require("./shmSetting"),p=require("cron").CronJob,h=require("../logger");let d,u,g,m,$=[];const c=e=>{let t,i=[];e?(t=(i=$.filter(n=>n.time.trim()===e))[0]).index=0:t=(i=$.filter(e=>e.index>0))[0],t&&t.index<t.plans.length?(l.runPlan(n.join(`${r}`,`${s.projectPath}`,`${t.plans[t.index]}.plan.json`)),t.index=t.index+1):t.index=0};module.exports=((e,t)=>{if(d=a.get(),g=d.loopSchedule.split(","),"loop"===d.scheduleMode)"start"===e||"shettingChanged"===e?(g=d.loopSchedule.split(","),l.handowStatus("status").running&&g.length>0?u=g[g.length-1].trim():(u=g[0].trim(),l.runPlan(n.join(`${r}`,`${s.projectPath}`,`${u}.plan.json`)))):"planFinished"===e&&g.length>0?(u=g.indexOf(u)===g.length-1?g[0].trim():g[g.indexOf(u)+1].trim(),l.runPlan(n.join(`${r}`,`${s.projectPath}`,`${u}.plan.json`))):h.error(new TypeError(`Looping plan error - Unknown scenario (${e}) or plans length (${g.length})`));else if("trigger"===d.scheduleMode)"trigger"===e&&t?(m=t.split(","),u=m[0].trim(),l.runPlan(n.join(`${r}`,`${s.projectPath}`,`${u}.plan.json`))):"planFinished"===e&&m.length>0?u!==m[m.length-1].trim()?(u=m[m.indexOf(u)+1].trim(),l.runPlan(n.join(`${r}`,`${s.projectPath}`,`${u}.plan.json`))):m=[]:t&&h.error(new TypeError(`Trigger plan error - Unknown scenario (${e}) or plans (${t})`));else if("timer"===d.scheduleMode)if("start"===e||"shettingChanged"===e){for(let e=0;e<$.length;e++)$[e].job.stop();$=[];for(const e of d.schedules){const n=e.startAt.split(":"),r=new p(`${n[2].trim()} ${n[1].trim()} ${n[0].trim()} * * *`,()=>{c(`${e.startAt}`)},null,!1,isNaN(d.shmTimezone)?d.shmTimezone:null);$.push({time:`${e.startAt.trim()}`,job:r,plans:e.plans.split(","),index:0})}for(let e=0;e<$.length;e++)$[e].job.start()}else"planFinished"===e&&c();else h.info("Info@doSchedule - No schedules after plan finished or config updated, waiting for maunal-run-command or url-trigger.")});