async initCleanupTask()

in common/lib/utils/sliding_expiration_cache_with_cleanup_task.ts [84:119]


  async initCleanupTask(): Promise<void> {
    this.isInitialized = true;
    logger.debug(Messages.get("SlidingExpirationCacheWithCleanupTask.cleanUpTaskInitialized", this.cacheId));
    while (this.isInitialized) {
      const [sleepPromise, abortSleepFunc] = sleepWithAbort(
        convertNanosToMs(this._cleanupIntervalNanos),
        Messages.get("SlidingExpirationCacheWithCleanupTask.cleanUpTaskInterrupted", this.cacheId)
      );
      this.interruptCleanupTask = abortSleepFunc;
      try {
        await sleepPromise;
      } catch (error) {
        // Sleep has been interrupted, exit cleanup task.
        logger.debug(error.message);
        return;
      }

      logger.debug(
        Messages.get("SlidingExpirationCacheWithCleanupTask.cleaningUp", convertNanosToMinutes(this._cleanupIntervalNanos).toString(), this.cacheId)
      );

      const itemsToRemove = [];
      for (const [key, val] of this.map.entries()) {
        if (val !== undefined && this._asyncItemDisposalFunc !== undefined && this.shouldCleanupItem(val)) {
          MapUtils.remove(this.map, key);
          itemsToRemove.push(this._asyncItemDisposalFunc(val.item));
        }
      }
      try {
        await Promise.all(itemsToRemove);
      } catch (error) {
        // Ignore.
      }
    }
    logger.debug(Messages.get("SlidingExpirationCacheWithCleanupTask.cleanUpTaskStopped", this.cacheId));
  }