Skip to content

Commit 1a93b49

Browse files
Fixed HookConfig.atomic flag, which was ignored in fire_hook method. (#242)
1 parent 217f18a commit 1a93b49

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Please use [this](https://docs.gitlab.com/ee/development/changelog.html) documen
66

77
### Fixed
88

9-
* context: Fixed CallbackManager pending_hooks loop when queue becomes empty
9+
* context: Fixed crash when calling `fire_hook` method.
10+
* context: Fixed `HookConfig.atomic` flag, which was ignored in `fire_hook` method.
1011
* index: Fixed loading handler callbacks from nested packages (@veqtor)
1112

1213
### Other

src/dipdup/context.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import time
55
from collections import deque
6+
from contextlib import AsyncExitStack
67
from contextlib import contextmanager
78
from contextlib import suppress
89
from os.path import exists
@@ -58,6 +59,7 @@
5859
from dipdup.utils import FormattedLogger
5960
from dipdup.utils import slowdown
6061
from dipdup.utils.database import execute_sql_scripts
62+
from dipdup.utils.database import in_global_transaction
6163
from dipdup.utils.database import wipe_schema
6264

6365
DatasourceT = TypeVar('DatasourceT', bound=Datasource)
@@ -331,6 +333,7 @@ async def fire_handler(
331333
handler_config=handler_config,
332334
datasource=datasource,
333335
)
336+
# NOTE: Handlers are not atomic, levels are. Do not open transaction here.
334337
with self._callback_wrapper('handler', name):
335338
await handler_config.callback_fn(new_ctx, *args, **kwargs)
336339

@@ -355,7 +358,12 @@ async def fire_hook(
355358
self._verify_arguments(new_ctx, *args, **kwargs)
356359

357360
async def _wrapper():
358-
with self._callback_wrapper('hook', name):
361+
async with AsyncExitStack() as stack:
362+
363+
stack.enter_context(self._callback_wrapper('hook', name))
364+
if hook_config.atomic:
365+
await stack.enter_async_context(in_global_transaction())
366+
359367
await hook_config.callback_fn(ctx, *args, **kwargs)
360368

361369
if wait:

src/dipdup/scheduler.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
from contextlib import AsyncExitStack
32
from datetime import datetime
43
from functools import partial
54
from typing import Any
@@ -16,7 +15,6 @@
1615
from dipdup.context import HookContext
1716
from dipdup.exceptions import ConfigurationError
1817
from dipdup.utils import FormattedLogger
19-
from dipdup.utils.database import in_global_transaction
2018

2119
DEFAULT_CONFIG = {
2220
'apscheduler.jobstores.default.class': 'apscheduler.jobstores.memory:MemoryJobStore',
@@ -51,14 +49,10 @@ async def _job_wrapper(ctx: DipDupContext, *args, **kwargs) -> None:
5149
hook_config=hook_config,
5250
)
5351

54-
async with AsyncExitStack() as stack:
55-
if hook_config.atomic:
56-
await stack.enter_async_context(in_global_transaction())
52+
await job_ctx.fire_hook(hook_config.callback, *args, **kwargs)
5753

58-
await job_ctx.fire_hook(hook_config.callback, *args, **kwargs)
59-
60-
if job_config.daemon:
61-
raise ConfigurationError('Daemon jobs are intended to run forever')
54+
if job_config.daemon:
55+
raise ConfigurationError('Daemon jobs are intended to run forever')
6256

6357
logger = FormattedLogger(
6458
name=f'dipdup.hooks.{hook_config.callback}',

0 commit comments

Comments
 (0)