Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,10 @@ ARGUMENTS
FLAGS
--json Format the command output as
JSON.
--poll-interval=<value> How often to poll the
platform, in seconds. Defaults to 2.
-t, --timeout=<value> Maximum seconds to wait
before giving up. Without this flag the command waits
--poll-interval=<value> In seconds, how often to
poll the platform. Defaults to 2.
-t, --timeout=<value> In seconds, how long to
wait before giving up. If skipped, it waits
indefinitely.
```
<!-- actor-build-commands-end -->
Expand Down
102 changes: 15 additions & 87 deletions src/commands/actors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,17 @@ import {
} from 'apify-client';
import chalk from 'chalk';

import { ACTOR_JOB_STATUSES } from '@apify/consts';

import { ApifyCommand, StdinMode } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { Flags } from '../../lib/command-framework/flags.js';
import {
consoleDatasetUrl,
consoleRunUrl,
exitCodeForJobStatus,
fetchLogTail,
formatResultSummary,
} from '../../lib/commands/agent-output.js';
import { getInputOverride } from '../../lib/commands/resolve-input.js';
import { runActorOrTaskOnCloud, SharedRunOnCloudFlags } from '../../lib/commands/run-on-cloud.js';
import { finalizeRun, runUrl } from '../../lib/commands/run-result.js';
import { CommandExitCodes, LOCAL_CONFIG_PATH } from '../../lib/consts.js';
import { error, simpleLog } from '../../lib/outputs.js';
import {
getLocalConfig,
getLocalUserInfo,
getLoggedClientOrThrow,
printJsonToStdout,
TimestampFormatter,
} from '../../lib/utils.js';
import { getLocalConfig, getLocalUserInfo, getLoggedClientOrThrow, TimestampFormatter } from '../../lib/utils.js';

export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
static override name = 'call' as const;
Expand Down Expand Up @@ -151,7 +141,7 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
}

let runStarted = false;
let run: ActorRun | undefined;
let run: ActorRun;

const iterator = runActorOrTaskOnCloud(apifyClient, {
actorOrTaskData: {
Expand All @@ -175,7 +165,7 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {

// A *lot* is copied from `runs info`
if (!this.flags.silent) {
const startedUrl = `https://console.apify.com/actors/${actorId}/runs/${yieldedRun.id}`;
const url = runUrl(actorId, yieldedRun.id);

const message: string[] = [`${chalk.yellow('Started')}: ${TimestampFormatter.display(yieldedRun.startedAt)}`];

Expand Down Expand Up @@ -217,88 +207,26 @@ export class ActorsCallCommand extends ApifyCommand<typeof ActorsCallCommand> {
message.push(`${chalk.yellow('Memory')}: ${run.options.memoryMbytes} MB`);

// url
message.push(`${chalk.blue('View on Apify Console')}: ${startedUrl}`, '');
message.push(`${chalk.blue('View on Apify Console')}: ${url}`, '');

simpleLog({ message: message.join('\n'), stdout: !this.flags.json });
}
}
}

if (!run) {
error({ message: 'Actor run did not start.' });
process.exitCode = CommandExitCodes.RunFailed;
return;
}
const finalRun = run;
const finalUrl = consoleRunUrl(actorId, finalRun.id);
const finalDatasetUrl = consoleDatasetUrl(finalRun.defaultDatasetId);
const ok = finalRun.status === 'SUCCEEDED';
const exitCode = exitCodeForJobStatus(finalRun.status, 'run');
const logTail = ok ? [] : await fetchLogTail(apifyClient, finalRun.id);
await finalizeRun({
apifyClient,
run: run!,
operation: 'call',
json: this.flags.json,
silent: this.flags.silent,
});

if (this.flags.json) {
printJsonToStdout({
ok,
operation: 'call',
actor: {
id: actorId,
url: `https://console.apify.com/actors/${actorId}`,
},
run: {
id: finalRun.id,
status: finalRun.status,
exitCode: finalRun.exitCode ?? null,
url: finalUrl,
},
storage: {
defaultDatasetId: finalRun.defaultDatasetId,
defaultKeyValueStoreId: finalRun.defaultKeyValueStoreId,
datasetUrl: finalDatasetUrl,
},
...(ok
? {}
: {
error: {
phase: 'run',
message: 'Actor run did not succeed',
logTail,
},
}),
exitCode,
});
process.exitCode = exitCode;
return;
}

if (!this.flags.silent) {
simpleLog({
message: formatResultSummary({
resultLabel: 'Apify call result',
overallStatus: finalRun.status as never,
lines: [
{ label: 'Run', value: finalRun.status as string },
{ label: 'Actor ID', value: actorId },
{ label: 'Run ID', value: finalRun.id },
{ label: 'Build number', value: finalRun.buildNumber },
...(typeof finalRun.exitCode === 'number'
? [{ label: 'Exit code', value: String(finalRun.exitCode) }]
: []),
{ label: 'Dataset ID', value: finalRun.defaultDatasetId },
{ label: 'Key-value store ID', value: finalRun.defaultKeyValueStoreId },
],
links: [
{ label: 'Run URL', url: finalUrl },
{ label: 'Dataset URL', url: finalDatasetUrl },
],
errorReason: ok ? undefined : logTail,
}),
stdout: true,
});
}

