-
Notifications
You must be signed in to change notification settings - Fork 19
/
run.ts
96 lines (77 loc) · 2.71 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Args } from '@oclif/core';
import { ApifyClient, TaskStartOptions } from 'apify-client';
import { ApifyCommand } from '../../lib/apify_command.js';
import { SharedRunOnCloudFlags, runActorOrTaskOnCloud } from '../../lib/commands/run-on-cloud.js';
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';
export class TaskRunCommand extends ApifyCommand<typeof TaskRunCommand> {
static override description =
'Runs a specific Actor remotely on the Apify cloud platform.\n' +
'The Actor is run under your current Apify account. Therefore you need to be logged in by calling "apify login". ' +
'It takes input for the Actor from the default local key-value store by default.';
static override flags = SharedRunOnCloudFlags('Task');
static override args = {
taskId: Args.string({
required: true,
description: 'Name or ID of the Task to run (e.g. "my-task" or "E2jjCZBezvAZnX8Rb").',
}),
};
async run() {
const apifyClient = await getLoggedClientOrThrow();
const userInfo = await getLocalUserInfo();
const usernameOrId = userInfo.username || (userInfo.id as string);
const { id: taskId, userFriendlyId, title } = await this.resolveTaskId(apifyClient, usernameOrId);
const runOpts: TaskStartOptions = {
waitForFinish: 2, // NOTE: We need to wait some time to Apify open stream and we can create connection
};
const waitForFinishMillis = Number.isNaN(this.flags.waitForFinish)
? undefined
: Number.parseInt(this.flags.waitForFinish!, 10) * 1000;
if (this.flags.build) {
runOpts.build = this.flags.build;
}
if (this.flags.timeout) {
runOpts.timeout = this.flags.timeout;
}
if (this.flags.memory) {
runOpts.memory = this.flags.memory;
}
await runActorOrTaskOnCloud(apifyClient, {
actorOrTaskData: {
id: taskId,
userFriendlyId,
title,
},
runOptions: runOpts,
type: 'Task',
waitForFinishMillis,
});
}
private async resolveTaskId(client: ApifyClient, usernameOrId: string) {
const { taskId } = this.args;
// Full ID
if (taskId?.includes('/')) {
const task = await client.task(taskId).get();
if (!task) {
throw new Error(`Cannot find Task with ID '${taskId}' in your account.`);
}
return {
id: task.id,
userFriendlyId: `${usernameOrId}/${task.name}`,
title: task.title,
};
}
// Try fetching task directly by name
if (taskId) {
const task = await client.task(`${usernameOrId}/${taskId.toLowerCase()}`).get();
if (!task) {
throw new Error(`Cannot find Task with name '${taskId}' in your account.`);
}
return {
id: task.id,
userFriendlyId: `${usernameOrId}/${task.name}`,
title: task.title,
};
}
throw new Error('Please provide a valid Task ID or name.');
}
}