-
Notifications
You must be signed in to change notification settings - Fork 56
feat(task): add create, ls, info, update, and rm commands #1315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ayush7614
wants to merge
4
commits into
apify:master
Choose a base branch
from
Ayush7614:feat/task-management-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3536a63
feat(task): add create, ls, info, update, and rm commands
Ayush7614 53b641a
fix(task): address review on exit codes, input, and tests
Ayush7614 a575ecd
Merge branch 'master' into feat/task-management-commands
Ayush7614 bb680e4
Merge branch 'master' into feat/task-management-commands
Ayush7614 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import process from 'node:process'; | ||
|
|
||
| import type { ApifyApiError, Dictionary } from 'apify-client'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; | ||
| import { Args } from '../../lib/command-framework/args.js'; | ||
| import { Flags } from '../../lib/command-framework/flags.js'; | ||
| import { resolveActorContext } from '../../lib/commands/resolve-actor-context.js'; | ||
| import { getInputOverride } from '../../lib/commands/resolve-input.js'; | ||
| import { error, success } from '../../lib/outputs.js'; | ||
| import { getLoggedClientOrThrow, printJsonToStdout } from '../../lib/utils.js'; | ||
|
|
||
| export class TaskCreateCommand extends ApifyCommand<typeof TaskCreateCommand> { | ||
| static override name = 'create' as const; | ||
|
|
||
| static override description = | ||
| 'Creates a new Actor task with optional saved input and run options.\n' + | ||
| 'Provide input via --input (inline JSON) or --input-file.'; | ||
|
|
||
| static override examples = [ | ||
| { | ||
| description: 'Create a task for an Actor with a name.', | ||
| command: 'apify task create my-task --actor apify/hello-world', | ||
| }, | ||
| { | ||
| description: 'Create a task with saved JSON input and custom memory.', | ||
| command: | ||
| 'apify task create my-task --actor my-username/my-actor --input \'{"url":"https://example.com"}\' --memory 2048', | ||
| }, | ||
| { | ||
| description: 'Create a task using input from a file.', | ||
| command: 'apify task create my-task --actor my-actor --input-file ./input.json --title "Nightly scrape"', | ||
| }, | ||
| ]; | ||
|
|
||
| static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-create'; | ||
|
|
||
| static override args = { | ||
| taskName: Args.string({ | ||
| required: true, | ||
| description: 'Name for the new Task (unique under your account).', | ||
| }), | ||
| }; | ||
|
|
||
| static override flags = { | ||
| actor: Flags.string({ | ||
| description: 'Actor ID or name the task should run (e.g. "apify/hello-world" or "my-actor").', | ||
| required: true, | ||
| }), | ||
| title: Flags.string({ | ||
| description: 'Optional human-readable title for the task.', | ||
| }), | ||
| description: Flags.string({ | ||
| description: 'Optional description for the task.', | ||
| }), | ||
| input: Flags.string({ | ||
| char: 'i', | ||
| description: 'Saved task input as a JSON string.', | ||
| exclusive: ['input-file'], | ||
| }), | ||
| 'input-file': Flags.string({ | ||
| char: 'f', | ||
| description: 'Path to a JSON file with saved task input.', | ||
| exclusive: ['input'], | ||
| }), | ||
| memory: Flags.integer({ | ||
| description: 'Memory limit for the task run, in megabytes.', | ||
| }), | ||
| timeout: Flags.integer({ | ||
| description: 'Timeout for the task run, in seconds. Use 0 for no timeout.', | ||
| }), | ||
| build: Flags.string({ | ||
| description: 'Actor build tag or number to use for the task (e.g. "latest" or "1.2.3").', | ||
| }), | ||
| }; | ||
|
|
||
| static override enableJsonFlag = true; | ||
|
|
||
| async run() { | ||
| const { taskName } = this.args; | ||
| const { actor: actorIdOrName, title, description, input, inputFile, memory, timeout, build, json } = this.flags; | ||
|
|
||
| const client = await getLoggedClientOrThrow(); | ||
| const cwd = process.cwd(); | ||
|
|
||
| const actorCtx = await resolveActorContext({ providedActorNameOrId: actorIdOrName, client }); | ||
| if (!actorCtx.valid) { | ||
| error({ | ||
| message: `${actorCtx.reason}. Please specify a valid Actor ID or name.`, | ||
| stdout: true, | ||
| }); | ||
| process.exitCode ||= 1; | ||
| return; | ||
| } | ||
|
|
||
| const inputOverride = await getInputOverride(cwd, input, inputFile); | ||
| if (inputOverride === false) { | ||
| return; | ||
| } | ||
|
|
||
| const parsedInput = inputOverride?.input as Dictionary | undefined; | ||
|
|
||
| const options = | ||
| memory != null || timeout != null || build | ||
| ? { | ||
| ...(memory != null ? { memoryMbytes: memory } : {}), | ||
| ...(timeout != null ? { timeoutSecs: timeout } : {}), | ||
| ...(build ? { build } : {}), | ||
| } | ||
| : undefined; | ||
|
|
||
| try { | ||
| const newTask = await client.tasks().create({ | ||
| actId: actorCtx.id, | ||
| name: taskName, | ||
| ...(title ? { title } : {}), | ||
| ...(description ? { description } : {}), | ||
| ...(parsedInput !== undefined ? { input: parsedInput } : {}), | ||
| ...(options ? { options } : {}), | ||
| }); | ||
|
|
||
| if (json) { | ||
| printJsonToStdout(newTask); | ||
| return; | ||
| } | ||
|
|
||
| success({ | ||
| message: `Task with ID ${chalk.yellow(newTask.id)} (called ${chalk.yellow(newTask.name)}) was created for Actor ${chalk.yellow(actorCtx.userFriendlyId)}.`, | ||
| stdout: true, | ||
| }); | ||
| } catch (err) { | ||
| const casted = err as ApifyApiError; | ||
| error({ | ||
| message: `Failed to create Task "${taskName}".\n ${casted.message || casted}`, | ||
| stdout: true, | ||
| }); | ||
| process.exitCode ||= 1; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're here: siblings with these flags also declare the shorthands (
src/commands/actors/call.ts:63,71):charis the only thing that registers a short alias (flags.ts:99-101setsoption.short), andparseArgsruns instrict: truemode, so without it-iis an unknown-option error. Right nowdocs/reference.mddocuments-i, --inputforactors callbut bare--inputfortask create— same flag, different ergonomics depending on namespace. Both chars are unclaimed here.Same in
task/update.ts:58-65.Correction to my original version of this comment, which also asked for
stdin: StdinMode.Stringified: that's wrong, it's already the default.flags.ts:118isstdin: options.stdin ?? StdinMode.Stringifiedfor the string builder, and the gate atapify-command.ts:572is a truthiness check (rawFlag === '-' && builderData.stdin), so--input -already works as written. The explicit declarations inactors/call.tsare redundant. Only thecharask stands.