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#

  1. Import Covalent, the trigger, and the Path library.

[1]:
import covalent as ct
from covalent.triggers import DirTrigger
from pathlib import Path
  1. Create a file in the current directory named my_text_file.txt and 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#

  1. Create a directory trigger. A DirTrigger object 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")
  1. Create a simple workflow called my_workflow and assign the trigger to the lattice with the triggers named keyword argument.

[4]:
@ct.lattice(triggers=dir_trigger)
@ct.electron
def my_workflow():
    return "File was changed."
  1. Dispatch the my_workflow lattice.

[5]:
dispatch_id = ct.dispatch(my_workflow)()
# Show the dispatch ID
print(dispatch_id)
6e3a3e60-f105-48b9-95ad-fdff968a3f2f
  1. Monitor the Covalent UI. Watch the Dashboard for new dispatches of my_workflow.

  2. Make a change to the file: Add or remove a number and save the file.

  3. In the Covalent UI, observe that a new my_workflow is dispatched whenever you change my_text_file.txt.

  4. 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