Can I use this?

This feature is available since Webiny v5.39.0.

What you’ll learn
  • what are available built-in background tasks
  • how to run built-in background tasks

Built-in Background Tasks
anchor

The following is a list of background tasks that are included in Webiny by default.

Amazon OpenSearch - Reindexing Background Task
anchor

This task can be used with the Amazon DynamoDB + Amazon OpenSearch database setup. It can be also used with the legacy Amazon DynamoDB + Amazon Elasticsearch setup.

The Reindexing Task is a background task that scans through the DynamoDB Elasticsearch table and pushes the data into the specific Elasticsearch/OpenSearch index. Internally it is a bit more than that, but this is the basic idea. You can check the definitionexternal link and the runnerexternal link code for more information.

The task can be triggered either via code or via the GraphQL API.

Triggering the Task via Code

Let’s say that you have a Page Builder lifecycle event hook which triggers the task when the page is published, but only on index which contains the word page in its name.

const myHook = () => {
  return new ContextPlugin(async context => {
    context.pageBuilder.onPageAfterPublish(async () => {
      await context.tasks.trigger({
        definition: "elasticsearchReindexing",
        input: {
          matching: "page"
        }
      });
    });
  });
};
Triggering the Task via GraphQL API

The task can be triggered via the GraphQL API, with the following mutation:

mutation TriggerTask {
  backgroundTasks {
    triggerTask(definition: elasticsearchReindexing, input: { matching: "page" }) {
      data {
        id
      }
      error {
        message
        code
        data
        stack
      }
    }
  }
}

The task disables indexing on all matching indexes, and enables it when the task is finished. This is done to reduce the strain on the Elasticsearch/OpenSearch cluster as the reindexing operation is a heavy one.

Amazon OpenSearch - Create Index Background Task
anchor

This task can be used with the Amazon DynamoDB + Amazon OpenSearch database setup. It can be also used with the legacy Amazon DynamoDB + Amazon Elasticsearch setup.

The Create Index background task is a task which creates all missing indexes in the Amazon OpenSearch cluster.

The task goes through all the CMS Models and creates indexes which do not exist. The task also creates Form Builder and Page Builder indexes.

The task can be triggered either via code or via the GraphQL API.

Triggering the Task via GraphQL API
mutation TriggerCreateMissingIndexesTask {
  backgroundTasks {
    triggerTask(
      definition: elasticsearchCreateIndexes
      input: {
        matching: "string matching your index, or nothing" # optional
      }
    ) {
      data {
        id
      }
      error {
        message
        code
        data
        stack
      }
    }
  }
}

Then you can check the task status and output with the following query:

query GetCreateIndexTask {
  backgroundTasks {
    getTask(id: "id you got from task trigger") {
      data {
        id
        name
        taskStatus
        output
      }
      error {
        message
        code
        data
        stack
      }
    }
  }
}