|
| 1 | +import { AppContext } from "../mod.ts"; |
| 2 | +import { ActorRun } from "../utils/types.ts"; |
| 3 | + |
| 4 | +export interface Props { |
| 5 | + /** |
| 6 | + * @title Actor ID |
| 7 | + * @description The ID of the actor to run |
| 8 | + */ |
| 9 | + actorId: string; |
| 10 | + |
| 11 | + /** |
| 12 | + * @title Input |
| 13 | + * @description Input data for the actor run (Stringified JSON object). If you don't know what object to pass, use an empty object: {} |
| 14 | + */ |
| 15 | + input: string; |
| 16 | + |
| 17 | + /** |
| 18 | + * @title Timeout (seconds) |
| 19 | + * @description Maximum timeout for the run in seconds |
| 20 | + */ |
| 21 | + timeout?: number; |
| 22 | + |
| 23 | + /** |
| 24 | + * @title Memory (MB) |
| 25 | + * @description Amount of memory allocated for the run in megabytes |
| 26 | + */ |
| 27 | + memory?: number; |
| 28 | + |
| 29 | + /** |
| 30 | + * @title Build |
| 31 | + * @description Specific build version to use (optional) |
| 32 | + */ |
| 33 | + build?: string; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @name RUN_ACTOR_ASYNC |
| 38 | + * @title Run Actor Async |
| 39 | + * @description Run an Apify actor asynchronously and return immediately without waiting for completion |
| 40 | + */ |
| 41 | +export default async function runActorAsync( |
| 42 | + props: Props, |
| 43 | + _req: Request, |
| 44 | + ctx: AppContext, |
| 45 | +): Promise< |
| 46 | + { data: ActorRun; error: null } | { |
| 47 | + error: string; |
| 48 | + data: null; |
| 49 | + } |
| 50 | +> { |
| 51 | + try { |
| 52 | + const { actorId, timeout, memory, build } = props; |
| 53 | + |
| 54 | + if (!actorId) { |
| 55 | + return { error: "Actor ID is required", data: null }; |
| 56 | + } |
| 57 | + |
| 58 | + // Build query parameters |
| 59 | + const searchParams = new URLSearchParams(); |
| 60 | + if (timeout !== undefined) { |
| 61 | + searchParams.set("timeout", timeout.toString()); |
| 62 | + } |
| 63 | + if (memory !== undefined) { |
| 64 | + searchParams.set("memory", memory.toString()); |
| 65 | + } |
| 66 | + if (build !== undefined) { |
| 67 | + searchParams.set("build", build); |
| 68 | + } |
| 69 | + |
| 70 | + const response = await ctx.api["POST /v2/acts/:actorId/runs"]({ |
| 71 | + actorId, |
| 72 | + ...(searchParams.toString() ? { searchParams } : {}), |
| 73 | + }); |
| 74 | + |
| 75 | + if (!response.ok) { |
| 76 | + const errorText = await response.text(); |
| 77 | + throw new Error(`HTTP ${response.status}: ${errorText}`); |
| 78 | + } |
| 79 | + |
| 80 | + const result = await response.json(); |
| 81 | + return { data: result, error: null }; |
| 82 | + } catch (error) { |
| 83 | + console.error("Error starting actor run:", error); |
| 84 | + return { |
| 85 | + error: ctx.errorHandler.toHttpError(error, "Error starting actor run"), |
| 86 | + data: null, |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
0 commit comments