EXECUTE TASK

Manually triggers an asynchronous single run of a task (either a standalone task or the root task in a task graph) independent of the schedule defined for the task.

成功运行根任务会触发任务图中子任务的级联运行(基于前置任务的完成),就像根任务按照其定义的计划运行一样。

此外,您可以手动触发重新执行先前失败的任务。

See also:

CREATE TASK , DESCRIBE TASK , ALTER TASK , DROP TASK , SHOW TASKS

语法

EXECUTE TASK <name>
  [ USING CONFIG = <configuration_string> ]

EXECUTE TASK <name> RETRY LAST

参数

name

要运行的独立任务或根任务的标识符。如果标识符包含空格或特殊字符,则整个字符串必须放在双引号内。放在双引号内的标识符也区分大小写。

USING CONFIG = configuration_string

Specifies a configuration string in valid JSON format for this single execution. This parameter creates a new execution with the dynamically specified configuration, but doesn’t modify the task definition.

Snowflake merges the dynamic configuration with the default configuration, which is the CONFIG parameter that you set in the task definition with CREATE or ALTER. For matching fields, Snowflake uses the dynamically specified values. For non-matching fields, Snowflake uses the values from the default configuration. For an example, see Use a dynamic CONFIG.

The configuration string follows the same format as the CONFIG parameter in CREATE TASK or ALTER TASK:

CONFIG = $${"string1": value1 [, "string2": value2, ...] }$$

Example:

CONFIG = $${"learning_rate": 0.2, "environment": "testing"}$$
RETRY LAST

Re-execute the last failed task of the task graph with name restarting from where the tasks failed.

要重新执行任务,必须满足以下条件:

  • 最后运行的任务图必须处于 FAILED 或 CANCELED 状态。
  • 任务图自上次运行以来不得有过修改。
  • The last failed graph run’s first attempt must have been executed in the last 14 days.

To view task history, see either the TASK_HISTORY table function or the Tasks page on Snowsight.

Note

RETRY LAST 创建一个新的图形运行,该运行从上次失败的任务处开始执行。

具体而言,所有 FAILED 或 CANCELED 任务运行都会立即重新执行,并且如果所有前置任务都成功执行,则会计划关联的子任务。

此外,重试后生成的新任务图运行的 ATTEMPT NUMBER 将比先前失败的任务图多运行多一次,并且与重试后或原始任务图运行具有相同的 GRAPH_RUN_GROUP_ID。

使用说明

  • 执行任务需要对该任务具有 OWNERSHIP 或 OPERATE 权限。

当 EXECUTE TASK 命令触发任务运行时,Snowflake 会验证对任务具有 OWNERSHIP 权限的角色是否也具有分配给该任务的仓库的 USAGE 权限,以及全局 EXECUTE TASK 权限;如果没有,则会产生错误。

任务始终使用原始所有者角色的权限运行,即使具有 OPERATE 权限的其他角色使用 EXECUTE TASK 来运行任务也是如此。

  • By default, Snowflake runs tasks by using the system user with the privileges of the task owner role. To run a task as a specific user, configure the task with EXECUTE AS USER. For more information, see Run tasks with user privileges.

  • For the USING CONFIG option:

    • If the task graph is currently executing and you run this command, Snowflake waits for the current execution to complete before starting a new execution with the dynamic configuration.
    • If you run this command multiple times while a task is executing, Snowflake uses the configuration from the most recent command for the next run. Previous configurations are replaced and won’t be executed.
    • The dynamic configuration only applies to the single execution triggered by this command. Subsequent scheduled runs use the default CONFIG parameter from the task definition.
  • The SQL command can only execute a standalone task or the root task in a task graph. If a child task is input, the command returns a user error.

  • Manually executing a standalone or root task establishes a version of the task. The standalone task or entire task graph completes its run with this version. For more information about task versions, see Versioning of task runs.

  • A suspended root task is run without resuming the task; there is no need to explicitly resume the root task before you execute this SQL command. However, EXECUTE TASK does not automatically resume child tasks in the task graph. The command skips any child tasks that are suspended.

    To recursively resume all dependent tasks tied to a root task in a task graph, query the SYSTEM$TASK_DEPENDENTS_ENABLE function rather than enabling each task individually (using ALTER TASK … RESUME).

    As a best practice when testing new or modified task graphs, set the root task to run on its intended production schedule but leave it in a suspended state. When you have tested the task graph successfully, resume the root task. Note that you must resume any suspended child tasks in the task graph for testing; otherwise, they are skipped during runs of the task graph.

  • 如果任务的实例没有在运行,则会立即开始新的运行。

  • If another instance is scheduled (that is, if the task shows a SCHEDULED state in the TASK_HISTORY output), the requested run replaces the scheduled run. The requested run starts immediately, using the current timestamp as the scheduled time.

  • If the task or task graph is currently queueing or executing (that is, if the task shows an EXECUTING state in the TASK_HISTORY output), then the current run continues using the task version that was current when the command was executed. A new run is then scheduled to start, at a time depending on the task type:

    • 对于独立任务,计划在当前运行完成后开始新的运行。
    • For task graphs, the behavior depends on the OVERLAP_POLICY setting. For more information, see OVERLAP_POLICY in the CREATE TASK documentation.

如果在下一次计划运行开始之前再次执行 EXECUTE TASK 命令,则请求的运行将取代计划的运行。

  • If a task fails with an unexpected error, you can receive a notification about the error. For more information on configuring task error notifications refer to Set up error notifications for tasks.
  • 要查看任务信息,您可以:
    • In Snowsight, in the navigation menu, select Transformation » Tasks.
    • Call the COMPLETE_TASK_GRAPHS table function, and examine the results.

示例

The following examples show how to manually trigger a task run and how to use a dynamic CONFIG.

Manually trigger a task run

Manually trigger a run of a task named mytask:

EXECUTE TASK mytask;

Use a dynamic CONFIG

Create a root task named my_root_task with a default configuration:

CREATE OR REPLACE TASK my_root_task
  WAREHOUSE = regress
  SCHEDULE = '10 m'
  CONFIG = $${
    "environment": "production",
    "output_paths": {
      "logs": "/prod/logs",
      "results": "/prod/results"
    }
  }$$
  AS ...;

Now, execute the task and specify a dynamic configuration:

EXECUTE TASK my_root_task
  USING CONFIG=$${
    "output_paths": {
      "results": "/temp/testing"
    }
  }$$;

The following example shows the resulting configuration for this execution:

{
  "environment": "production",
  "output_paths": {
    "logs": "/prod/logs",
    "results": "/temp/testing"
  }
}

The environment field and the output_paths.logs field remain unchanged from the default configuration; only output_paths.results is updated with the dynamic value.