React Reference

useExecutor

useExecutor is a hook that manages a single TaskExecutor. Use this hook to initialize and later terminate an executor. To run tasks you should use the useTask hook. It returns an object with the following properties:

NameDescription
executorThe TaskExecutor instance.
isInitializingBoolean indicating whether the executor is being initialized. A new executor cannot be initialized while this is true.
errorError object containing information about the error that occurred while initializing the executor.
initializeA function to initialize a new executor
terminateA function to terminate the executor (calls TaskExecutor.end() under the hood)
isInitializedBoolean indicating whether the executor is initialized. This is always equal to !!executor.
isTerminatingBoolean indicating whether the executor is being terminated.

Parameters

The hook accepts two configuration objects, one for the executor itself (package, demand specification, budget, etc) and one for the extra behavior (e.g. whether to add a beforeunload event listener to prevent the user from accidentally closing the tab while the executor is running).

NameDescription
options (required)TaskExecutorOptions
extraOptions (optional)See below

Extra options

NameDescription
addBeforeUnloadHandler (optional)Boolean indicating whether to add a beforeunload event listener to prevent the user from closing the tab while the executor is running. Defaults to true.

Example

function MyComponent() {
  const {
    executor,
    initialize,
    isInitialized,
    isInitializing,
    terminate,
    error,
  } = useExecutor(
    {
      demand: {
        workload: { imageTag: 'golem/alpine:latest' },
      },
      market: {
        rentHours: 0.5,
        pricing: {
          model: 'linear',
          maxStartPrice: 0.5,
          maxCpuPerHourPrice: 1.0,
          maxEnvPerHourPrice: 0.5,
        },
      },
    },
    {
      addBeforeUnloadHandler: true,
    }
  )
  if (isInitializing) {
    return <div>Initializing executor...</div>
  }
  if (error) {
    return <div>Error: {error.toString()}</div>
  }
  if (!isInitialized) {
    return (
      <div>
        <button onClick={initialize}>Initialize executor</button>
      </div>
    )
  }
  return (
    <div>
      <MyTaskComponent executor={executor} />
      <button onClick={terminate}>Terminate executor</button>
    </div>
  )
}

Was this helpful?