diff --git a/src/acme2.js b/src/acme2.js index 754d96983..349d6b6ef 100644 --- a/src/acme2.js +++ b/src/acme2.js @@ -244,7 +244,7 @@ Acme2.prototype.waitForOrder = async function (orderUrl) { log(`waitForOrder: ${orderUrl}`); - return await promiseRetry({ times: 15, interval: 20000, debug: log }, async () => { + return await promiseRetry({ times: 15, interval: 20000, log }, async () => { log('waitForOrder: getting status'); const result = await this.postAsGet(orderUrl); @@ -297,7 +297,7 @@ Acme2.prototype.waitForChallenge = async function (challenge) { log(`waitingForChallenge: ${JSON.stringify(challenge)}`); - await promiseRetry({ times: 15, interval: 20000, debug: log }, async () => { + await promiseRetry({ times: 15, interval: 20000, log }, async () => { log('waitingForChallenge: getting status'); const result = await this.postAsGet(challenge.url); @@ -335,7 +335,7 @@ Acme2.prototype.signCertificate = async function (finalizationUrl, csrPem) { Acme2.prototype.downloadCertificate = async function (certUrl) { assert.strictEqual(typeof certUrl, 'string'); - return await promiseRetry({ times: 5, interval: 20000, debug: log }, async () => { + return await promiseRetry({ times: 5, interval: 20000, log }, async () => { log(`downloadCertificate: downloading certificate of ${this.cn}`); const result = await this.postAsGet(certUrl); @@ -471,7 +471,7 @@ Acme2.prototype.acmeFlow = async function () { const challenge = await this.prepareChallenge(cn, authorization); await this.notifyChallengeReady(challenge); await this.waitForChallenge(challenge); - await safe(this.cleanupChallenge(cn, challenge), { debug: log }); + await safe(this.cleanupChallenge(cn, challenge), { log }); } const csr = await openssl.createCsr(this.key, this.cn, this.altNames); @@ -485,7 +485,7 @@ Acme2.prototype.acmeFlow = async function () { }; Acme2.prototype.loadDirectory = async function () { - await promiseRetry({ times: 3, interval: 20000, debug: log }, async () => { + await promiseRetry({ times: 3, interval: 20000, log }, async () => { const response = await superagent.get(this.caDirectory).timeout(30000).ok(() => true); if (response.status !== 200) throw new BoxError(BoxError.ACME_ERROR, `Invalid response code when fetching directory : ${response.status}`); @@ -522,7 +522,7 @@ async function getCertificate(fqdn, domainObject, key) { const owner = await users.getOwner(); const email = owner?.email || 'webmaster@cloudron.io'; // can error if not activated yet - return await promiseRetry({ times: 3, interval: 0, debug: log }, async function () { + return await promiseRetry({ times: 3, interval: 0, log }, async function () { log(`getCertificate: for fqdn ${fqdn} and domain ${domainObject.domain}`); const acme = new Acme2(fqdn, domainObject, email, key, { /* profile: 'shortlived' */ }); diff --git a/src/appstore.js b/src/appstore.js index 50a26bc59..90e968bd3 100644 --- a/src/appstore.js +++ b/src/appstore.js @@ -388,7 +388,7 @@ async function getApp(appId) { async function downloadIcon(appStoreId, version) { const iconUrl = `${await getApiServerOrigin()}/api/v1/apps/${appStoreId}/versions/${version}/icon`; - return await promiseRetry({ times: 10, interval: 5000, debug: log }, async function () { + return await promiseRetry({ times: 10, interval: 5000, log }, async function () { const [networkError, response] = await safe(superagent.get(iconUrl) .timeout(60 * 1000) .ok(() => true)); diff --git a/src/apptask.js b/src/apptask.js index 0f54718b8..2487fd9cf 100644 --- a/src/apptask.js +++ b/src/apptask.js @@ -63,7 +63,7 @@ async function allocateContainerIp(app) { if (app.manifest.id === constants.PROXY_APP_APPSTORE_ID) return; - await promiseRetry({ times: 10, interval: 0, debug: log }, async function () { + await promiseRetry({ times: 10, interval: 0, log }, async function () { const iprange = iputils.intFromIp(constants.APPS_IPv4_END) - iputils.intFromIp(constants.APPS_IPv4_START); const rnd = Math.floor(Math.random() * iprange); const containerIp = iputils.ipFromInt(iputils.intFromIp(constants.APPS_IPv4_START) + rnd); @@ -865,9 +865,9 @@ async function run(appId, args, progressCallback) { if (app.installationState === apps.ISTATE_PENDING_UPDATE && error.backupError) { log('run: update aborted because backup failed'); - await safe(updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }, { debug: log })); + await safe(updateApp(app, { installationState: apps.ISTATE_INSTALLED, error: null, health: null }, { log })); } else { - await safe(updateApp(app, { installationState: apps.ISTATE_ERROR, error: makeTaskError(error, app) }), { debug: log }); + await safe(updateApp(app, { installationState: apps.ISTATE_ERROR, error: makeTaskError(error, app) }), { log }); } throw error; diff --git a/src/backupformat/rsync.js b/src/backupformat/rsync.js index c5680e653..126217c3f 100644 --- a/src/backupformat/rsync.js +++ b/src/backupformat/rsync.js @@ -193,7 +193,7 @@ async function sync(backupSite, remotePath, dataLayout, progressCallback) { } else if (change.operation === 'remove') { await backupSites.storageApi(backupSite).remove(backupSite.config, fullPath); } else if (change.operation === 'add') { - await promiseRetry({ times: 5, interval: 20000, debug: log }, async (retryCount) => { + await promiseRetry({ times: 5, interval: 20000, log }, async (retryCount) => { reportUploadProgress(`Current: ${change.path}` + (retryCount > 1 ? ` (Try ${retryCount})` : '')); if (retryCount > 1) log(`sync: retrying ${change.path} position ${change.position} try ${retryCount}`); const uploader = await backupSites.storageApi(backupSite).upload(backupSite.config, backupSite.limits, fullPath); diff --git a/src/backupformat/tgz.js b/src/backupformat/tgz.js index 6365a59e3..f78bf3611 100644 --- a/src/backupformat/tgz.js +++ b/src/backupformat/tgz.js @@ -74,7 +74,7 @@ function addEntryToPack(pack, header, options) { if (options?.input) { const ensureFileSizeStream = new EnsureFileSizeStream({ name: header.name, size: header.size }); - safe(stream.pipeline(options.input, ensureFileSizeStream, packEntry), { debug: log }); // background. rely on pack.entry callback for promise completion + safe(stream.pipeline(options.input, ensureFileSizeStream, packEntry), { log }); // background. rely on pack.entry callback for promise completion } }); } @@ -163,7 +163,7 @@ async function tarPack(dataLayout, encryption, uploader, progressCallback) { let fileCount = 0; for (const localPath of dataLayout.localPaths()) { - const [error, stats] = await safe(addPathToPack(pack, localPath, dataLayout), { debug: log }); + const [error, stats] = await safe(addPathToPack(pack, localPath, dataLayout), { log }); if (error) break; // the pipeline will error and we will retry the whole packing all over fileCount += stats.fileCount; } @@ -253,7 +253,7 @@ async function download(backupSite, remotePath, dataLayout, progressCallback) { log(`download: Downloading ${remotePath} to ${dataLayout.toString()}`); - await promiseRetry({ times: 3, interval: 20000, debug: log }, async () => { + await promiseRetry({ times: 3, interval: 20000, log }, async () => { progressCallback({ message: `Downloading backup ${remotePath}` }); const sourceStream = await backupSites.storageApi(backupSite).download(backupSite.config, remotePath); @@ -269,7 +269,7 @@ async function upload(backupSite, remotePath, dataLayout, progressCallback) { log(`upload: uploading to site ${backupSite.id} path ${remotePath} (encrypted: ${!!backupSite.encryption}) dataLayout ${dataLayout.toString()}`); - return await promiseRetry({ times: 5, interval: 20000, debug: log }, async () => { + return await promiseRetry({ times: 5, interval: 20000, log }, async () => { progressCallback({ message: `Uploading backup ${remotePath}` }); const uploader = await backupSites.storageApi(backupSite).upload(backupSite.config, backupSite.limits, remotePath); diff --git a/src/community.js b/src/community.js index 70a961311..fc3e0e7cc 100644 --- a/src/community.js +++ b/src/community.js @@ -170,7 +170,7 @@ async function getAppUpdate(app, options) { } async function downloadIcon(manifest) { - return await promiseRetry({ times: 10, interval: 5000, debug: log }, async function () { + return await promiseRetry({ times: 10, interval: 5000, log }, async function () { const [networkError, response] = await safe(superagent.get(manifest.iconUrl) .timeout(60 * 1000) .ok(() => true)); diff --git a/src/dns.js b/src/dns.js index 191e2f998..99b265973 100644 --- a/src/dns.js +++ b/src/dns.js @@ -250,7 +250,7 @@ async function registerLocations(locations, options, progressCallback) { for (const location of locations) { progressCallback({ message: `Registering location ${fqdn(location.subdomain, location.domain)}` }); - await promiseRetry({ times: 200, interval: 5000, debug: log, retry: (error) => error.retryable }, async function () { + await promiseRetry({ times: 200, interval: 5000, log, retry: (error) => error.retryable }, async function () { // cname records cannot co-exist with other records const [getError, values] = await safe(getDnsRecords(location.subdomain, location.domain, 'CNAME')); if (!getError && values.length === 1) { @@ -263,14 +263,14 @@ async function registerLocations(locations, options, progressCallback) { await registerLocation(location, options, 'A', ipv4); } else { const [aError, aValues] = await safe(getDnsRecords(location.subdomain, location.domain, 'A')); - if (!aError && aValues.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'A', aValues), { debug: log }); + if (!aError && aValues.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'A', aValues), { log }); } if (ipv6) { await registerLocation(location, options, 'AAAA', ipv6); } else { const [aaaaError, aaaaValues] = await safe(getDnsRecords(location.subdomain, location.domain, 'AAAA')); - if (!aaaaError && aaaaValues.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'AAAA', aaaaValues), { debug: log }); + if (!aaaaError && aaaaValues.length) await safe(removeDnsRecords(location.subdomain, location.domain, 'AAAA', aaaaValues), { log }); } }); } @@ -300,7 +300,7 @@ async function unregisterLocations(locations, progressCallback) { for (const location of locations) { progressCallback({ message: `Unregistering location: ${location.subdomain ? (location.subdomain + '.') : ''}${location.domain}` }); - await promiseRetry({ times: 30, interval: 5000, debug: log, retry: (error) => error.retryable }, async function () { + await promiseRetry({ times: 30, interval: 5000, log, retry: (error) => error.retryable }, async function () { if (ipv4) await unregisterLocation(location, 'A', ipv4); if (ipv6) await unregisterLocation(location, 'AAAA', ipv6); }); @@ -364,7 +364,7 @@ async function startSyncDnsRecords(options) { assert.strictEqual(typeof options, 'object'); const taskId = await tasks.add(tasks.TASK_SYNC_DNS_RECORDS, [ options ]); - safe(tasks.startTask(taskId, {}), { debug: log }); // background + safe(tasks.startTask(taskId, {}), { log }); // background return taskId; } diff --git a/src/dns/hetznercloud.js b/src/dns/hetznercloud.js index f752f9907..4646ecf8c 100644 --- a/src/dns/hetznercloud.js +++ b/src/dns/hetznercloud.js @@ -88,7 +88,7 @@ async function waitForAction(domainConfig, id) { assert.strictEqual(typeof domainConfig, 'object'); assert.strictEqual(typeof id, 'number'); - await promiseRetry({ times: 100, interval: 1000, debug: log }, async () => { + await promiseRetry({ times: 100, interval: 1000, log }, async () => { const [error, response] = await safe(superagent.get(`${ENDPOINT}/actions/${id}`) .set('Authorization', `Bearer ${domainConfig.token}`) .timeout(30 * 1000) diff --git a/src/dns/waitfordns.js b/src/dns/waitfordns.js index 5754f6fe8..f634687c6 100644 --- a/src/dns/waitfordns.js +++ b/src/dns/waitfordns.js @@ -89,7 +89,7 @@ async function waitForDns(hostname, zoneName, type, value, options) { log(`waitForDns: waiting for ${hostname} to be ${value} in zone ${zoneName}`); - await promiseRetry(Object.assign({ debug: log }, options), async function () { + await promiseRetry(Object.assign({ log }, options), async function () { const nameservers = await dig.resolve(zoneName, 'NS', { timeout: 5000 }); if (!nameservers) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to get nameservers'); log(`waitForDns: nameservers are ${JSON.stringify(nameservers)}`); diff --git a/src/docker.js b/src/docker.js index ecf0fbab4..8e303a457 100644 --- a/src/docker.js +++ b/src/docker.js @@ -441,7 +441,7 @@ async function downloadImage(manifest) { const parsedManifestRef = parseImageRef(manifest.dockerImage); - await promiseRetry({ times: 10, interval: 5000, debug: log, retry: (pullError) => pullError.reason !== BoxError.FS_ERROR }, async () => { + await promiseRetry({ times: 10, interval: 5000, log, retry: (pullError) => pullError.reason !== BoxError.FS_ERROR }, async () => { // custom (non appstore) image if (parsedManifestRef.registry !== null || !parsedManifestRef.fullRepositoryName.startsWith('cloudron/')) return await pullImage(manifest.dockerImage); diff --git a/src/locks.js b/src/locks.js index 34f97e563..af58a9328 100644 --- a/src/locks.js +++ b/src/locks.js @@ -59,7 +59,7 @@ function canAcquire(data, type) { async function acquire(type) { assert.strictEqual(typeof type, 'string'); - await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, debug: log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { + await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { const { version, data } = await read(); const error = canAcquire(data, type); if (error) throw error; @@ -72,13 +72,13 @@ async function acquire(type) { async function wait(type) { assert.strictEqual(typeof type, 'string'); - await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 10000, debug: log }, async () => await acquire(type)); + await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 10000, log }, async () => await acquire(type)); } async function release(type) { assert.strictEqual(typeof type, 'string'); - await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, debug: log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { + await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { const { version, data } = await read(); if (!(type in data)) throw new BoxError(BoxError.BAD_STATE, `Lock ${type} was never acquired`); if (data[type] !== gTaskId) throw new BoxError(BoxError.BAD_STATE, `Task ${gTaskId} attempted to release lock ${type} acquired by ${data[type]}`); @@ -98,7 +98,7 @@ async function releaseAll() { async function releaseByTaskId(taskId) { assert.strictEqual(typeof taskId, 'string'); - await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, debug: log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { + await promiseRetry({ times: Number.MAX_SAFE_INTEGER, interval: 100, log, retry: (error) => error.reason === BoxError.CONFLICT }, async () => { const { version, data } = await read(); for (const type of Object.keys(data)) { diff --git a/src/promise-retry.js b/src/promise-retry.js index 5489a6cb9..e2825f217 100644 --- a/src/promise-retry.js +++ b/src/promise-retry.js @@ -12,7 +12,7 @@ async function promiseRetry(options, asyncFunction) { try { return await asyncFunction(i+1 /* attempt */); } catch (error) { - if (options.debug) options.debug(`Attempt ${i+1} failed. Will retry: ${error.message} ${i === times-1 ? error.stack : ''}`); // only print stack on last error + if (options.log) options.log(`Attempt ${i+1} failed. Will retry: ${error.message} ${i === times-1 ? error.stack : ''}`); // only print stack on last error if (i === times - 1) throw error; if (options.retry && !options.retry(error)) throw error; // no more retry await timers.setTimeout(interval); diff --git a/src/services.js b/src/services.js index 8c04a4b35..25ee1f0f1 100644 --- a/src/services.js +++ b/src/services.js @@ -194,7 +194,7 @@ async function waitForContainer(containerName, tokenEnvName) { const result = await getContainerDetails(containerName, tokenEnvName); - await promiseRetry({ times: 20, interval: 15000, debug: log }, async () => { + await promiseRetry({ times: 20, interval: 15000, log }, async () => { const [networkError, response] = await safe(superagent.get(`http://${result.ip}:3000/healthcheck?access_token=${result.token}`) .timeout(20000) .ok(() => true)); @@ -237,7 +237,7 @@ async function stopUnusedServices() { for (const name of LAZY_SERVICES) { if (usedAddons.has(name)) continue; log(`stopUnusedServices: stopping ${name} (no apps use it)`); - await safe(docker.stopContainer(name), { debug: log }); + await safe(docker.stopContainer(name), { log }); } } @@ -1766,7 +1766,7 @@ async function applyServiceLimits() { changed = true; } - safe(applyMemoryLimit(id), { debug: log }); + safe(applyMemoryLimit(id), { log }); } if (changed) await settings.setJson(settings.SERVICES_CONFIG_KEY, servicesConfig); @@ -1783,7 +1783,7 @@ async function applyServiceLimits() { await apps.update(app.id, { servicesConfig: app.servicesConfig }); } - safe(applyMemoryLimit(`redis:${app.id}`), { debug: log }); + safe(applyMemoryLimit(`redis:${app.id}`), { log }); } } @@ -1877,7 +1877,7 @@ async function rebuildService(id, auditSource) { // nothing to rebuild for now. } - safe(applyMemoryLimit(id), { debug: log }); // do this in background. ok to fail + safe(applyMemoryLimit(id), { log }); // do this in background. ok to fail await eventlog.add(eventlog.ACTION_SERVICE_REBUILD, auditSource, { id }); } @@ -1916,9 +1916,9 @@ async function configureService(id, data, auditSource) { // do this in background if (needsRebuild) { - safe(rebuildService(id, auditSource), { debug: log }); + safe(rebuildService(id, auditSource), { log }); } else { - safe(applyMemoryLimit(id), { debug: log }); + safe(applyMemoryLimit(id), { log }); } await eventlog.add(eventlog.ACTION_SERVICE_CONFIGURE, auditSource, { id, data }); @@ -1950,7 +1950,7 @@ async function startServices(existingInfra, progressCallback) { await fn(existingInfra); } - safe(applyServiceLimits(), { debug: log }); + safe(applyServiceLimits(), { log }); } async function teardownOauth(app, options) { diff --git a/src/updater.js b/src/updater.js index 66cdc9725..6eb9f6c81 100644 --- a/src/updater.js +++ b/src/updater.js @@ -58,7 +58,7 @@ async function downloadBoxUrl(url, file) { safe.fs.unlinkSync(file); - await promiseRetry({ times: 10, interval: 5000, debug: log }, async function () { + await promiseRetry({ times: 10, interval: 5000, log }, async function () { log(`downloadBoxUrl: downloading ${url} to ${file}`); const [error] = await safe(shell.spawn('curl', ['-s', '--fail', url, '-o', file], { encoding: 'utf8' })); if (error) throw new BoxError(BoxError.NETWORK_ERROR, `Failed to download ${url}: ${error.message}`); @@ -363,7 +363,7 @@ async function checkForUpdates(options) { // check app updates const result = await apps.list(); for (const app of result) { - await safe(checkAppUpdate(app, options), { debug: log }); + await safe(checkAppUpdate(app, options), { log }); } // raise notifications here because the updatechecker runs regardless of auto-updater cron job