robweber / xbmcbackup

Backup Addon for Kodi
MIT License
112 stars 48 forks source link

Any Backup timer produces backups at Kodi startup #147

Closed n0valis closed 5 years ago

n0valis commented 5 years ago

If I schedule a timer a backup would start at any startup of Kodi. Even if it is not checked in the settings. Only way to prevent it is to turn of the scheduler.

This is on Raspi 3 / LE 9.0.2

robweber commented 5 years ago

Can you be more specific with what you're doing? The settings for the scheduler are either that it's off or it's on. How are you not checking it but yet not turning it off? Just trying to find a series of steps I can reproduce.

n0valis commented 5 years ago

What I mean is that a timer e.g. set to weekly should run weekly and not on every startup. You have included an option 'run at startup if missed' and I'd understand that Kodi would run a backup at startup if this is checked. But in my case it isn't. That's what I'm referring to. If I don't want to have backup perform a backuo at every startup I have to disable the scheduler.

robweber commented 5 years ago

Ok gotcha now. I'll take a look at reproducing this within the next few days.

robweber commented 5 years ago

I tried a a series of things to reproduce this today and just couldn't get the same behavior. Tried with a default install too. I enabled the scheduler, set a schedule for weekly with no backup if missed. When I hit OK I saw a notification about the next backup time (which was correct). After rebooting Kodi the backup did not run, and I saw another notification with the correct next run time. I tried this numerous times with different configurations. I even tried waiting for the backup time to pass, then starting Kodi to see if that did it.

One thing I did see as a potential problem looking at the scheduler code is related to how it finds a "missed" time. You can see on these lines that if the next_run.txt file can't be read it defaults to 0 for the next run. This essentially means now. Do you think there could be a problem reading/writing to this file? If you can dig into the file system a bit I would like to see the contents of your next_run.txt file and the settings.xml file located in the userdata/addon_data/script.xbmcbackup/ directory? Go ahead and remove any confidential info if needed. I'm mostly concerned with the schedule settings. On LibreElec you should be able to go into the Userdata folder and find this from an SMB browser.

robweber commented 5 years ago

Saw your post on the forum about this still happening with the new Beta version. I cannot reproduce for anything, I wish I could.

I enabled the scheduler, and set it to once a month as you indicated. In the logs I saw the following:

2019-08-01 14:44:14.798 T:16988   DEBUG: script.xbmcbackup-1.5.0~beta1: scheduler enabled, finding next run time
2019-08-01 14:44:14.798 T:16988   DEBUG: script.xbmcbackup-1.5.0~beta1: scheduler will run again on 09-01-2019 00:00

I also saw a notification the screen. In the addon_data/script.xbmcbackup folder there is a file called next_run.txt this is the file used to test if the backup should run on startup. It should update with a current timestamp and a non-zero value (unix timestamp).

I restarted Kodi and saw a notification with the correct backup time and no backup ran right then. In the logs I saw the following:

2019-08-01 14:44:55.632 T:18464   DEBUG: script.xbmcbackup-1.5.0~beta1: scheduler enabled, finding next run time
2019-08-01 14:44:55.634 T:18464   DEBUG: script.xbmcbackup-1.5.0~beta1: scheduler will run again on 09-01-2019 00:00

Please check your next_run.txt file and ensure it's being written to correctly. If it isn't that might result in the backup running on startup. If the file is there trying deleting it and turning the scheduler on/off again. It could be that the current file permissions won't let the addon update it.

ubuntu748 commented 5 years ago

I've had the same issue on this. I believe it is related to the time not being updated prior to the addon running. This is because the devices in question TV boxes, RPi1,2,3,4 do not have a realtime chip and thus have to wait for the NTP daemon to update the time correctly. Time is stuck at 12:00 and backup "thinks" it has missed a schedule and runs immediately. I got round this by adding a sleep(120) in scheduler.py. This gives the clock time to update. If, possible this could be added to the Kodi addon prefs in a later release.

robweber commented 5 years ago

Thank you!

This will be a really simple fix to add. I don't use an RPi for Kodi but have used it in other applications before. The time chip thing is something I'm aware of but not thought about it in this context. Totally makes sense that this would mess up the scheduler checking if it missed a run. I'll do some searching too on the platform detection capabilities within an addon. I know you can detect windows vs linux but if it gets as granular as an Rpi I could just hardcode the delay for that platform rather than offering a setting for it.

robweber commented 5 years ago

Just for my own info later when looking at this - here is the list of system conditional variables. Rpi is on the list. Further testing will need to be done though: https://kodi.wiki/view/List_of_boolean_conditions#System

ubuntu748 commented 5 years ago

Many H/W have a hwclock or in the case of RPi fake-hwclock (based on last boot time) which should set the time at some stage in the boot process. However, in my case, this took some time. Unless there is a real reason to run at startup - a delay of 2 -> 5 minutes wouldn't do any harm on any platform and would be easier to implement rather than check OS's etc. No need for a change to the Kodi Gui.

This should work, even if there is no network, as (I believe) it's a timing issue of the OS rather than the OS not actually updating the time.

n0valis commented 5 years ago

Ok I was a bit asleep here sorry. Seems we found the cause. 1567339200.0 is what isc n my nextrun. txt but I think it's not important anymore. Hope that's resolved now. Thanks

robweber commented 5 years ago

In my experience with developing other apps on an RPi the time is very often off for at least 1-2 minutes. I see what you're saying though, a general delay of a few minutes wouldn't hurt the addon as a whole. It really only affects if "run on startup if missed" is checked and you also happened to actually miss a backup for the delay to really mean anything.

I'll get something put together this weekend so hopefully @n0valis can test.

robweber commented 5 years ago

@n0valis - try out the beta branch https://github.com/robweber/xbmcbackup/tree/krypton again and see if that does the trick for you. I can backport this fix to the current branch as well if it's working.

n0valis commented 5 years ago

Tested and confirmed as good. What I couldn't test is whether the scheduler actually works as intended, but the original problem is solved.

robweber commented 5 years ago

Great - thanks for checking this out. I did a few test on my end doing daily backups and a few cron schedules as well. Rebooted Kodi between tests and they always ran for me. I'm using a comb of Windows and Libreelec on an Intel PC so good to know it's working outside of that as well.

I'll back port this to the master branch as well. Was a pretty easy fix. Not sure I can push out to the main repo on this as I was recently told they're only accepting python 2/3 compatible addons now. I thought this was for the Leia branch only but apparently it applies to everything. I'll definitely put a post out on the forum and stuff for people that need it. Will close this when done.

n0valis commented 5 years ago

much appreciated

robweber commented 5 years ago

Ok, going to close this out. Changes have been made to both the master and beta branches. Only difference I made is that instead of xbmc.sleep() I changed this to xbmc.Monitor().waitForAbort(). That way if someone tries to close out Kodi before the 2 minutes is up the process won't hang.