startBackgroundHousekeeping function

void startBackgroundHousekeeping()

Start background housekeeping tasks.

Implementation

void startBackgroundHousekeeping() {
  bool needsCleanup = true;

  Future<void> runVerySlowOps() async {
    // If the user did something in the last minute, defer.
    if (_getIsInteractive() &&
        _getLastInteractionTime() >
            DateTime.now().millisecondsSinceEpoch - 60000) {
      Timer(_delayVerySlowOps, () {
        runVerySlowOps();
      });
      return;
    }

    if (needsCleanup) {
      needsCleanup = false;
      await cleanupOldMessageFilesInBackground();
    }

    // If the user did something in the last minute, defer.
    if (_getIsInteractive() &&
        _getLastInteractionTime() >
            DateTime.now().millisecondsSinceEpoch - 60000) {
      Timer(_delayVerySlowOps, () {
        runVerySlowOps();
      });
      return;
    }

    // cleanupOldVersions() equivalent would go here.
  }

  Timer(_delayVerySlowOps, () {
    runVerySlowOps();
  });

  // For long-running sessions, schedule recurring cleanup every 24 hours.
  final userType = Platform.environment['USER_TYPE'];
  if (userType == 'ant') {
    Timer.periodic(_recurringCleanupInterval, (_) {
      // cleanupNpmCacheForAnthropicPackages() equivalent.
      // cleanupOldVersionsThrottled() equivalent.
    });
  }
}