Merge branch 'mark-db-clean-after-compacting' into 'master'

Mark DB as clean after compacting, keep foreground service until shutdown completes

See merge request briar/briar!1842
This commit is contained in:
akwizgran
2025-04-30 10:30:48 +00:00
3 changed files with 31 additions and 19 deletions

View File

@@ -89,11 +89,17 @@ class H2Database extends JdbcDatabase {
try { try {
c = createConnection(); c = createConnection();
closeAllConnections(); closeAllConnections();
setDirty(c, false); LOG.info("Compacting DB");
s = c.createStatement(); s = c.createStatement();
s.execute("SHUTDOWN COMPACT"); s.execute("SHUTDOWN COMPACT");
LOG.info("Finished compacting DB");
s.close(); s.close();
c.close(); c.close();
// Reopen the DB to mark it as clean after compacting
c = createConnection();
setDirty(c, false);
LOG.info("Marked DB as clean");
c.close();
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(s, LOG, WARNING); tryToClose(s, LOG, WARNING);
tryToClose(c, LOG, WARNING); tryToClose(c, LOG, WARNING);
@@ -126,6 +132,7 @@ class H2Database extends JdbcDatabase {
closeAllConnections(); closeAllConnections();
s = c.createStatement(); s = c.createStatement();
s.execute("SHUTDOWN COMPACT"); s.execute("SHUTDOWN COMPACT");
LOG.info("Finished compacting DB");
s.close(); s.close();
c.close(); c.close();
} catch (SQLException e) { } catch (SQLException e) {

View File

@@ -217,19 +217,14 @@ public class BriarService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
// Hold a wake lock during shutdown super.onDestroy();
wakeLockManager.runWakefully(() -> { LOG.info("Destroyed");
super.onDestroy(); // Stop the lifecycle, if not already stopped
LOG.info("Destroyed"); shutdown(false);
stopForeground(true); stopForeground(true);
if (receiver != null) { if (receiver != null) {
getApplicationContext().unregisterReceiver(receiver); getApplicationContext().unregisterReceiver(receiver);
} }
// Stop the services in a background thread
wakeLockManager.executeWakefully(() -> {
if (started) lifecycleManager.stopServices();
}, "LifecycleShutdown");
}, "LifecycleShutdown");
} }
@Override @Override
@@ -299,8 +294,8 @@ public class BriarService extends Service {
private void shutdownFromBackground() { private void shutdownFromBackground() {
// Hold a wake lock during shutdown // Hold a wake lock during shutdown
wakeLockManager.runWakefully(() -> { wakeLockManager.runWakefully(() -> {
// Stop the service // Begin lifecycle shutdown
stopSelf(); shutdown(true);
// Hide the UI // Hide the UI
hideUi(); hideUi();
// Wait for shutdown to complete, then exit // Wait for shutdown to complete, then exit
@@ -335,8 +330,18 @@ public class BriarService extends Service {
/** /**
* Starts the shutdown process. * Starts the shutdown process.
*/ */
public void shutdown() { public void shutdown(boolean stopAndroidService) {
stopSelf(); // This will call onDestroy() // Hold a wake lock during shutdown
wakeLockManager.runWakefully(() -> {
// Stop the lifecycle services in a background thread,
// then stop this Android service if needed
wakeLockManager.executeWakefully(() -> {
if (started) lifecycleManager.stopServices();
if (stopAndroidService) {
androidExecutor.runOnUiThread(() -> stopSelf());
}
}, "LifecycleShutdown");
}, "LifecycleShutdown");
} }
public class BriarBinder extends Binder { public class BriarBinder extends Binder {

View File

@@ -147,7 +147,7 @@ public class BriarControllerImpl implements BriarController {
service.waitForStartup(); service.waitForStartup();
// Shut down the service and wait for it to shut down // Shut down the service and wait for it to shut down
LOG.info("Shutting down service"); LOG.info("Shutting down service");
service.shutdown(); service.shutdown(true);
service.waitForShutdown(); service.waitForShutdown();
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.warning("Interrupted while waiting for service"); LOG.warning("Interrupted while waiting for service");