-
Notifications
You must be signed in to change notification settings - Fork 44
feat: forward user signals to builds/runs #1088
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
vladfrangu
wants to merge
5
commits into
master
Choose a base branch
from
feat/forward-signals
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.
+398
−21
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe46397
feat: forward user signals to builds/runs
vladfrangu 5738d9c
feat: abort runs with ctrl+c, gracefully then forcefully
vladfrangu 5b3e6c4
chore: update error message
vladfrangu 5c79520
fmt: ty copilot
vladfrangu 4933c72
refactor: extract useAbortJobOnSignal hook
vladfrangu 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
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,130 @@ | ||
| import type { ApifyClient } from 'apify-client'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| import { error, info } from '../outputs.js'; | ||
| import { useSignalHandler } from './useSignalHandler.js'; | ||
|
|
||
| export type UseAbortJobOnSignalInput = { | ||
| /** Logged-in client used to issue the abort request. */ | ||
| apifyClient: ApifyClient; | ||
| /** Suppress status output. The listener still fires — it just stays silent. Defaults to `false`. */ | ||
| silent?: boolean; | ||
| } & ( | ||
| | { | ||
| /** Abort an Actor build. Builds have no graceful/force distinction. */ | ||
| kind: 'build'; | ||
| /** ID of the build to abort. */ | ||
| jobId: string; | ||
| } | ||
| | { | ||
| /** Abort an Actor or Task run. Runs escalate: graceful on the first signal, forced on the second. */ | ||
| kind: 'run'; | ||
| /** ID of the run to abort. */ | ||
| jobId: string; | ||
| /** Used purely for the user-visible status line (e.g. "aborting actor run ..."). */ | ||
| runType: 'Actor' | 'Task'; | ||
| } | ||
| ); | ||
|
|
||
| /** | ||
| * Registers a signal handler that aborts the given build or run on the Apify | ||
| * platform, and returns a `Disposable` that removes it. Pair with the `using` | ||
| * keyword so the listener is always cleaned up when the enclosing block | ||
| * exits. | ||
| * | ||
| * Repeat signals never terminate the CLI while an abort is in flight — the | ||
| * listener stays registered for the lifetime of the `using` binding: | ||
| * | ||
| * - For `kind: 'build'`, the first signal issues the abort and subsequent | ||
| * signals are silent no-ops. The build-abort API has no "gracefully" knob. | ||
| * - For `kind: 'run'`, the first signal issues `abort({ gracefully: true })` | ||
| * with a hint that pressing Ctrl+C again forces an immediate abort. The | ||
| * second signal issues `abort({ gracefully: false })`. Third and later | ||
| * signals are silent no-ops. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * { | ||
| * using _signalHandler = useAbortJobOnSignal({ | ||
| * apifyClient: client, | ||
| * kind: 'build', | ||
| * jobId: build.id, | ||
| * }); | ||
| * | ||
| * await outputJobLog({ job: build, apifyClient: client }); | ||
| * } // listener is removed here | ||
| * ``` | ||
| */ | ||
| export function useAbortJobOnSignal(input: UseAbortJobOnSignalInput): Disposable { | ||
| const { apifyClient, silent = false } = input; | ||
|
|
||
| let abortAttempt = 0; | ||
|
|
||
| return useSignalHandler({ | ||
| signals: ['SIGINT', 'SIGTERM', 'SIGHUP'], | ||
| once: false, | ||
| handler: async (signal) => { | ||
| abortAttempt += 1; | ||
|
|
||
| if (input.kind === 'build') { | ||
| if (abortAttempt > 1) { | ||
| return; | ||
| } | ||
|
|
||
| if (!silent) { | ||
| info({ | ||
| message: chalk.gray( | ||
| `Received ${chalk.yellow(signal)}, aborting build "${chalk.yellow(input.jobId)}" on the Apify platform...`, | ||
| ), | ||
| stdout: true, | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| await apifyClient.build(input.jobId).abort(); | ||
| } catch (abortErr) { | ||
| error({ | ||
| message: `Failed to abort build "${input.jobId}": ${(abortErr as Error).message}`, | ||
| stdout: true, | ||
| }); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (abortAttempt > 2) { | ||
| return; | ||
| } | ||
|
|
||
| const gracefully = abortAttempt === 1; | ||
| const runLabel = `${input.runType.toLowerCase()} run`; | ||
|
|
||
| if (!silent) { | ||
| if (gracefully) { | ||
| info({ | ||
| message: chalk.gray( | ||
| `Received ${chalk.yellow(signal)}, gracefully aborting ${runLabel} "${chalk.yellow(input.jobId)}" on the Apify platform... ${chalk.dim('(press Ctrl+C again to abort immediately)')}`, | ||
| ), | ||
| stdout: true, | ||
| }); | ||
| } else { | ||
| info({ | ||
| message: chalk.gray( | ||
| `Received ${chalk.yellow(signal)} again, aborting ${runLabel} "${chalk.yellow(input.jobId)}" immediately...`, | ||
| ), | ||
| stdout: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| await apifyClient.run(input.jobId).abort({ gracefully }); | ||
| } catch (abortErr) { | ||
| error({ | ||
| message: `Failed to abort run "${input.jobId}": ${(abortErr as Error).message}`, | ||
| stdout: true, | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
| } |
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
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.
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.
isnt this supported only on node 24+?
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.
It gets downcompiled by tsup/down 👀
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.
Ignore the terminal breaking (volta doesn't forward signals LOL)