Adding a Directory Trigger to a Lattice#
This example illustrates how to use a covalent.trigger.DirTrigger
to trigger workflow dispatches automatically whenever a file in a directory is modified.
Prerequisites#
Import Covalent, the trigger, and the
Path
library.
[14]:
import covalent as ct
from covalent.triggers import DirTrigger
from pathlib import Path
Create a file in the current directory named
my_text_file.txt
and populate it with some numbers.
[15]:
with open("my_text_file.txt", "w") as f:
for i in range(10):
f.write(f"{i}\n")
Procedure#
Create a directory trigger. A
DirTrigger
object performs a trigger action whenever a file is modified in the current directory.
[16]:
dir_trigger = DirTrigger(dir_path=str(Path(".").resolve()), event_names="modified")
Create a simple workflow called
my_workflow
and assign the trigger to the lattice with thetriggers
named keyword argument.
[17]:
@ct.lattice(triggers=dir_trigger)
@ct.electron
def my_workflow():
return "File was changed."
Dispatch the
my_workflow
lattice.
[18]:
dispatch_id = ct.dispatch(my_workflow)()
# Show the dispatch ID
print(dispatch_id)
656213bc-ebd3-444b-9c48-1542c497061a
Monitor the Covalent UI. Watch the Dashboard for new dispatches of
my_workflow
.Make a change to the file: Add or remove a number and save the file.
In the Covalent UI, observe that a new
my_workflow
is dispatched whenever you changemy_text_file.txt
.To stop the triggering behavior, use the
ct.stop_triggers
function.
[21]:
ct.stop_triggers(dispatch_id)
No new dispatches are triggered on my_workflow
. To demonstrate this, save another change to the my_text_file.txt
file.
Note that the stop_triggers
function disables all triggers attached to the specified dispatch.
See Also#
Adding a Time Trigger to a Lattice
Adding a Database Trigger to a Lattice
Adding a SQLite Trigger to a Lattice
[ ]: