Plan for cleanup of stale temporary files

From OHRRPGCE-Wiki
Jump to navigation Jump to search

The OHRRPGCE game player stores temporary files. These are located in a platform-specific temporary folder, and inside a folder named ohrrpgceDATETIME.RANDOM.tmp for example, ohrrpgce20130618103302.978.tmp

Under normal circumstances, the game player deletes all temporary files when it terminates.

Unfortunately, when the game player crashes for any reason, the temporary folder gets left behind.

We need a scheme to delete unused temporary folders.

Using the datestamp in the foldername is not useful because multiple copies of the game player can be running, and might sometimes remain running for a long time.

There is no reliable method of guessing if a running process is still using a temporary file.

Proposal[edit]

Inside the temporary folder there should be a small file that contains only a timestamp. We will call this a keepalive file.

A running copy of the game player refreshes the keepalive once each hour from the playtimer() sub. The keepalive should also be refreshed once as soon as the game starts.

When the game player terminates, when cleaning up its own temporary folder, it also looks through a directory listing of all the temp folders, and checks the keepalive file inside each one. If a folder is sufficiently stale, it will be deleted.

The threshold for deleting a stale tmp folder should be considerably larger than the 1 hour interval that the game player updates the keepalive, maybe something like 3 days.

If the cleanup routine finds no keepalive file at all, it knows that the temp file is left over from an old version of game from before the keepalive scheme was implemented. In this case it falls back to the timestamp in the folder name, and deletes it with a slightly longer threshold. Maybe 2 weeks.

Disadvantages[edit]

If you are running more than one copy of the game player simultaneously, then you change your system clock forward more than 3 days, and then exit one of the copies of the game player before an hour has passed, it will attempt to delete the files for the other copy of the game player. This problem isn't important enough for us to worry about.

If you are running an old copy of the game player from before this cleanup feature is implemented simultaneously with a new copy of the game player that has this feature, and you let them both run for more than a week, and then you close the newer copy, it will try to delete the files belonging to the old version. This isn't a big enough problem to worry about.

If you run two copies of the game player simultaneously, then hibernate your computer, wait more than 3 days, awaken your computer, and then exit one copy of the game player before the hour has completed, it will try to delete the files belonging to the other copy of the game player. It would be nice to fix this issue somehow, but it is not important enough to stop the plan from being implemented. A nice workaround would be if we had some platform-specific code to detect power-saving resume events and update the keepalive file when one is received, but of course, the difficulty of doing this is platform-dependant.

Android[edit]

Android only allows one copy of an app with the same package name to be running at a time, and if the ohrrpgce is installed under two different package names they will have their temp file locations safely insulated from one another.

Therefore, on Android it would be best to delete all leftover temp files every time the app starts.

The only downside to this approach is a (probably pretty small) delay on the next start-up after a crash.