Skip to content

Commit 9b61d71

Browse files
committed
updates
1 parent 9ae9d77 commit 9b61d71

File tree

2 files changed

+147
-29
lines changed

2 files changed

+147
-29
lines changed

components/apify/actions/run-actor/run-actor.mjs

Lines changed: 119 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-unused-vars */
22
import apify from "../../apify.app.mjs";
3+
import { parseObject } from "../../common/utils.mjs";
34

45
export default {
56
key: "apify-run-actor",
@@ -14,8 +15,53 @@ export default {
1415
apify,
1516
"actorId",
1617
],
18+
},
19+
buildId: {
20+
propDefinition: [
21+
apify,
22+
"buildId",
23+
(c) => ({
24+
actorId: c.actorId,
25+
}),
26+
],
27+
reloadProps: true,
28+
},
29+
runAsynchronously: {
30+
type: "boolean",
31+
label: "Run Asynchronously",
32+
description: "Set to `true` to run the actor asynchronously",
1733
reloadProps: true,
1834
},
35+
timeout: {
36+
type: "string",
37+
label: "Timeout",
38+
description: "Optional timeout for the run, in seconds. By default, the run uses a timeout specified in the default run configuration for the Actor.",
39+
optional: true,
40+
},
41+
memory: {
42+
type: "string",
43+
label: "Memory",
44+
description: "Memory limit for the run, in megabytes. The amount of memory can be set to a power of 2 with a minimum of 128. By default, the run uses a memory limit specified in the default run configuration for the Actor.",
45+
optional: true,
46+
},
47+
maxItems: {
48+
type: "string",
49+
label: "Max Items",
50+
description: "The maximum number of items that the Actor run should return. This is useful for pay-per-result Actors, as it allows you to limit the number of results that will be charged to your subscription. You can access the maximum number of items in your Actor by using the ACTOR_MAX_PAID_DATASET_ITEMS environment variable.",
51+
optional: true,
52+
},
53+
maxTotalChargeUsd: {
54+
type: "string",
55+
label: "Max Total Charge USD",
56+
description: "Specifies the maximum cost of the Actor run. This parameter is useful for pay-per-event Actors, as it allows you to limit the amount charged to your subscription. You can access the maximum cost in your Actor by using the ACTOR_MAX_TOTAL_CHARGE_USD environment variable.",
57+
optional: true,
58+
},
59+
webhooks: {
60+
type: "string",
61+
label: "Webhooks",
62+
description: "Specifies optional webhooks associated with the Actor run, which can be used to receive a notification e.g. when the Actor finished or failed. The value is a Base64-encoded JSON array of objects defining the webhooks. For more information, see [Webhooks documentation](https://docs.apify.com/platform/integrations/webhooks).",
63+
optional: true,
64+
},
1965
},
2066
methods: {
2167
getType(type) {
@@ -28,16 +74,14 @@ export default {
2874
? type
2975
: "string[]";
3076
},
31-
async getSchema() {
32-
const { data: { items: builds } } = await this.apify.listBuilds(this.actorId);
33-
const buildId = builds.at(-1).id;
77+
async getSchema(buildId) {
3478
const { data: { inputSchema } } = await this.apify.getBuild(buildId);
3579
return JSON.parse(inputSchema);
3680
},
3781
async prepareData(data) {
3882
const newData = {};
3983

40-
const { properties } = await this.getSchema();
84+
const { properties } = await this.getSchema(this.buildId);
4185
for (const [
4286
key,
4387
value,
@@ -85,31 +129,54 @@ export default {
85129
},
86130
async additionalProps() {
87131
const props = {};
88-
if (this.actorId) {
89-
const {
90-
properties, required: requiredProps = [],
91-
} = await this.getSchema();
132+
if (this.buildId) {
133+
try {
134+
const {
135+
properties, required: requiredProps = [],
136+
} = await this.getSchema(this.buildId);
92137

93-
for (const [
94-
key,
95-
value,
96-
] of Object.entries(properties)) {
97-
if (value.editor === "hidden") continue;
138+
for (const [
139+
key,
140+
value,
141+
] of Object.entries(properties)) {
142+
if (value.editor === "hidden") continue;
98143

99-
props[key] = {
100-
type: this.getType(value.type),
101-
label: value.title,
102-
description: value.description,
103-
optional: !requiredProps.includes(key),
104-
};
105-
const options = this.prepareOptions(value);
106-
if (options) props[key].options = options;
107-
if (value.default) {
108-
props[key].description += ` Default: \`${JSON.stringify(value.default)}\``;
109-
if (props[key].type !== "object") { // default values don't work properly for object props
110-
props[key].default = value.default;
144+
props[key] = {
145+
type: this.getType(value.type),
146+
label: value.title,
147+
description: value.description,
148+
optional: !requiredProps.includes(key),
149+
};
150+
const options = this.prepareOptions(value);
151+
if (options) props[key].options = options;
152+
if (value.default) {
153+
props[key].description += ` Default: \`${JSON.stringify(value.default)}\``;
154+
if (props[key].type !== "object") { // default values don't work properly for object props
155+
props[key].default = value.default;
156+
}
111157
}
112158
}
159+
} catch {
160+
props.properties = {
161+
type: "object",
162+
label: "Properties",
163+
description: "Properties to set for this actor",
164+
};
165+
}
166+
if (this.runAsynchronously) {
167+
props.outputRecordKey = {
168+
type: "string",
169+
label: "Output Record Key",
170+
description: "Key of the record from run's default key-value store to be returned in the response. By default, it is OUTPUT.",
171+
optional: true,
172+
};
173+
} else {
174+
props.waitForFinish = {
175+
type: "string",
176+
label: "Wait For Finish",
177+
description: "The maximum number of seconds the server waits for the run to finish. By default, it is 0, the maximum value is 60. If the build finishes in time then the returned run object will have a terminal status (e.g. SUCCEEDED), otherwise it will have a transitional status (e.g. RUNNING).",
178+
optional: true,
179+
};
113180
}
114181
}
115182
return props;
@@ -123,12 +190,37 @@ export default {
123190
prepareData,
124191
apify,
125192
actorId,
193+
buildId,
194+
properties,
195+
runAsynchronously,
196+
outputRecordKey,
197+
timeout,
198+
memory,
199+
maxItems,
200+
maxTotalChargeUsd,
201+
waitForFinish,
202+
webhooks,
126203
...data
127204
} = this;
128205

129-
const response = await apify.runActor({
206+
const fn = runAsynchronously
207+
? apify.runActorAsynchronously
208+
: apify.runActor;
209+
210+
const response = await fn({
130211
actorId,
131-
data: await prepareData(data),
212+
data: properties
213+
? parseObject(properties)
214+
: await prepareData(data),
215+
params: {
216+
outputRecordKey,
217+
timeout,
218+
memory,
219+
maxItems,
220+
maxTotalChargeUsd,
221+
waitForFinish,
222+
webhooks,
223+
},
132224
});
133225
$.export("$summary", `Successfully started actor run with ID: ${response.data.id}`);
134226
return response;

components/apify/apify.app.mjs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929
actorId: {
3030
type: "string",
3131
label: "Actor ID",
32-
description: "The ID of the actor to run.",
32+
description: "Actor ID or a tilde-separated owner's username and Actor name",
3333
async options({ page }) {
3434
const { data: { items } } = await this.listActors({
3535
params: {
@@ -105,6 +105,23 @@ export default {
105105
})) || [];
106106
},
107107
},
108+
buildId: {
109+
type: "string",
110+
label: "Build",
111+
description: "Specifies the Actor build to run. It can be either a build tag or build number.",
112+
async options({
113+
page, actorId,
114+
}) {
115+
const { data: { items } } = await this.listBuilds({
116+
actorId,
117+
params: {
118+
offset: LIMIT * page,
119+
limit: LIMIT,
120+
},
121+
});
122+
return items?.map(({ id }) => id) || [];
123+
},
124+
},
108125
clean: {
109126
type: "boolean",
110127
label: "Clean",
@@ -178,6 +195,15 @@ export default {
178195
...opts,
179196
});
180197
},
198+
runActorAsynchronously({
199+
actorId, ...opts
200+
}) {
201+
return this._makeRequest({
202+
method: "POST",
203+
path: `/acts/${actorId}/runs`,
204+
...opts,
205+
});
206+
},
181207
runTask({ taskId }) {
182208
return this._makeRequest({
183209
method: "POST",
@@ -207,7 +233,7 @@ export default {
207233
...opts,
208234
});
209235
},
210-
listBuilds(actorId) {
236+
listBuilds({ actorId }) {
211237
return this._makeRequest({
212238
path: `/acts/${actorId}/builds`,
213239
});

0 commit comments

Comments
 (0)