const metadata = {
  queue: "domain-tls-retry",
  id: "customer_accounts:domain-tls-retry",
  concurrency: 1
};
function parseInt32(value, fallback) {
  const parsed = Number.parseInt(value ?? "", 10);
  if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
  return parsed;
}
function parseFloat01(value, fallback) {
  const parsed = Number.parseFloat(value ?? "");
  if (!Number.isFinite(parsed) || parsed <= 0 || parsed > 1) return fallback;
  return parsed;
}
let backoffMultiplier = 1;
let nextEligibleAt = 0;
const BACKOFF_CAP_HOURS = 6;
async function handle(_job, ctx) {
  const baseIntervalSeconds = parseInt32(process.env.DOMAIN_TLS_RETRY_INTERVAL_SECONDS, 1800);
  const batchSize = parseInt32(process.env.DOMAIN_TLS_RETRY_BATCH, 50);
  const maxRetries = parseInt32(process.env.DOMAIN_TLS_MAX_RETRIES, 6);
  const failureThreshold = parseFloat01(process.env.DOMAIN_TLS_RETRY_FAILURE_THRESHOLD, 0.8);
  const capSeconds = BACKOFF_CAP_HOURS * 3600;
  const maxMultiplier = Math.max(1, Math.floor(capSeconds / baseIntervalSeconds));
  if (Date.now() < nextEligibleAt) return;
  const service = ctx.resolve("domainMappingService");
  const candidates = await service.findPendingTls({ maxRetries, batchSize });
  if (candidates.length === 0) {
    backoffMultiplier = 1;
    nextEligibleAt = 0;
    return;
  }
  let attempted = 0;
  let failed = 0;
  for (const mapping of candidates) {
    attempted += 1;
    try {
      const updated = await service.healthCheck(mapping.id);
      if (updated.status !== "active") failed += 1;
    } catch {
      failed += 1;
    }
  }
  if (attempted === 0) return;
  const failureRate = failed / attempted;
  if (failureRate >= failureThreshold) {
    backoffMultiplier = Math.min(backoffMultiplier * 2, maxMultiplier);
    nextEligibleAt = Date.now() + backoffMultiplier * baseIntervalSeconds * 1e3;
  } else {
    backoffMultiplier = 1;
    nextEligibleAt = 0;
  }
}
const __testing__ = {
  reset: () => {
    backoffMultiplier = 1;
    nextEligibleAt = 0;
  },
  getState: () => ({ backoffMultiplier, nextEligibleAt })
};
export {
  __testing__,
  handle as default,
  metadata
};
//# sourceMappingURL=domainTlsRetryWorker.js.map
