diff --git a/components/apify/actions/run-actor/run-actor.mjs b/components/apify/actions/run-actor/run-actor.mjs index 84febb9e9b6a8..d31e8a7aa589b 100644 --- a/components/apify/actions/run-actor/run-actor.mjs +++ b/components/apify/actions/run-actor/run-actor.mjs @@ -1,7 +1,7 @@ /* eslint-disable no-unused-vars */ import apify from "../../apify.app.mjs"; import { parseObject } from "../../common/utils.mjs"; -import { EVENT_TYPES } from "../../common/constants.mjs"; +import { WEBHOOK_EVENT_TYPES } from "@apify/consts"; export default { key: "apify-run-actor", @@ -186,7 +186,7 @@ export default { type: "string[]", label: "Event Types", description: "The types of events to send to the webhook", - options: EVENT_TYPES, + options: Object.values(WEBHOOK_EVENT_TYPES), }; } return props; diff --git a/components/apify/actions/scrape-single-url/scrape-single-url.mjs b/components/apify/actions/scrape-single-url/scrape-single-url.mjs index b4c73f0e540cc..e00d6c3d40bf6 100644 --- a/components/apify/actions/scrape-single-url/scrape-single-url.mjs +++ b/components/apify/actions/scrape-single-url/scrape-single-url.mjs @@ -1,5 +1,8 @@ import apify from "../../apify.app.mjs"; import { ACTOR_ID } from "../../common/constants.mjs"; +import { + ACTOR_JOB_STATUSES, ACTOR_JOB_TERMINAL_STATUSES, +} from "@apify/consts"; export default { key: "apify-scrape-single-url", @@ -36,7 +39,7 @@ export default { }, }, async run({ $ }) { - const response = await this.apify.runActor({ + const startActorResponse = await this.apify.runActorAsynchronously({ $, actorId: ACTOR_ID, data: { @@ -51,7 +54,46 @@ export default { ], }, }); + + const { + data: { + id: runId, defaultDatasetId, + }, + } = startActorResponse; + + let actorRunStatus = null; + let retries = 0; + const maxRetries = 30; + const delay = 1000; + + while ((!actorRunStatus || !ACTOR_JOB_TERMINAL_STATUSES.includes(actorRunStatus)) + && retries < maxRetries + ) { + await new Promise((resolve) => setTimeout(resolve, delay)); + const runDetails = await this.apify.getActorRun({ + $, + runId, + }); + actorRunStatus = runDetails.data.status; + retries++; + } + + if (actorRunStatus !== ACTOR_JOB_STATUSES.SUCCEEDED) { + throw new Error(`Actor run did not succeed. Final status: ${actorRunStatus}`); + } + + const datasetResponse = await this.apify.listDatasetItems({ + $, + datasetId: defaultDatasetId, + params: { + limit: 1, + offset: 0, + }, + }); + + console.log(datasetResponse); + $.export("$summary", `Successfully scraped content from ${this.url}`); - return response; + return datasetResponse[0]; }, }; diff --git a/components/apify/apify.app.mjs b/components/apify/apify.app.mjs index ad0fb25c9773d..21021543ac342 100644 --- a/components/apify/apify.app.mjs +++ b/components/apify/apify.app.mjs @@ -196,6 +196,15 @@ export default { ...opts, }); }, + getActorRun({ + runId, ...opts + }) { + return this._makeRequest({ + method: "GET", + path: `/actor-runs/${runId}`, + ...opts, + }); + }, runActorAsynchronously({ actorId, ...opts }) { diff --git a/components/apify/common/constants.mjs b/components/apify/common/constants.mjs index 869e38c5bd157..a16d51a32af1a 100644 --- a/components/apify/common/constants.mjs +++ b/components/apify/common/constants.mjs @@ -1,11 +1,2 @@ export const ACTOR_ID = "aYG0l9s7dbB7j3gbS"; export const LIMIT = 100; - -export const EVENT_TYPES = [ - "ACTOR.RUN.CREATED", - "ACTOR.RUN.SUCCEEDED", - "ACTOR.RUN.FAILED", - "ACTOR.RUN.ABORTED", - "ACTOR.RUN.TIMED_OUT", - "ACTOR.RUN.RESURRECTED", -];