安排笔记本运行

When you create a schedule for running your notebook, Snowsight creates a task to run your notebook on that schedule. Snowsight executes the notebook in a non-interactive mode, cell by cell from top to bottom. The task used to run the notebook is owned by the notebook owner role and uses the notebook warehouse to run. By default, task runs of the notebook auto-suspend after 10 failures. For more information about tasks, see Introduction to tasks.

每次运行笔记本都会启动笔记本并将其与笔记本仓库连接起来。因此,笔记本使用的仓库将恢复并保持活动状态,直到计划任务完成 15 分钟后为止。

按计划运行笔记本所需的权限

由于按计划运行笔记本的过程会创建和执行任务,因此您必须使用具有以下权限的角色:

PrivilegeObject
EXECUTE TASKAccount
USAGEDatabase containing the notebook
USAGE, CREATE TASKSchema containing the notebook

计划您的笔记本

要计划笔记本的运行,请通过执行以下操作来创建任务:

  1. In the navigation menu, select Projects » Notebooks.

  2. 找到并选择要计划的笔记本。

  3. In the notebook, select the schedule button, then Create schedule.

    Schedule button at the top right corner.

    The Schedule a notebook run dialog appears.

  4. For Schedule name, enter a name for the notebook schedule. This is used as the name of the task that runs the notebook.

  5. For Frequency, select a frequency at which to run the notebook (for example, Daily).

  6. Depending on the frequency that you select, adjust the Scheduled time and other options to match when you want the notebook to run.

  7. Optionally, for Parameter, you can add command-line syntax arguments to pass to the scheduled notebook. For example: key1=value1 key2=value2 --option2.

    Note

    If you pass -- (two dashes) as a standalone argument, then any argument passed before the dashes will be interpreted as passed to the notebook runtime.

  8. Review the preview of the schedule, and select Create.

创建了一项任务,用于计划笔记本的运行。

管理笔记本计划

为笔记本创建计划后,您可以通过笔记本所在架构的任务列表查看和编辑计划。

  1. In the navigation menu, select Projects » Notebooks.
  2. 找到并选择要管理计划的笔记本。
  3. In the notebook, select the schedule button, then View schedules. This displays a table of all tasks contained in the schema that the notebook is in.
  4. Use the vertical ellipsis More actions for worksheet menu on the task that runs your notebook and select an action.

You can make edits such as changing the time or frequency of the schedule, and suspending or dropping the task completely. See Introduction to tasks for more details on managing tasks.

将实参传递给计划的笔记本

You can pass command-line arguments to a scheduled notebook using syntax such as key1=value1 key2=value2 --option2. You can then access these parameters within the scheduled notebook using sys.argv inside the notebook.

# first argument
sys.argv[0]

# print the entire list
st.write(sys.argv)

查看过去的笔记本计划运行时间

笔记本按计划运行后,您可以查看其运行历史记录:

  1. In the navigation menu, select Projects » Notebooks.

  2. 找到并选择要计划的笔记本。

  3. In the notebook, select the schedule button, then View run history.

    The Run History dialog for your notebook appears.

  4. You can review the run history for the notebook, including run activity generated by the scheduled task or an API. Notebook runs performed by a user are not included. You can review the following details:

    • Trigger: The name of the task that caused the notebook to run.
    • Last Ran: The timestamp of the last scheduled run of the notebook.
    • Status: The status of the task that ran.
    • Duration: The length of time it took to run the notebook.
    • Results: A link to the results of the notebook run. The results are read-only and the notebook cells cannot be edited. You can select Edit current notebook to open and edit the current version of the notebook.
  5. Optionally select View all tasks in schema to see a table of all tasks contained in the schema.

  6. Select Done to return to your notebook.

For more information on viewing task history, see View the task history for your account.

使用您自己的计划

To run a notebook using your own scheduling tool like Airflow, use the EXECUTE NOTEBOOK command. You can also pass command-line arguments to the scheduled notebook directly when running this command. For example:

EXECUTE NOTEBOOK DB.SCHEMA.NOTEBOOK_NAME('--env staging --tablename staging-table');

这会从上到下逐个单元格执行笔记本。可在笔记本的运行历史记录部分中查看结果。

限制

  • 您的计划笔记本的运行历史记录仅限于最近七天。
  • Changing the name of a notebook running as a scheduled task may cause an error in the task. You can manually edit the task using the CREATE TASK command to call the notebook with the changed name.
  • 不支持嵌套执行 SPCS 笔记本。计划的 SPCS 笔记本无法在其代码中运行其他 SPCS 笔记本。
  • The task’s owner role must match the notebook’s owner role. Scheduled runs do not support secondary roles, but you can switch to a role granted to the task’s owner role. For example, in most cases, you can switch to the PUBLIC role:
    from snowflake.snowpark.context import get_active_session
    session = get_active_session()
    session.use_role('PUBLIC')

笔记本运行失败

  • If a scheduled notebook run results in an error, that run will appear in the run history with a Failed status. The user who created the notebook can open the failed run and isolate the cell where the error occurred.