Steve-Mcl / node-red-contrib-cron-plus

A flexible scheduler node for Node-RED (featuring full dynamic control, sunrise/sunset by location and Timezone support)
MIT License
45 stars 12 forks source link

(Restart) a non stopped scheduler #24

Closed lzago closed 3 years ago

lzago commented 4 years ago

I have configured only one static CRON scheduler, every 2 minutes. I need to reset it sometimes by a message. so let's suppose the scheduler is scheduling events on 06:02, 06:04, 06:06 etc At 6:01 i send a message with the command 'start-all'. Expected behavior: the scheduler resets and start sending events at 06:03, 6:05 etc Actual Behavior: the scheduler shows 'Done' at 6:03 but it doesn't send out anything, but it does at 6:04. And it continues to show Done at 6:05, 6:07, but sending messages only at 6:06, 6:08 as if it follows the former scheduling.

regards,

Steve-Mcl commented 4 years ago

Hi @lzago this is how cron works & it is by design.

e.g. if you specify 0 0/2 * * * * * - you are asking for events every 2 minutes, starting on 0 minutes.

image

however if you specify 0 1/2 * * * * - you are asking for events every 2 minutes, starting at 1 minute past. image

There are a few workarounds here is one way...

[{"id":"ac2514bb.d5d388","type":"inject","z":"21106466.70e60c","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1070,"y":480,"wires":[["6a36ae86.19fa4"]]},{"id":"6a36ae86.19fa4","type":"function","z":"21106466.70e60c","name":"","func":"var min = (new Date().getMinutes() % 2);\n\nmsg.payload = {\n    \"command\": \"add\",\n    \"name\": \"every-2-minutes\",\n    \"expression\": `0 ${min}/2 * * * * *`,\n    \"expressionType\": \"cron\",\n    \"payloadType\": \"default\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1220,"y":480,"wires":[["36ca38b.fb889c8"]]},{"id":"36ca38b.fb889c8","type":"cronplus","z":"21106466.70e60c","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output2","outputs":2,"options":[],"x":1380,"y":480,"wires":[["1091b76.217c549"],["5ccc15cb.071fac"]]},{"id":"b8f4a7a3.a44758","type":"inject","z":"21106466.70e60c","name":"stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"stop","payload":"every-2-minutes","payloadType":"str","x":1070,"y":520,"wires":[["36ca38b.fb889c8"]]},{"id":"1091b76.217c549","type":"debug","z":"21106466.70e60c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1570,"y":440,"wires":[]},{"id":"5ccc15cb.071fac","type":"debug","z":"21106466.70e60c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1570,"y":480,"wires":[]}]

... essentially, I use the current minute to generate either either 0 0/2 * * * * or 0 1/2 * * * *

lzago commented 4 years ago

@Steve-Mcl thanks for the reply, i got your point, but why when i send the start message at 6:01 i don't receive any message at 6:03, 6:05 etc? probably it depends on the fact the scheduler is static in my case? your example works fine, because it pratically update a dynamic scheduler.

Steve-Mcl commented 3 years ago

Hi @lzago as the CRON is "doing what it is told" and there is a dynamic way to handle this, I dont think there is an other elegant way to solve this.

If you have an idea, please feel free to re-open.