Class: Executor

olympe.async. Executor

Executor is an abstract class for implementing an arbitrary number of asynchronous tasks while keeping track of their progress. A task is a function taking three parameters:

  • done: a function to call when the task has completed. This is the expected outcome.
  • end: a function to call when the task failed and the executor should abort.
  • env: a Map that is passed between tasks and can be used to carry arbitrary values. This map will always contain an 'Executor' key whose value will be the Executor itself. done() and end() should always be called right before returning from the tasks function.

Example

executor.add(
    (done, fail, env) => {
        setTimeout( () => {
            env.set('val', 10);
            done();
        }, 5000);
    });
executor.onEnd(success, msg, env) => {
    console.log(`Executor ended: ${env.get('val')}`);
});
executor.start();

<abstract> new Executor( [context] [, autoDestroy])

Creates an Executor and binds it to an olympe.df.ExecutionContext. Defaults to the current context.

Parameters:
Name Type Argument Description
context olympe.df.ExecutionContext <optional>

The ExecutionContext to use for running the callbacks. Default is a new one based on the current context.

autoDestroy boolean <optional>

true to automatically destroy the ExecutionContext on completion. Default is true if no context is provided, false otherwise.

Members


<protected, non-null> callbacks :Array.<function(boolean, ?string, Map.<string, *>)>

Type:
  • Array.<function(boolean, ?string, Map.<string, *>)>

<protected> ended :boolean

Type:
  • boolean

<protected, non-null> env :Map.<string, *>

Type:
  • Map.<string, *>

<protected, nullable> errorMsg :string

Type:
  • string

<protected> logger :olympe.logging.Channel

Type:
  • olympe.logging.Channel

<protected> started :boolean

Type:
  • boolean

<protected, non-null> tasks :Array.<olympe.async.Task>

Type:

Methods


<static> runWithTimeout(task, timeout)

Executes a single asynchronous task with a given timeout. If the tasks doesn't complete within the specified time frame, the associated Executor will be completed as failed. An ExecutionContext will be automatically created at the start and destroyed on completion.

Parameters:
Name Type Description
task olympe.async.Task

The task to execute.

timeout number

The timeout in milliseconds.

Returns:

The created Executor.

Type
olympe.async.Executor

add(task)

Adds a task to this Executor. If the executor has already ended, this is ignored.

Parameters:
Name Type Description
task olympe.async.Task

The task to add.

Returns:

this Executor.

Type
olympe.async.Executor

<protected> clear()

Clears the executor: remove all tasks and callbacks.


<protected> complete(success)

Notifies the end of the execution and triggers the callbacks.

Parameters:
Name Type Description
success boolean

Whether the execution was successful or not.


getContext()

Gets the ExecutionContext associated with this Executor.

Returns:
Type
olympe.df.ExecutionContext

getLogger()

Gets the logger attached to this Executor.

Returns:

The attached logger.

Type
olympe.logging.Channel

hasEnded()

Checks whether this Executor has completed its execution.

Returns:

true when the Executor has ended.

Type
boolean

isRunning()

Checks whether this Executor is currently running one or more tasks.

Returns:

true when the Executor is running.

Type
boolean

onEnd(callback)

Registers a callback to be called when the Executor has completed. The callback is passed a boolean, true if all the tasks completed successfully, false otherwise.

Parameters:
Name Type Description
callback function

The function to register.

Returns:

this Executor.

Type
olympe.async.Executor

start()

Starts the executor. Does nothing if it had already been started.


withLogger(logger)

Attach a logger to this executor.

Parameters:
Name Type Description
logger olympe.logging.Channel

The logger to attach.

Returns:

this Executor.

Type
olympe.async.Executor

withTimeout(t)

Sets a timeout for the executor. If all the tasks in this Executor are not completed within the specified time the Executor will complete as failed. All pending tasks will be cancelled.

Parameters:
Name Type Description
t number

timeout in milliseconds.

Returns:

this Executor.

Type
olympe.async.Executor