Schedule your Snowflake Notebook to run¶
You can schedule your Snowflake Notebook to run as a task. Tasks run and execute code within Snowflake. For more information about tasks, see Introduction to tasks.
About scheduling notebook runs¶
When you create a schedule for running your notebook, Snowsight creates a task to run your notebook on that schedule. This executes the Notebook in a non-interactive mode, cell by cell from top to bottom. This task is owned by the notebook owner role and uses the notebook warehouse to run. By default, failed task runs of the notebook auto-suspend after 10 failures.
Each notebook run starts the notebook and connects it with the notebook warehouse. As a result, the warehouse used by the notebook is resumed and remains active until 15 minutes after the scheduled task completes. See Billing considerations for running Snowflake Notebooks.
Privileges required to run your notebook on a schedule¶
Because the process of running a notebook on a schedule creates and executes a task, you must use a role that has the following privileges:
Privilege |
Object |
---|---|
EXECUTE TASK |
Account |
USAGE |
Database containing the notebook |
USAGE, CREATE TASK |
Schema containing the notebook |
Schedule your notebook¶
To schedule your notebook to run, create a task by doing the following:
Note
You must use a role granted the CREATE TASK and EXECUTE TASK privileges. See required privileges section for a complete list.
Select Projects » Notebooks.
Locate and select the notebook to schedule.
In the notebook, select the schedule button, then Create schedule.
The Schedule a notebook run dialog appears.
For Schedule name, enter a name for the notebook schedule. This is used as the name of the task that runs the notebook.
For Frequency, select a frequency at which to run the notebook (for example, Daily).
Depending on the frequency that you select, adjust the Scheduled time and other options to match when you want the notebook to run.
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.Review the preview of the schedule, and select Create.
A task is created that schedules your notebook to run.
Manage notebook schedules¶
Once you create schedules for your notebook, you can view and make edits to the schedules via the task list for the schema that the notebook is in.
Select Projects » Notebooks.
Locate and select the notebook to manage schedules for.
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.
Use the vertical ellipsis 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.
Access parameters in a scheduled notebook¶
You can access parameters within a scheduled notebook using sys.argv
. For example:
# first argument
sys.argv[0]
# print the entire list
st.write(sys.argv)
Manage past scheduled notebook runs¶
After your notebook runs as scheduled, you can review the run history for the notebook:
Select Projects » Notebooks.
Locate and select the notebook to schedule.
In the notebook, select the schedule button, then View run history.
The Run History dialog for your notebook appears.
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.
Optionally select View all tasks in schema to see a table of all tasks contained in the schema.
Select Done to return to your notebook.
For more information on viewing task history, see Viewing the task history for your account.
Using your own schedule¶
Some customers may prefer running the notebook using their own scheduling tool like Airflow. To do so, they can execute the following command:
EXECUTE NOTEBOOK DB.SCHEMA.NOTEBOOK_NAME();
This executes the notebook cell by cell from top to bottom. The results are accessible in the Notebook’s run history section.
Limitations¶
The run history for your scheduled notebooks is limited to the last 7 days.
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.