mrgadget / node-red-contrib-eztimer

A simple-yet-flexible timer/scheduler for node-red
MIT License
13 stars 6 forks source link

reset all events on system time changes like NTP #52

Open razvanstanga opened 2 years ago

razvanstanga commented 2 years ago

I am having a problem when the system time changes (ex: NTP after a restart) and the events are late. More details about the problem here https://discourse.nodered.org/t/inject-node-runs-late-sometimes/30365/56?page=2

Please take a look at this : https://github.com/razvanstanga/node-red-contrib-eztimer-ha/commit/62119b53eb6e364ec3acbf5efbc77acf07fed488

It's not tested. I will test it soon. Is the logic correct suspend() -> resume() ? I am not very familiar with the code.

razvanstanga commented 2 years ago

Dec 23 16:52:24 ha systemd-timesyncd[178]: Synchronized to time server for the first time 216.229.4.66:123 (1.us.pool.ntp.org). Dec 23 16:52:24 ha Node-RED[233]: 23 Dec 16:52:24 - [info] [eztimer:147727ae.33f438] System Time Change Detected!

The case is :

  1. power is down, everything shuts down
  2. power comes back
  3. raspberry pi and eztimer start before the router gets a chance to start
  4. when the router is back online, systemd-timesyncd runs and sets the correct system time. eztimer events are already scheduled with the wrong time
  5. if the time change is more then 5 seconds, eztimer events are reinitialized to the new system time
razvanstanga commented 2 years ago

Steps to reproduce : systemctl stop systemd-timesyncd.service date -s "2021-12-23 17:10:00" # something behind the current time systemctl restart nodered # wait for it to restart systemctl start systemd-timesyncd.service tail -n 100 /var/log/syslog

Dec 23 17:32:48 ha Node-RED[952]: 23 Dec 17:32:48 - [info] [eztimer:acf2bbd8.e4d9e8] System time change detected! Dec 23 17:32:48 ha Node-RED[952]: 23 Dec 17:32:48 - [info] [eztimer:acf2bbd8.e4d9e8] --- old time was :2021-12-23 17:11:27 Dec 23 17:32:48 ha Node-RED[952]: 23 Dec 17:32:48 - [info] [eztimer:acf2bbd8.e4d9e8] --- new time is :2021-12-23 17:32:48

https://github.com/razvanstanga/node-red-contrib-eztimer-ha/commit/16d5707ccd0339840dd875e36097ca9296b8dded