-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add worker entrypoint * feat: add run_worker function
- Loading branch information
Showing
10 changed files
with
102 additions
and
24 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from rocketry import Rocketry | ||
from rocketry.conds import every | ||
|
||
from fastqueue.config import settings | ||
from fastqueue.database import SessionLocal | ||
from fastqueue.logger import get_logger | ||
from fastqueue.models import Queue | ||
from fastqueue.services import QueueService | ||
|
||
logger = get_logger(__name__) | ||
worker = Rocketry() | ||
|
||
|
||
@worker.task(every(f"{settings.queue_cleanup_interval_seconds} seconds")) | ||
def queue_cleanup_task(): | ||
logger.info("starting queue_cleanup task") | ||
|
||
with SessionLocal() as session: | ||
for result in session.query(Queue.id): | ||
queue_id = result[0] | ||
QueueService.cleanup(id=queue_id, session=session) | ||
|
||
logger.info("finishing queue_cleanup task") | ||
|
||
|
||
def run_worker(): | ||
return worker.run(debug=settings.debug) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from fastqueue.models import Message | ||
from fastqueue.workers import queue_cleanup_task | ||
from tests.factories import MessageFactory | ||
|
||
|
||
def test_queue_cleanup(session, queue): | ||
messages = MessageFactory.build_batch(5, queue_id=queue.id) | ||
for message in messages: | ||
message.expired_at = datetime.utcnow() - timedelta(seconds=1) | ||
session.add(message) | ||
session.commit() | ||
|
||
assert session.query(Message).filter_by(queue_id=queue.id).count() == 5 | ||
|
||
assert queue_cleanup_task() is None | ||
|
||
assert session.query(Message).filter_by(queue_id=queue.id).count() == 0 |