Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Avoid negative waits after workflow scheduling deadlines

HuaTNAe096efc0dce6
diff --git a/src/python/txtai/workflow/base.py b/src/python/txtai/workflow/base.py
index 61d597be..aaec4cfb 100644
--- a/src/python/txtai/workflow/base.py
+++ b/src/python/txtai/workflow/base.py
@@ -96,7 +96,7 @@ class Workflow:
# Schedule using localtime
schedule = croniter(cron, datetime.now().astimezone()).get_next(datetime)
logger.info("'%s' next run scheduled for %s", self.name, schedule.isoformat())
- time.sleep(schedule.timestamp() - time.time())
+ time.sleep(max(schedule.timestamp() - time.time(), 0))
# Run workflow
# pylint: disable=W0703
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index ce88f7cd..ce193a57 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -11,11 +11,15 @@ import tempfile
import sys
import unittest
+from datetime import datetime, timezone
from itertools import product
+from unittest.mock import patch
import numpy as np
import torch
+from croniter import croniter
+
from txtai.api import API
from txtai.app import Application
from txtai.embeddings import Documents, Embeddings
@@ -537,6 +541,21 @@ class TestWorkflow(unittest.TestCase):
self.assertIn("a sentence to segment", output.getvalue())
+ def testScheduleElapsedWorkflow(self):
+ """
+ Test that an elapsed cron deadline still runs its scheduled task.
+ """
+
+ start = datetime(2026, 1, 1, tzinfo=timezone.utc)
+ executed = []
+ workflow = Workflow([Task(action=executed.append)])
+
+ with patch("txtai.workflow.base.croniter", return_value=croniter("* * * * * *", start)):
+ with patch("txtai.workflow.base.time.time", return_value=start.timestamp() + 1.01):
+ workflow.schedule("* * * * * *", ["test"], 1)
+
+ self.assertEqual(executed, [["test"]])
+
def testScheduleErrorWorkflow(self):
"""
Test workflow schedules with errors