Skip to content

Commit 1fe0e42

Browse files
committed
feat(writer): add support for CRON blueprints and triggers in BlueprintRunner - AB-881
1 parent bbc8f7d commit 1fe0e42

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

src/writer/blueprints.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def __init__(self, session: writer.core.WriterSession):
6969
def api_blueprints(self):
7070
return self._gather_api_blueprints()
7171

72+
@property
73+
def cron_blueprints(self):
74+
return self._gather_cron_blueprints()
75+
7276
@contextmanager
7377
def _get_executor(self) -> Generator[ThreadPoolExecutor, None, None]:
7478
"""Return the application's thread pool executor.
@@ -137,6 +141,17 @@ def is_blueprint_api_available(
137141
"""
138142
return blueprint_id in self.api_blueprints
139143

144+
def is_blueprint_cron_available(
145+
self, blueprint_id: str
146+
):
147+
"""
148+
Checks if a blueprint with the given key is available for Cron execution.
149+
150+
:param blueprint_id: The blueprint identifier.
151+
:return: True if the blueprint is available for Cron execution, False otherwise.
152+
"""
153+
return blueprint_id in self.cron_blueprints
154+
140155
def get_blueprint_api_trigger(
141156
self, blueprint_id: str
142157
):
@@ -152,36 +167,67 @@ def get_blueprint_api_trigger(
152167
)
153168
return self.api_blueprints[blueprint_id]
154169

155-
def _gather_api_blueprints(self):
170+
def get_blueprint_cron_trigger(
171+
self, blueprint_id: str
172+
):
156173
"""
157-
Gathers all blueprints that have an API trigger.
174+
Retrieves the Cron trigger for a given blueprint key.
158175
159-
:return: A set of blueprint keys that have an API trigger.
176+
:param blueprint_key: The blueprint identifier.
177+
:return: The Cron trigger component.
178+
"""
179+
if not self.is_blueprint_cron_available(blueprint_id):
180+
raise ValueError(
181+
f'Cron trigger not found for blueprint "{blueprint_id}".'
182+
)
183+
return self.cron_blueprints[blueprint_id]
184+
185+
def _gather_blueprints_by_trigger(self, trigger_type: str):
186+
"""
187+
Gathers all blueprints that have a trigger of the specified type.
188+
189+
:param trigger_type: The trigger component type (e.g., "blueprints_apitrigger").
190+
:return: A dict mapping blueprint IDs to their trigger IDs.
160191
"""
161192
triggers = [
162193
c for c in self.session.session_component_tree.components.values()
163-
if c.type == "blueprints_apitrigger"
164-
]
165-
api_blueprints = {}
194+
if c.type == trigger_type
195+
]
196+
blueprints = {}
166197

167198
for trigger in triggers:
168199
parent_blueprint_id = \
169200
self.session.session_component_tree.get_parent(trigger.id)[0]
170201
parent_blueprint = \
171202
self.session.session_component_tree.get_component(
172203
parent_blueprint_id
173-
)
204+
)
174205

175206
if (
176207
parent_blueprint
177208
and
178209
parent_blueprint.type == "blueprints_blueprint"
179210
):
180211
# Store the blueprint key against its trigger ID
181-
api_blueprints[parent_blueprint_id] = \
182-
trigger.id
212+
blueprints[parent_blueprint_id] = trigger.id
213+
214+
return blueprints
215+
216+
def _gather_api_blueprints(self):
217+
"""
218+
Gathers all blueprints that have an API trigger.
219+
220+
:return: A dict mapping blueprint IDs to their API trigger IDs.
221+
"""
222+
return self._gather_blueprints_by_trigger("blueprints_apitrigger")
183223

184-
return api_blueprints
224+
def _gather_cron_blueprints(self):
225+
"""
226+
Gathers all blueprints that have a Cron trigger.
227+
228+
:return: A dict mapping blueprint IDs to their Cron trigger IDs.
229+
"""
230+
return self._gather_blueprints_by_trigger("blueprints_crontrigger")
185231

186232
def run_blueprint_via_api(
187233
self,
@@ -194,6 +240,8 @@ def run_blueprint_via_api(
194240
Executes a blueprint by its key via the API.
195241
196242
:param blueprint_id: The blueprint identifier.
243+
:param trigger_type: The type of trigger ("API" or "Cron").
244+
:param branch_id: Optional branch ID to start execution from.
197245
:param execution_environment: The execution environment for
198246
the blueprint.
199247
:return: The result of the blueprint execution.
@@ -203,7 +251,11 @@ def run_blueprint_via_api(
203251

204252
trigger_id = branch_id
205253
if trigger_id is None:
206-
trigger_id = self.get_blueprint_api_trigger(blueprint_id)
254+
if trigger_type == "Cron" or not self.is_blueprint_api_available(blueprint_id):
255+
trigger_id = self.get_blueprint_cron_trigger(blueprint_id)
256+
trigger_type = "Cron"
257+
else:
258+
trigger_id = self.get_blueprint_api_trigger(blueprint_id)
207259

208260
return self.run_branch(
209261
trigger_id,

0 commit comments

Comments
 (0)