Skip to content

Tasks

Tasks are threaded execution loops that repeat the code within thier execute method.

Adding Tasks

To add a task, create a class which inherits the Task base class and define your specific logic within the execute method. The Task base class handles looping the execute method as well as determining if the task should run while unprovisioned or not, which is controlled by run_unprovisioned.

NOTE For a full example see [examples\define_hcc2_io_monitor_task.py]

class Task1(Task):
    """Thread task 1."""

    def __init__(self, run_unprovisioned=True):
        super().__init__("Task 1", run_unprovisioned)
        self.api = RestAPI()

    def execute(self):
        """Thread task 1 loop function"""

        # Add task logic here.
        time.sleep(1)

Then at the bottom of [app.py] instantiate the task and start it.

#                      Start Application Task Loop
# =========================================================================
info_banner(f"Starting Application {AppConfig.app_func_name}")

if not Provisioning.is_provisioned:
    info_banner("Waiting On Provisioning Data")

# Init task classes
try:
    task1 = Task1()
    task1.start()

    task2 = Task2()
    task2.start()

except Exception as exc:
    hcc2_logger.info(f"{AppConfig.app_func_name} failed due to exception: {exc}")
    hcc2_logger.info(f"Stopping {AppConfig.app_func_name}.")

    task1.stop()
    task2.stop()

    time.sleep(10) # Prevent rapid restarting on error
    sys.exit(ExitCode.UNEXPECTED_ERROR)