@@ -227,18 +227,32 @@ export interface ApifyEnv {
227227
228228export type UserFunc < T = unknown > = ( ) => Awaitable < T > ;
229229
230- export interface CallOptions extends ActorCallOptions {
230+ export interface CallOptions extends Omit < ActorCallOptions , 'timeout' > {
231231 /**
232232 * User API token that is used to run the Actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
233233 */
234234 token ?: string ;
235+ /**
236+ * Timeout for the Actor run in seconds, or `'inherit'`.
237+ *
238+ * Using `inherit` will set timeout of the newly started Actor run to the time
239+ * remaining until this Actor run times out so that the new run does not outlive this one.
240+ */
241+ timeout ?: number | 'inherit' ;
235242}
236243
237- export interface CallTaskOptions extends TaskCallOptions {
244+ export interface CallTaskOptions extends Omit < TaskCallOptions , 'timeout' > {
238245 /**
239246 * User API token that is used to run the Actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
240247 */
241248 token ?: string ;
249+ /**
250+ * Timeout for the Actor task in seconds, or `'inherit'`.
251+ *
252+ * Using `inherit` will set timeout of the newly started Actor task to the time
253+ * remaining until this Actor run times out so that the new run does not outlive this one.
254+ */
255+ timeout ?: number | 'inherit' ;
242256}
243257
244258export interface AbortOptions extends RunAbortOptions {
@@ -668,10 +682,13 @@ export class Actor<Data extends Dictionary = Dictionary> {
668682 input ?: unknown ,
669683 options : CallOptions = { } ,
670684 ) : Promise < ClientActorRun > {
685+ const timeout =
686+ options . timeout === 'inherit'
687+ ? this . getRemainingTime ( )
688+ : options . timeout ;
671689 const { token, ...rest } = options ;
672690 const client = token ? this . newClient ( { token } ) : this . apifyClient ;
673-
674- return client . actor ( actorId ) . call ( input , rest ) ;
691+ return client . actor ( actorId ) . call ( input , { ...rest , timeout } ) ;
675692 }
676693
677694 /**
@@ -703,10 +720,14 @@ export class Actor<Data extends Dictionary = Dictionary> {
703720 input ?: unknown ,
704721 options : CallOptions = { } ,
705722 ) : Promise < ClientActorRun > {
723+ const timeout =
724+ options . timeout === 'inherit'
725+ ? this . getRemainingTime ( )
726+ : options . timeout ;
706727 const { token, ...rest } = options ;
707728 const client = token ? this . newClient ( { token } ) : this . apifyClient ;
708729
709- return client . actor ( actorId ) . start ( input , rest ) ;
730+ return client . actor ( actorId ) . start ( input , { ... rest , timeout } ) ;
710731 }
711732
712733 /**
@@ -771,10 +792,14 @@ export class Actor<Data extends Dictionary = Dictionary> {
771792 input ?: Dictionary ,
772793 options : CallTaskOptions = { } ,
773794 ) : Promise < ClientActorRun > {
795+ const timeout =
796+ options . timeout === 'inherit'
797+ ? this . getRemainingTime ( )
798+ : options . timeout ;
774799 const { token, ...rest } = options ;
775800 const client = token ? this . newClient ( { token } ) : this . apifyClient ;
776801
777- return client . task ( taskId ) . call ( input , rest ) ;
802+ return client . task ( taskId ) . call ( input , { ... rest , timeout } ) ;
778803 }
779804
780805 /**
@@ -2325,6 +2350,22 @@ export class Actor<Data extends Dictionary = Dictionary> {
23252350 ) ;
23262351 }
23272352
2353+ /**
2354+ * Get time remaining from the Actor run timeout. Returns `undefined` if not on an Apify platform or the current
2355+ * run was started without a timeout.
2356+ */
2357+ private getRemainingTime ( ) : number | undefined {
2358+ const env = this . getEnv ( ) ;
2359+ if ( this . isAtHome ( ) && env . timeoutAt !== null ) {
2360+ return env . timeoutAt . getTime ( ) - Date . now ( ) ;
2361+ }
2362+ log . warning (
2363+ 'Using `inherit` argument is only possible when the Actor is running on the Apify platform and when the ' +
2364+ 'timeout for the Actor run is set.' ,
2365+ ) ;
2366+ return undefined ;
2367+ }
2368+
23282369 private async inferDefaultsFromInputSchema < T extends Dictionary > (
23292370 input : T ,
23302371 ) : Promise < T > {
0 commit comments