Skip to content
4 changes: 2 additions & 2 deletions components/apify/actions/run-actor/run-actor.mjs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 44 additions & 2 deletions components/apify/actions/scrape-single-url/scrape-single-url.mjs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -36,7 +39,7 @@ export default {
},
},
async run({ $ }) {
const response = await this.apify.runActor({
const startActorResponse = await this.apify.runActorAsynchronously({
$,
actorId: ACTOR_ID,
data: {
Expand All @@ -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];
},
};
9 changes: 9 additions & 0 deletions components/apify/apify.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ export default {
...opts,
});
},
getActorRun({
runId, ...opts
}) {
return this._makeRequest({
method: "GET",
path: `/actor-runs/${runId}`,
...opts,
});
},
runActorAsynchronously({
actorId, ...opts
}) {
Expand Down
9 changes: 0 additions & 9 deletions components/apify/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -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",
];
Loading