Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Bugfix of Apify.call() - add delay if getRun() returns null, added unit test

Jan Curn82a169985748
diff --git a/src/actor.js b/src/actor.js
index 7cd517258..ad408d8b9 100644
--- a/src/actor.js
+++ b/src/actor.js
@@ -657,8 +657,15 @@ export const call = (actId, input, opts = {}, callback) => {
return acts
.getRun(Object.assign({}, defaultOpts, { waitForFinish, runId: run.id }))
.then((updatedRun) => {
- // Note that it might take some time for db replicas to get up-to-date, so getRun() might return null
- if (!updatedRun) return waitForRunToFinish(run);
+ // It might take some time for database replicas to get up-to-date,
+ // so getRun() might return null. Wait a little while and try it again.
+ if (!updatedRun) {
+ return new Promise(resolve => setTimeout(resolve, 250))
+ .then(() => {
+ return waitForRunToFinish(run);
+ });
+ }
+
if (!_.contains(ACT_TASK_TERMINAL_STATUSES, updatedRun.status)) return waitForRunToFinish(updatedRun);
if (!fetchOutput) return updatedRun;
diff --git a/test/actor.js b/test/actor.js
index 991fac53b..52880335f 100644
--- a/test/actor.js
+++ b/test/actor.js
@@ -1370,6 +1370,48 @@ describe('Apify.call()', () => {
actsMock.restore();
});
});
+
+ it('handles getRun() returning null the first time', () => {
+ const actId = 'some-act-id';
+ const token = 'some-token';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const runningRun = Object.assign({}, run, { status: 'RUNNING' });
+ const finishedRun = Object.assign({}, run, { status: ACT_TASK_STATUSES.SUCCEEDED });
+ const input = 'something';
+ const contentType = 'text/plain';
+ const output = { contentType, body: 'some-output' };
+ const expected = Object.assign({}, finishedRun, { output });
+ const build = 'xxx';
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('runAct')
+ .withExactArgs({ token, actId, contentType: `${contentType}; charset=utf-8`, body: input, build })
+ .once()
+ .returns(Promise.resolve(runningRun));
+ actsMock.expects('getRun')
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })
+ .twice()
+ .returns(Promise.resolve(null));
+ actsMock.expects('getRun')
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })
+ .once()
+ .returns(Promise.resolve(finishedRun));
+
+ const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
+ keyValueStoresMock.expects('getRecord')
+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', disableBodyParser: true })
+ .once()
+ .returns(Promise.resolve(output));
+
+ return Apify
+ .call(actId, input, { contentType, token, disableBodyParser: true, build })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(expected);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ });
+ });
});
describe('Apify.getMemoryInfo()', () => {