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.

Rewriting call() and adding callTask()

Marek Trunkateab0bb5e95c3
diff --git a/src/actor.js b/src/actor.js
index 4a3890596..0ff148786 100644
--- a/src/actor.js
+++ b/src/actor.js
@@ -24,6 +24,44 @@ const tryParseDate = (str) => {
return unix > 0 ? new Date(unix) : undefined;
};
+/**
+ * Waits for given run to finish. If "timeoutSecs" is reached then returns unfinished run.
+ *
+ * @ignore
+ */
+const waitForRunToFinish = async ({ actId, runId, token, timeoutSecs }) => {
+ let updatedRun;
+
+ const { acts } = apifyClient;
+ const startedAt = Date.now();
+ const shouldRepeat = () => {
+ if (timeoutSecs && (Date.now() - startedAt) / 1000 >= timeoutSecs) return false;
+ if (updatedRun && ACT_JOB_TERMINAL_STATUSES.includes(updatedRun.status)) return false;
+
+ return true;
+ };
+
+ while (shouldRepeat()) {
+ const waitForFinish = timeoutSecs
+ ? Math.round(timeoutSecs - (Date.now() - startedAt) / 1000)
+ : 999999;
+
+ updatedRun = await acts.getRun({ actId, runId, token, waitForFinish });
+
+ // It might take some time for database replicas to get up-to-date,
+ // so getRun() might return null. Wait a little bit and try it again.
+ if (!updatedRun) await Promise.delay(250);
+ }
+
+ if (!updatedRun) throw new ApifyCallError({ id: runId, actId }, 'Apify.call() failed, cannot fetch run from a server');
+ if (
+ updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED
+ && updatedRun.status !== ACT_JOB_STATUSES.RUNNING
+ ) throw new ApifyCallError(updatedRun);
+
+ return updatedRun;
+};
+
/**
* Returns a new object which contains information parsed from the `APIFY_XXX` environment variables.
* It has the following properties:
@@ -198,7 +236,6 @@ export const main = (userFunc) => {
}
};
-
/**
* Runs an actor on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
* waits for the actor to finish and fetches its output.
@@ -242,11 +279,11 @@ export const main = (userFunc) => {
* @param {String} [options.build]
* Tag or number of the actor build to run (e.g. `beta` or `1.2.345`).
* If not provided, the run uses build tag or number from the default actor run configuration (typically `latest`).
- * @param {String} [options.waitSecs]
+ * @param {String} [options.timeoutSecs]
* Maximum time to wait for the actor run to finish, in seconds.
* If the limit is reached, the returned promise is resolved to a run object that will have
* status `READY` or `RUNNING` and it will not contain the actor run output.
- * If `waitSecs` is null or undefined, the function waits for the actor to finish (default behavior).
+ * If `timeoutSecs` is null or undefined, the function waits for the actor to finish (default behavior).
* @param {Boolean} [options.fetchOutput=true]
* If `false` then the function does not fetch output of the actor.
* @param {Boolean} [options.disableBodyParser=false]
@@ -259,97 +296,148 @@ export const main = (userFunc) => {
* @function
* @name call
*/
-export const call = (actId, input, options = {}) => {
+export const call = async (actId, input, options = {}) => {
const { acts, keyValueStores } = apifyClient;
+ // Use optionsCopy here as maybeStringify() may override contentType
+ const optionsCopy = Object.assign({}, options);
checkParamOrThrow(actId, 'actId', 'String');
checkParamOrThrow(options, 'opts', 'Object');
// Common options.
- const { token } = options;
+ const { token } = optionsCopy;
checkParamOrThrow(token, 'token', 'Maybe String');
- const defaultOpts = { actId };
- if (token) defaultOpts.token = token;
// RunAct() options.
- const { build, memory } = options;
+ const { build, memory } = optionsCopy;
const runActOpts = {};
checkParamOrThrow(build, 'build', 'Maybe String');
checkParamOrThrow(memory, 'memory', 'Maybe Number');
if (build) runActOpts.build = build;
if (memory) runActOpts.memory = memory;
-
if (input) {
- input = maybeStringify(input, options);
+ input = maybeStringify(input, optionsCopy);
checkParamOrThrow(input, 'input', 'Buffer|String');
- checkParamOrThrow(options.contentType, 'contentType', 'String');
+ checkParamOrThrow(optionsCopy.contentType, 'contentType', 'String');
- if (options.contentType) runActOpts.contentType = addCharsetToContentType(options.contentType);
+ if (optionsCopy.contentType) runActOpts.contentType = addCharsetToContentType(optionsCopy.contentType);
runActOpts.body = input;
}
- // GetAct() options.
- const { timeoutSecs, fetchOutput = true } = options;
- let { waitSecs } = options;
- // TODO: This is bad, timeoutSecs should be used! Fix this and mark as breaking change!!!
- // The comments should be * @param {Number} [options.timeoutSecs]
- // * Time limit for the finish of the actor run, in seconds.
- // * If the limit is reached the resulting run will have the `TIMED-OUT` status.
- // * If not provided, the run uses timeout specified in the default actor run configuration.
- // And there's no point to have waitSecs more than
- // 30 secs anyway, since connection will break.
- // Backwards compatibility: waitSecs used to be called timeoutSecs
- if (typeof timeoutSecs === 'number' && typeof waitSecs !== 'number') waitSecs = timeoutSecs;
- checkParamOrThrow(waitSecs, 'waitSecs', 'Maybe Number');
- checkParamOrThrow(fetchOutput, 'fetchOutput', 'Boolean');
- const waitUntil = typeof waitSecs === 'number' ? Date.now() + (waitSecs * 1000) : null;
-
- // GetRecord() options.
- const { disableBodyParser } = options;
- checkParamOrThrow(disableBodyParser, 'disableBodyParser', 'Maybe Boolean');
-
- // Adds run.output field to given run and returns it.
- const addOutputToRun = (run) => {
- const getRecordOpts = { key: 'OUTPUT', storeId: run.defaultKeyValueStoreId };
- if (disableBodyParser) getRecordOpts.disableBodyParser = disableBodyParser;
-
- return keyValueStores
- .getRecord(getRecordOpts)
- .then(output => Object.assign({}, run, { output }));
- };
+ // Run actor.
+ const { timeoutSecs } = options;
+ checkParamOrThrow(timeoutSecs, 'timeoutSecs', 'Maybe Number');
+ const run = await acts.runAct(Object.assign({ actId, token }, runActOpts));
+ if (timeoutSecs <= 0) return run; // In this case there is nothing more to do.
+
+ // Wait for run to finish.
+ const updatedRun = await waitForRunToFinish({
+ actId,
+ runId: run.id,
+ token,
+ timeoutSecs,
+ });
+
+ // Finish if output is not requested or run haven't finished.
+ const { fetchOutput = true } = options;
+ if (!fetchOutput || updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED) return updatedRun;
+
+ // Fetch output.
+ const { disableBodyParser = false } = options;
+ checkParamOrThrow(disableBodyParser, 'disableBodyParser', 'Boolean');
+ const output = await keyValueStores.getRecord({
+ key: 'OUTPUT',
+ storeId: updatedRun.defaultKeyValueStoreId,
+ disableBodyParser,
+ });
+
+ return Object.assign({}, updatedRun, { output });
+};
- // Keeps requesting given run until it gets finished or timeout is reached.
- const waitForRunToFinish = (run) => {
- const waitForFinish = waitUntil !== null ? Math.round((waitUntil - Date.now()) / 1000) : 999999;
-
- // We are timing out ...
- if (waitForFinish <= 0) return Promise.resolve(run);
-
- return acts
- .getRun(Object.assign({}, defaultOpts, { waitForFinish, runId: run.id }))
- .then((updatedRun) => {
- // It might take some time for database replicas to get up-to-date,
- // so getRun() might return null. Wait a little bit and try it again.
- if (!updatedRun) {
- return Promise.delay(250).then(() => {
- return waitForRunToFinish(run);
- });
- }
+/**
+ * Runs an actor actor on the Apify platform using the current user account (determined by the `APIFY_TOKEN` environment variable),
+ * waits for the actor to finish and fetches its output.
+ *
+ * By passing the `waitSecs` option you can reduce the maximum amount of time to wait for the run to finish.
+ * If the value is less than or equal to zero, the function returns immediately after the run is started.
+ *
+ * The result of the function is an {@link ActorRun} object
+ * that contains details about the actor run and its output (if any).
+ * If the actor run failed, the function fails with {@link ApifyCallError} exception.
+ *
+ * **Example usage:**
+ *
+ * ```javascript
+ * const run = await Apify.call('apify/hello-world-task');
+ * console.log(`Received message: ${run.output.body.message}`);
+ * ```
+ *
+ * Internally, the `call()` function calls the
+ * <a href="https://www.apify.com/docs/api/v2#/reference/actor-tasks/runs-collection/run-task-asynchronously" target="_blank">Run actor</a>
+ * Apify API endpoint and few others to obtain the output.
+ *
+ * @param {String} taskId
+ * Either `username/task-name` or task ID.
+ * @param {Object|String|Buffer} [input]
+ * This parameter is not supported yet. You must pass either `null` or `undefined` value!
+ * @param {Object} [options]
+ * Object with the settings below:
+ * @param {String} [options.token]
+ * User API token that is used to run the actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
+ * @param {String} [options.timeoutSecs]
+ * Maximum time to wait for the actor run to finish, in seconds.
+ * If the limit is reached, the returned promise is resolved to a run object that will have
+ * status `READY` or `RUNNING` and it will not contain the actor run output.
+ * If `timeoutSecs` is null or undefined, the function waits for the actor to finish (default behavior).
+ * @returns {Promise<ActorRun>}
+ * @throws {ApifyCallError} If the run did not succeed, e.g. if it failed or timed out.
+ *
+ * @memberof module:Apify
+ * @function
+ * @name call
+ */
+export const callTask = async (taskId, input, options = {}) => {
+ const { tasks, keyValueStores } = apifyClient;
- if (!_.contains(ACT_JOB_TERMINAL_STATUSES, updatedRun.status)) return waitForRunToFinish(updatedRun);
- if (updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED) throw new ApifyCallError(updatedRun);
- if (!fetchOutput) return updatedRun;
+ if (input) throw new Error('Parameter "input" of Apify.callTask() is not supported yet!');
- return addOutputToRun(updatedRun);
- });
- };
+ checkParamOrThrow(taskId, 'taskId', 'String');
+ checkParamOrThrow(options, 'opts', 'Object');
- return acts
- .runAct(Object.assign({}, defaultOpts, runActOpts))
- .then(run => waitForRunToFinish(run));
-};
+ // Common options.
+ const { token } = options;
+ checkParamOrThrow(token, 'token', 'Maybe String');
+ // Run task.
+ const { timeoutSecs } = options;
+ checkParamOrThrow(timeoutSecs, 'timeoutSecs', 'Maybe Number');
+ const run = await tasks.runTask({ taskId, token });
+ if (timeoutSecs <= 0) return run; // In this case there is nothing more to do.
+
+ // Wait for run to finish.
+ const updatedRun = await waitForRunToFinish({
+ actId: run.actId,
+ runId: run.id,
+ token,
+ timeoutSecs,
+ });
+
+ // Finish if output is not requested or run haven't finished.
+ const { fetchOutput = true } = options;
+ if (!fetchOutput || updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED) return updatedRun;
+
+ // Fetch output.
+ const { disableBodyParser = false } = options;
+ checkParamOrThrow(disableBodyParser, 'disableBodyParser', 'Boolean');
+ const output = await keyValueStores.getRecord({
+ key: 'OUTPUT',
+ storeId: updatedRun.defaultKeyValueStoreId,
+ disableBodyParser,
+ });
+
+ return Object.assign({}, updatedRun, { output });
+};
/**
* Represents information about an actor run, as returned by the
diff --git a/src/errors.js b/src/errors.js
index cc72bbd98..6f0b74cd3 100644
--- a/src/errors.js
+++ b/src/errors.js
@@ -13,8 +13,8 @@ export const APIFY_CALL_ERROR_NAME = 'ApifyCallError';
* Object representing the failed actor run.
*/
export class ApifyCallError extends Error {
- constructor(run) {
- super(`The actor invoked by Apify.call() did not succeed (run ID: ${run.id})`);
+ constructor(run, message = 'The actor invoked by Apify.call() did not succeed') {
+ super(`${message} (run ID: ${run.id})`);
this.name = APIFY_CALL_ERROR_NAME;
this.run = run;
diff --git a/src/index.js b/src/index.js
index a669058a6..9e5c87225 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,6 +1,6 @@
import log from 'apify-shared/log';
import { ENV_VARS } from 'apify-shared/consts';
-import { main, getEnv, call, getApifyProxyUrl } from './actor';
+import { main, getEnv, call, callTask, getApifyProxyUrl } from './actor';
import AutoscaledPool from './autoscaling/autoscaled_pool';
import BasicCrawler from './basic_crawler';
import CheerioCrawler from './cheerio_crawler';
@@ -43,6 +43,7 @@ module.exports = {
main,
getEnv,
call,
+ callTask,
getMemoryInfo,
getApifyProxyUrl,
isAtHome,
diff --git a/src/puppeteer.js b/src/puppeteer.js
index a62858277..492ab8b38 100644
--- a/src/puppeteer.js
+++ b/src/puppeteer.js
@@ -163,7 +163,8 @@ const getPuppeteerOrThrow = () => {
* <a href="https://www.apify.com/docs/actor#base-images" target="_blank">Apify Actor documentation</a>
* for details.
*
- * For an example of usage, see the [Synchronous run Example](../examples/synchronousrun) or the [Puppeteer proxy Example](../examples/puppeteerwithproxy)
+ * For an example of usage, see the [Synchronous run Example](../examples/synchronousrun)
+ * or the [Puppeteer proxy Example](../examples/puppeteerwithproxy)
*
* @param {LaunchPuppeteerOptions} [options]
* Optional settings passed to `puppeteer.launch()`. In addition to
diff --git a/test/actor.js b/test/actor.js
index e581272ed..d00e12831 100644
--- a/test/actor.js
+++ b/test/actor.js
@@ -288,21 +288,21 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId })
+ .withExactArgs({ actId, token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(finishedRun));
const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
keyValueStoresMock.expects('getRecord')
- .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT' })
+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', disableBodyParser: false })
.once()
.returns(Promise.resolve(output));
@@ -329,21 +329,21 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId })
+ .withExactArgs({ actId, token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(finishedRun));
const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
keyValueStoresMock.expects('getRecord')
- .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT' })
+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', disableBodyParser: false })
.once()
.returns(Promise.resolve(output));
@@ -371,22 +371,22 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId, contentType: 'application/json; charset=utf-8', body: JSON.stringify(input, null, 2) })
+ .withExactArgs({ actId, contentType: 'application/json; charset=utf-8', body: JSON.stringify(input, null, 2), token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
.once()
.returns(Promise.resolve(finishedRun));
const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
keyValueStoresMock.expects('getRecord')
- .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT' })
+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', disableBodyParser: false })
.once()
.returns(Promise.resolve(output));
@@ -480,7 +480,7 @@ describe('Apify.call()', () => {
const defaultKeyValueStoreId = 'some-store-id';
const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
const runningRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.RUNNING });
- const waitSecs = 1;
+ const timeoutSecs = 1;
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
@@ -488,17 +488,17 @@ describe('Apify.call()', () => {
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ token, actId, runId: run.id, waitForFinish: waitSecs })
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: timeoutSecs })
.once()
.returns(new Promise((resolve) => {
- setTimeout(() => resolve(runningRun), waitSecs * 1000);
+ setTimeout(() => resolve(runningRun), timeoutSecs * 1000);
}));
const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
keyValueStoresMock.expects('getRecord').never();
return Apify
- .call(actId, null, { token, waitSecs })
+ .call(actId, null, { token, timeoutSecs })
.then((callOutput) => {
expect(callOutput).to.be.eql(runningRun);
keyValueStoresMock.restore();
@@ -554,7 +554,7 @@ describe('Apify.call()', () => {
const defaultKeyValueStoreId = 'some-store-id';
const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
const readyRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.READY });
- const waitSecs = 0;
+ const timeoutSecs = 0;
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
@@ -567,7 +567,7 @@ describe('Apify.call()', () => {
keyValueStoresMock.expects('getRecord').never();
return Apify
- .call(actId, null, { token, waitSecs })
+ .call(actId, null, { token, timeoutSecs })
.then((callOutput) => {
expect(callOutput).to.be.eql(readyRun);
keyValueStoresMock.restore();
@@ -598,6 +598,232 @@ describe('Apify.call()', () => {
expect(err).to.be.instanceOf(ApifyCallError);
expect(err.run.status).to.be.eql(ACT_JOB_STATUSES.ABORTED);
expect(err.run).to.be.eql(failedRun);
+ actsMock.restore();
+ });
+ });
+});
+
+describe('Apify.callTask()', () => {
+ it('works as expected', () => {
+ const taskId = 'some-act-id';
+ const actId = 'xxx';
+ const token = 'some-token';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const runningRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.RUNNING });
+ const finishedRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.SUCCEEDED });
+ const output = { contentType: 'application/json', body: 'some-output' };
+ const expected = Object.assign({}, finishedRun, { output });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token, taskId })
+ .once()
+ .returns(Promise.resolve(runningRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('getRun')
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })
+ .once()
+ .returns(Promise.resolve(runningRun));
+ 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
+ .callTask(taskId, undefined, { token, disableBodyParser: true })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(expected);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ tasksMock.restore();
+ });
+ });
+
+ it('works as expected with fetchOutput = false', () => {
+ const taskId = 'some-act-id';
+ const actId = 'xxx';
+ const token = 'some-token';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const runningRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.RUNNING });
+ const finishedRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.SUCCEEDED });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token, taskId })
+ .once()
+ .returns(Promise.resolve(runningRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('getRun')
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })
+ .once()
+ .returns(Promise.resolve(runningRun));
+ 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').never();
+
+ return Apify
+ .callTask(taskId, undefined, { token, disableBodyParser: true, fetchOutput: false })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(finishedRun);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ tasksMock.restore();
+ });
+ });
+
+ it('timeouts as expected with unfinished run', () => {
+ const timeoutSecs = 1;
+ const taskId = 'some-act-id';
+ const actId = 'xxx';
+ const token = 'some-token';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const runningRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.RUNNING });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token, taskId })
+ .once()
+ .returns(Promise.resolve(runningRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('getRun')
+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: timeoutSecs })
+ .once()
+ .returns(new Promise((resolve) => {
+ setTimeout(() => resolve(runningRun), timeoutSecs * 1000);
+ }));
+
+ const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
+ keyValueStoresMock.expects('getRecord').never();
+
+ return Apify
+ .callTask(taskId, undefined, { token, disableBodyParser: true, fetchOutput: false, timeoutSecs })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(runningRun);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ tasksMock.restore();
+ });
+ });
+
+ it('handles getRun() returning null the first time', () => {
+ const actId = 'some-act-id';
+ const taskId = '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: ACT_JOB_STATUSES.RUNNING });
+ const finishedRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.SUCCEEDED });
+ const contentType = 'text/plain';
+ const output = { contentType, body: 'some-output' };
+ const expected = Object.assign({}, finishedRun, { output });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token, taskId })
+ .once()
+ .returns(Promise.resolve(runningRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ 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
+ .callTask(taskId, undefined, { token, disableBodyParser: true })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(expected);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ tasksMock.restore();
+ });
+ });
+
+ it('returns immediately with zero timeout', () => {
+ const timeoutSecs = 0;
+ const taskId = 'some-act-id';
+ const actId = 'xxx';
+ const token = 'some-token';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const readyRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.READY });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token, taskId })
+ .once()
+ .returns(Promise.resolve(readyRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('getRun').never();
+
+ const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);
+ keyValueStoresMock.expects('getRecord').never();
+
+ return Apify
+ .callTask(taskId, undefined, { token, timeoutSecs })
+ .then((callOutput) => {
+ expect(callOutput).to.be.eql(readyRun);
+ keyValueStoresMock.restore();
+ actsMock.restore();
+ tasksMock.restore();
+ });
+ });
+
+ it('throws if run doesn\'t succeed', () => {
+ const taskId = 'some-act-id';
+ const actId = 'xxx';
+ const defaultKeyValueStoreId = 'some-store-id';
+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };
+ const readyRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.READY });
+ const failedRun = Object.assign({}, run, { status: ACT_JOB_STATUSES.ABORTED });
+
+ const tasksMock = sinon.mock(Apify.client.tasks);
+ tasksMock.expects('runTask')
+ .withExactArgs({ token: undefined, taskId })
+ .once()
+ .returns(Promise.resolve(readyRun));
+
+ const actsMock = sinon.mock(Apify.client.acts);
+ actsMock.expects('getRun')
+ .withExactArgs({ token: undefined, actId, runId: run.id, waitForFinish: 999999 })
+ .once()
+ .returns(Promise.resolve(failedRun));
+
+ return Apify
+ .callTask(taskId)
+ .catch((err) => {
+ expect(err).to.be.instanceOf(ApifyCallError);
+ expect(err.run.status).to.be.eql(ACT_JOB_STATUSES.ABORTED);
+ expect(err.run).to.be.eql(failedRun);
+ actsMock.restore();
+ tasksMock.restore();
});
});
});