Skip to content

Commit

Permalink
Avoid result factories if persistence is turned off for a task (#15055)
Browse files Browse the repository at this point in the history
  • Loading branch information
cicdw authored Aug 23, 2024
1 parent 2f000f9 commit 0e4c2d9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/prefect/task_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,17 +705,22 @@ def start(

@contextmanager
def transaction_context(self) -> Generator[Transaction, None, None]:
result_factory = getattr(TaskRunContext.get(), "result_factory", None)

# refresh cache setting is now repurposes as overwrite transaction record
overwrite = (
self.task.refresh_cache
if self.task.refresh_cache is not None
else PREFECT_TASKS_REFRESH_CACHE.value()
)

result_factory = getattr(TaskRunContext.get(), "result_factory", None)
if result_factory and result_factory.persist_result:
store = ResultFactoryStore(result_factory=result_factory)
else:
store = None

with transaction(
key=self.compute_transaction_key(),
store=ResultFactoryStore(result_factory=result_factory),
store=store,
overwrite=overwrite,
logger=self.logger,
) as txn:
Expand Down Expand Up @@ -1199,17 +1204,21 @@ async def start(

@asynccontextmanager
async def transaction_context(self) -> AsyncGenerator[Transaction, None]:
result_factory = getattr(TaskRunContext.get(), "result_factory", None)

# refresh cache setting is now repurposes as overwrite transaction record
overwrite = (
self.task.refresh_cache
if self.task.refresh_cache is not None
else PREFECT_TASKS_REFRESH_CACHE.value()
)
result_factory = getattr(TaskRunContext.get(), "result_factory", None)
if result_factory and result_factory.persist_result:
store = ResultFactoryStore(result_factory=result_factory)
else:
store = None

with transaction(
key=self.compute_transaction_key(),
store=ResultFactoryStore(result_factory=result_factory),
store=store,
overwrite=overwrite,
logger=self.logger,
) as txn:
Expand Down

0 comments on commit 0e4c2d9

Please sign in to comment.