Add a LocalDB Auto Save Mechanism #41

Merged
kske merged 1 commits from f/localdb-autosave into develop 2020-09-22 16:45:58 +02:00
4 changed files with 25 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public final class ClientConfig extends Config {
put("server", "s", identity()); put("server", "s", identity());
put("port", "p", Integer::parseInt); put("port", "p", Integer::parseInt);
put("localDB", "db", File::new); put("localDB", "db", File::new);
put("localDBSaveInterval", "db-si", Integer::parseInt);
} }
/** /**
@ -54,4 +55,10 @@ public final class ClientConfig extends Config {
* @since Envoy Client v0.1-alpha * @since Envoy Client v0.1-alpha
*/ */
public File getLocalDB() { return (File) items.get("localDB").get(); } public File getLocalDB() { return (File) items.get("localDB").get(); }
/**
* @return the amount of minutes after which the local database should be saved
* @since Envoy Client v0.2-beta
*/
public Integer getLocalDBSaveInterval() { return (Integer) items.get("localDBSaveInterval").get(); }
} }

View File

@ -160,6 +160,20 @@ public final class LocalDB implements EventListener {
.forEach(chats::add); .forEach(chats::add);
} }
/**
* Initializes a timer that automatically saves this local database after a
* period of time specified in the settings.
*
* @since Envoy Client v0.2-beta
*/
public void initAutoSave() {
new Timer("LocalDB Autosave", true).schedule(new TimerTask() {
@Override
public void run() { save(); }
}, 2000, ClientConfig.getInstance().getLocalDBSaveInterval() * 60000);
}
/** /**
* Stores all users. If the client user is specified, their chats will be stored * Stores all users. If the client user is specified, their chats will be stored
* as well. The message id generator will also be saved if present. * as well. The message id generator will also be saved if present.

View File

@ -226,5 +226,8 @@ public final class Startup extends Application {
else trayIcon.hide(); else trayIcon.hide();
}); });
} }
// Start auto save thread
localDB.initAutoSave();
} }
} }

View File

@ -1,8 +1,6 @@
server=localhost server=localhost
port=8080 port=8080
localDB=localDB localDB=localDB
password= localDBSaveInterval=2
user=
ignoreLocalDB=false
consoleLevelBarrier=FINER consoleLevelBarrier=FINER
fileLevelBarrier=OFF fileLevelBarrier=OFF