-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecd4f18
commit b0c9aeb
Showing
9 changed files
with
60 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import structlog | ||
from sqlalchemy.ext.asyncio import async_scoped_session | ||
from sqlalchemy.future import select | ||
|
||
from ..common.logging import LOGGER | ||
from ..config import config | ||
from ..db.queue import Queue | ||
from ..db.script import Script | ||
|
||
logger = structlog.get_logger(__name__) | ||
logger = LOGGER.bind(module=__name__) | ||
|
||
|
||
async def daemon_iteration(session: async_scoped_session) -> None: | ||
iteration_start = datetime.now() | ||
queue_entries = await session.execute(select(Queue).where(Queue.time_next_check < iteration_start)) | ||
logger.debug("Daemon Iteration: %s", iteration_start) | ||
|
||
# TODO: should the daemon check any campaigns with a state == prepared that | ||
# do not have queues? Queue creation should not be a manual step. | ||
queue_entry: Queue | ||
for (queue_entry,) in queue_entries: | ||
queued_node = await queue_entry.get_node(session) | ||
if ( | ||
queued_node.status.is_processable_script() | ||
if isinstance(queued_node, Script) | ||
else queued_node.status.is_processable_element() | ||
): | ||
logger.info(f"Processing queue_entry {queued_node.fullname}") | ||
await queue_entry.process_node(session) | ||
sleep_time = await queue_entry.node_sleep_time(session) | ||
else: | ||
# Put this entry to sleep for a while | ||
sleep_time = config.daemon.processing_interval | ||
time_next_check = iteration_start + timedelta(seconds=sleep_time) | ||
queue_entry.time_next_check = time_next_check | ||
logger.info(f"Next check for {queued_node.fullname} at {time_next_check}") | ||
try: | ||
queued_node = await queue_entry.get_node(session) | ||
if ( | ||
queued_node.status.is_processable_script() | ||
if isinstance(queued_node, Script) | ||
else queued_node.status.is_processable_element() | ||
): | ||
logger.info("Processing queue_entry %s", queued_node.fullname) | ||
await queue_entry.process_node(session) | ||
sleep_time = await queue_entry.node_sleep_time(session) | ||
else: | ||
# Put this entry to sleep for a while | ||
sleep_time = config.daemon.processing_interval | ||
time_next_check = iteration_start + timedelta(seconds=sleep_time) | ||
queue_entry.time_next_check = time_next_check | ||
logger.info(f"Next check for {queued_node.fullname} at {time_next_check}") | ||
except Exception: | ||
logger.exception() | ||
continue | ||
await session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Provide a common application root logger.""" | ||
|
||
import structlog | ||
from safir.logging import configure_logging | ||
|
||
from ..config import config | ||
|
||
configure_logging(profile=config.logging.profile, log_level=config.logging.level, name=config.logging.handle) | ||
|
||
LOGGER = structlog.get_logger(config.logging.handle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters