Skip to content

Commit

Permalink
Changes for triggers rtd and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunPsiog committed Sep 25, 2023
1 parent 154b0dd commit 44061cb
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 18 deletions.
2 changes: 2 additions & 0 deletions doc/source/api/triggers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Examples

- :doc:`Add a timed trigger to a workflow <../../how_to/execution/trigger_time>`
- :doc:`Add a directory trigger to a workflow <../../how_to/execution/trigger_dir>`
- :doc:`Add a sqlite trigger to a workflow <../../how_to/execution/trigger_sqlite>`
- :doc:`Add a database trigger to a workflow <../../how_to/execution/trigger_database>`
18 changes: 16 additions & 2 deletions doc/source/features/triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,24 @@ def my_workflow():
```


4. `DatabaseTrigger`: This trigger monitors the database for changes and performs the trigger action when changes occur. It is helpful for automating tasks in response to database updates. For example:

```{code-block} python
from covalent.triggers import DatabaseTrigger
import covalent as ct
database_trigger = DatabaseTrigger(db_path="db path",table_name='table name')
@ct.lattice(triggers=database_trigger)
def my_workflow():
...
```

These triggers can be easily integrated into your Covalent workflows to automate various tasks based on the desired conditions.

## Trigger How-to Guides

For further examples on how to use triggers, check out the Trigger how to guides:
- {doc}`How to add a directory trigger to a lattice <../how_to/coding/dir_trigger>`
- {doc}`How to add a time trigger to a lattice <../how_to/coding/time_trigger>`
- {doc}`How to add a directory trigger to a lattice <../../how_to/execution/trigger_dir>`
- {doc}`How to add a time trigger to a lattice <../../how_to/execution/trigger_time>`
- {doc}`How to add a sqlite trigger to a lattice <../../how_to/execution/trigger_sqlite>`
- {doc}`How to add a database trigger to a lattice <../../how_to/execution/trigger_database>`
183 changes: 183 additions & 0 deletions doc/source/how_to/execution/trigger_database.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Adding a Database Trigger to a Lattice\n",
"\n",
"This example illustrates how to use a covalent.trigger.DatabaseTrigger to trigger workflow dispatches automatically at a specified interval.\n",
"\n",
"## Prerequisites\n",
"\n",
"Import Covalent and the trigger."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import covalent as ct\n",
"from covalent.triggers import DatabaseTrigger"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Procedure\n",
"\n",
"1. Create a `Database Trigger` object that performs a trigger."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"database_trigger = DatabaseTrigger(db_path='path/to/your/database',table_name='table name')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Create a workflow:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"@ct.lattice\n",
"@ct.electron\n",
"def my_workflow():\n",
" return 42"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Dispatch `my_workflow`, disabling its first execution using the `disable_run` parameter in `ct.dispatch`."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5041f1fc-8943-4b96-9f26-a3c7f35cabef\n"
]
}
],
"source": [
"dispatch_id = ct.dispatch(my_workflow)()\n",
"print(dispatch_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Attach the trigger to the `dispatch_id` and register it with the trigger server."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"database_trigger.lattice_dispatch_id = dispatch_id\n",
"database_trigger.register()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Monitor the Covalent UI. Watch the Dashboard for new dispatches of `my_workflow`.\n",
"\n",
"6. In the Covalent UI, observe that a new `my_workflow` is dispatched every five seconds.\n",
"\n",
"7. To disable triggers on the dispatch, use the `ct.stop_triggers` function."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2023-09-22 07:37:27,218] [DEBUG] local.py: Line 334 in stop_triggers: Triggers for following dispatch_ids have stopped observing:\n",
"[2023-09-22 07:37:27,220] [DEBUG] local.py: Line 336 in stop_triggers: 5041f1fc-8943-4b96-9f26-a3c7f35cabef\n"
]
}
],
"source": [
"ct.stop_triggers(dispatch_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the `stop_triggers` function disables all triggers attached to the specified dispatch. \n",
"\n",
"## See Also\n",
"\n",
"[Adding a Directory Trigger to a Lattice](./trigger_dir.ipynb)\n",
"\n",
"[Adding a TimeTrigger to a Lattice](./trigger_time.ipynb)\n",
"\n",
"[Adding a SQLite Trigger to a Lattice](./trigger_sqlite.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
},
"vscode": {
"interpreter": {
"hash": "ffe78875ce1aa6161f50f6a6dec2555e7255bbdb44cc39b93c0dfc1daa8da522"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
6 changes: 5 additions & 1 deletion doc/source/how_to/execution/trigger_dir.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@
"\n",
"## See Also\n",
"\n",
"[Adding a Time Trigger to a Lattice](./trigger_time.ipynb)"
"[Adding a Time Trigger to a Lattice](./trigger_time.ipynb)\n",
"\n",
"[Adding a Database Trigger to a Lattice](./trigger_database.ipynb)\n",
"\n",
"[Adding a SQLite Trigger to a Lattice](./trigger_sqlite.ipynb)"
]
},
{
Expand Down
Loading

0 comments on commit 44061cb

Please sign in to comment.