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
Pathlibrary.
[1]:
import covalent as ct
from covalent.triggers import DirTrigger
from pathlib import Path
Create a file in the current directory named
my_text_file.txtand populate it with some numbers.
[2]:
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
DirTriggerobject performs a trigger action whenever a file is modified in the current directory.
[3]:
dir_trigger = DirTrigger(dir_path=str(Path(".").resolve()), event_names="modified")
Create a simple workflow called
my_workflowand assign the trigger to the lattice with thetriggersnamed keyword argument.
[4]:
@ct.lattice(triggers=dir_trigger)
@ct.electron
def my_workflow():
return "File was changed."
Dispatch the
my_workflowlattice.
[5]:
dispatch_id = ct.dispatch(my_workflow)()
# Show the dispatch ID
print(dispatch_id)
6e3a3e60-f105-48b9-95ad-fdff968a3f2f
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_workflowis dispatched whenever you changemy_text_file.txt.To stop the triggering behavior, use the
ct.stop_triggersfunction.
[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