process.exitCode = exitCode;

if (this.flags.outputDataset) {
if (this.flags.outputDataset && run!.status === ACTOR_JOB_STATUSES.SUCCEEDED) {
const datasetId = run!.defaultDatasetId;

let info: Dataset;
Expand Down
94 changes: 5 additions & 89 deletions src/commands/task/run.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import process from 'node:process';

import type { ActorRun, ApifyClient, TaskStartOptions } from 'apify-client';

import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import {
consoleDatasetUrl,
consoleRunUrl,
exitCodeForJobStatus,
fetchLogTail,
formatResultSummary,
} from '../../lib/commands/agent-output.js';
import { runActorOrTaskOnCloud, SharedRunOnCloudFlags } from '../../lib/commands/run-on-cloud.js';
import { simpleLog } from '../../lib/outputs.js';
import { getLocalUserInfo, getLoggedClientOrThrow, printJsonToStdout } from '../../lib/utils.js';
import { finalizeRun } from '../../lib/commands/run-result.js';
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';

export class TaskRunCommand extends ApifyCommand<typeof TaskRunCommand> {
static override name = 'run' as const;
Expand Down Expand Up @@ -69,7 +60,7 @@ export class TaskRunCommand extends ApifyCommand<typeof TaskRunCommand> {
runOpts.memory = this.flags.memory;
}

let run: ActorRun | undefined;
let run!: ActorRun;

const iterator = runActorOrTaskOnCloud(apifyClient, {
actorOrTaskData: {
Expand All @@ -79,91 +70,16 @@ export class TaskRunCommand extends ApifyCommand<typeof TaskRunCommand> {
},
runOptions: runOpts,
type: 'Task',
printRunLogs: true,
waitForRunToFinish: true,
silent: this.flags.json,
printRunLogs: true,
suppressFinalStatus: true,
});

for await (const yieldedRun of iterator) {
run = yieldedRun;
}

if (!run) {
simpleLog({ message: 'Task run did not start.', stdout: false });
process.exitCode = 1;
return;
}
const finalRun = run;
const finalUrl = consoleRunUrl(finalRun.actId, finalRun.id);
const finalDatasetUrl = consoleDatasetUrl(finalRun.defaultDatasetId);
const ok = finalRun.status === 'SUCCEEDED';
const exitCode = exitCodeForJobStatus(finalRun.status, 'run');
const logTail = ok ? [] : await fetchLogTail(apifyClient, finalRun.id);

if (this.flags.json) {
printJsonToStdout({
ok,
operation: 'task.run',
task: {
id: taskId,
name: userFriendlyId,
title,
},
actor: {
id: finalRun.actId,
url: `https://console.apify.com/actors/${finalRun.actId}`,
},
run: {
id: finalRun.id,
status: finalRun.status,
exitCode: finalRun.exitCode ?? null,
url: finalUrl,
},
storage: {
defaultDatasetId: finalRun.defaultDatasetId,
defaultKeyValueStoreId: finalRun.defaultKeyValueStoreId,
datasetUrl: finalDatasetUrl,
},
...(ok
? {}
: {
error: {
phase: 'run',
message: 'Task run did not succeed',
logTail,
},
}),
exitCode,
});
process.exitCode = exitCode;
return;
}

simpleLog({
message: formatResultSummary({
resultLabel: 'Apify task run result',
overallStatus: finalRun.status as never,
lines: [
{ label: 'Run', value: finalRun.status as string },
{ label: 'Task ID', value: taskId },
{ label: 'Actor ID', value: finalRun.actId },
{ label: 'Run ID', value: finalRun.id },
{ label: 'Build number', value: finalRun.buildNumber },
...(typeof finalRun.exitCode === 'number' ? [{ label: 'Exit code', value: String(finalRun.exitCode) }] : []),
{ label: 'Dataset ID', value: finalRun.defaultDatasetId },
{ label: 'Key-value store ID', value: finalRun.defaultKeyValueStoreId },
],
links: [
{ label: 'Run URL', url: finalUrl },
{ label: 'Dataset URL', url: finalDatasetUrl },
],
errorReason: ok ? undefined : logTail,
}),
stdout: true,
});

process.exitCode = exitCode;
await finalizeRun({ apifyClient, run, operation: 'task-run', json: this.flags.json });
}

private async resolveTaskId(client: ApifyClient, usernameOrId: string) {
Expand Down
Loading
Loading