From 7e8452471460a2aff2a1bc4c137bd8b95383c970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Ko=C5=BAmi=C5=84ski?= Date: Thu, 10 Sep 2026 12:04:32 +0200 Subject: [PATCH] chore: upgrade @orbito/api-lib to 3.3.3 Lock TTL is now passed in seconds, matching what the core lock RPC methods expect and what the 3.x SDK documents as ttlSeconds. The presta-api timeout test no longer drains the event loop: it drives the unref-ed timeout timer through node:test mock timers instead of real ones. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01LpoFvfAbnMmkSJFRc867XA --- index.js | 8 +++++--- package-lock.json | 8 ++++---- package.json | 2 +- src/job-runtime.js | 17 +++++++++++++---- test/job-runtime.test.js | 27 +++++++++++++++++++++++++-- test/presta-api.test.js | 10 +++++++++- 6 files changed, 57 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 50b1b1a..5b0f3e6 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,8 @@ const { createJobRuntime } = require('./src/job-runtime'); const { ensureShopIcon } = require('./src/shop-icon'); const NEW_ORDERS_WINDOW_MS = 20 * 60 * 1000; +// TTL locków cronowych w sekundach (jednostka oczekiwana przez Core.getLock/Core.extendLockTime). +const JOB_LOCK_TTL_SECONDS = 5 * 60; const runtime = createJobRuntime(Core, Log); Utils.loadMethods(__dirname + '/methods'); @@ -75,13 +77,13 @@ async function syncShopIcon() { const jobs = [ new CronJob('0 */5 * * * *', () => runtime.track(() => - runtime.withRenewedLock('prestashop-orders-puller', 5 * 60 * 1000, runOrdersPuller) + runtime.withRenewedLock('prestashop-orders-puller', JOB_LOCK_TTL_SECONDS, runOrdersPuller) )), new CronJob('0 */3 * * * *', () => runtime.track(() => - runtime.withRenewedLock('prestashop-payments-sync', 5 * 60 * 1000, valid => runPaymentSync('recent', valid)) + runtime.withRenewedLock('prestashop-payments-sync', JOB_LOCK_TTL_SECONDS, valid => runPaymentSync('recent', valid)) )), new CronJob('30 */30 * * * *', () => runtime.track(() => - runtime.withRenewedLock('prestashop-payments-sync', 5 * 60 * 1000, valid => runPaymentSync('older', valid)) + runtime.withRenewedLock('prestashop-payments-sync', JOB_LOCK_TTL_SECONDS, valid => runPaymentSync('older', valid)) )), ]; diff --git a/package-lock.json b/package-lock.json index 651f21c..40ec78b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.1.4", "license": "ISC", "dependencies": { - "@orbito/api-lib": "^2.2.1", + "@orbito/api-lib": "^3.3.3", "cron": "^4.4.0", "dotenv": "^17.2.3", "ejs": "^4.0.1", @@ -20,9 +20,9 @@ } }, "node_modules/@orbito/api-lib": { - "version": "2.2.1", - "resolved": "https://git.kozminski.net.pl/api/packages/orbito/npm/%40orbito%2Fapi-lib/-/2.2.1/api-lib-2.2.1.tgz", - "integrity": "sha512-y0xFkU8IIObdniRiuEX9t4+gOxdwA3t6+a7xkJVvAogB4+4r+7Vlo9VNW9w6GABXX5xTrSIsPRkx/I1lnVdsVQ==", + "version": "3.3.3", + "resolved": "https://git.kozminski.net.pl/api/packages/orbito/npm/%40orbito%2Fapi-lib/-/3.3.3/api-lib-3.3.3.tgz", + "integrity": "sha512-+YyN/FdLEoCCkKVcHzHopgB7/sFSXEn61l+Y+Jz/Ccb2oXe9EZ5MZ8+M1fWjA03sVzUO7HqDEWUOatgDdyikoQ==", "license": "ISC", "dependencies": { "bson": "^6.10.4", diff --git a/package.json b/package.json index 403142d..fcda0da 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "author": "", "license": "ISC", "dependencies": { - "@orbito/api-lib": "^2.2.1", + "@orbito/api-lib": "^3.3.3", "cron": "^4.4.0", "dotenv": "^17.2.3", "ejs": "^4.0.1", diff --git a/src/job-runtime.js b/src/job-runtime.js index f091f6f..1688b4a 100644 --- a/src/job-runtime.js +++ b/src/job-runtime.js @@ -13,21 +13,30 @@ function createJobRuntime(Core, Log = console) { return promise; } - async function withRenewedLock(lockName, ttlMs, operation) { - const lockId = await Core.getLock(lockName, ttlMs); + /** + * Uruchamia operację pod rozproszonym lockiem, odnawiając go w tle. + * + * @param {string} lockName - Nazwa locka + * @param {number} ttlSeconds - TTL locka w SEKUNDACH (jednostka wymagana przez Core.getLock/Core.extendLockTime) + * @param {(isLockValid: () => boolean) => Promise<*>} operation - Operacja do wykonania pod lockiem + */ + async function withRenewedLock(lockName, ttlSeconds, operation) { + const lockId = await Core.getLock(lockName, ttlSeconds); if (!lockId) return null; let lockValid = true; let renewal = null; + // Odnawiamy trzy razy częściej niż wynosi TTL; setInterval oczekuje milisekund. + const renewIntervalMs = Math.max(1000, Math.floor(ttlSeconds * 1000 / 3)); const timer = setInterval(() => { if (renewal || !lockValid) return; - renewal = Core.extendLockTime(lockId, ttlMs) + renewal = Core.extendLockTime(lockId, ttlSeconds) .catch(error => { lockValid = false; Log.error('PrestaShop cron lock renewal failed', { lockName, error: error.message }); }) .finally(() => { renewal = null; }); - }, Math.max(1000, Math.floor(ttlMs / 3))); + }, renewIntervalMs); timer.unref?.(); try { diff --git a/test/job-runtime.test.js b/test/job-runtime.test.js index 3c158f6..cc97286 100644 --- a/test/job-runtime.test.js +++ b/test/job-runtime.test.js @@ -11,8 +11,8 @@ test('lock is released after success and after failure', async () => { }; const runtime = createJobRuntime(Core, { error() {}, warn() {} }); - assert.equal(await runtime.withRenewedLock('ok', 5000, async valid => valid() && 7), 7); - await assert.rejects(() => runtime.withRenewedLock('fail', 5000, async () => { throw new Error('boom'); }), /boom/); + assert.equal(await runtime.withRenewedLock('ok', 5, async valid => valid() && 7), 7); + await assert.rejects(() => runtime.withRenewedLock('fail', 5, async () => { throw new Error('boom'); }), /boom/); assert.deepEqual(released, ['ok', 'fail']); }); @@ -29,3 +29,26 @@ test('stop waits for tracked work and blocks subsequent runs until start', async runtime.start(); assert.equal(await runtime.track(async () => 'running'), 'running'); }); + +test('lock TTL is forwarded to Core in seconds, without conversion', async t => { + t.mock.timers.enable({ apis: ['setInterval'] }); + const getLockCalls = []; + const extendCalls = []; + const Core = { + getLock: async (name, ttl) => { getLockCalls.push([name, ttl]); return 'lock-1'; }, + extendLockTime: async (id, ttl) => { extendCalls.push([id, ttl]); return true; }, + releaseLock: async () => true, + }; + const runtime = createJobRuntime(Core, { error() {}, warn() {} }); + + let finish; + const run = runtime.withRenewedLock('ttl', 5, () => new Promise(resolve => { finish = resolve; })); + await new Promise(resolve => setImmediate(resolve)); + // Interwał odnowienia to ttlSeconds * 1000 / 3 = ~1666 ms, więc jeden tick wystarczy. + t.mock.timers.tick(1700); + finish('done'); + assert.equal(await run, 'done'); + + assert.deepEqual(getLockCalls, [['ttl', 5]]); + assert.deepEqual(extendCalls, [['lock-1', 5]]); +}); diff --git a/test/presta-api.test.js b/test/presta-api.test.js index 832c7c6..fbb4404 100644 --- a/test/presta-api.test.js +++ b/test/presta-api.test.js @@ -56,12 +56,20 @@ test('rejects non-retryable 4xx and invalid JSON', async t => { test('times out stalled requests', async t => { const originalFetch = Core.fetch; t.after(() => { Core.fetch = originalFetch; }); + // Timer timeoutu w fetchWithTimeout jest `unref`-owany, a Core.fetch nigdy się tu nie rozwiązuje, + // więc na prawdziwych timerach pętla zdarzeń pustoszeje i node:test anuluje ten test. + // Mockowany setTimeout pozwala wysterować timeout ręcznie przez tick(). + t.mock.timers.enable({ apis: ['setTimeout'] }); Core.fetch = async () => new Promise(() => {}); const api = new PrestashopApi('https://shop.example', 'secret', true, { timeoutMs: 5, maxGetAttempts: 1, }); - await assert.rejects(() => api.makeRequest('orders', 'GET'), /timed out/); + const pending = api.makeRequest('orders', 'GET'); + await new Promise(resolve => setImmediate(resolve)); + t.mock.timers.tick(10); + + await assert.rejects(() => pending, /timed out/); }); test('preserves the explicitly allowed product partial-success compatibility path', async t => {