Add a bulk insert for queue and notifications.
Use it for Clair spawned notifications.
This commit is contained in:
parent
eb363876cd
commit
21e3001446
4 changed files with 110 additions and 36 deletions
|
@ -1,10 +1,15 @@
|
|||
import json
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
from app import app, notification_queue
|
||||
from data import model
|
||||
from auth.auth_context import get_authenticated_user, get_validated_oauth_token
|
||||
|
||||
|
||||
DEFAULT_BATCH_SIZE = 1000
|
||||
|
||||
|
||||
def build_event_data(repo, extra_data={}, subpage=None):
|
||||
repo_string = '%s/%s' % (repo.namespace_name, repo.name)
|
||||
homepage = '%s://%s/repository/%s' % (app.config['PREFERRED_URL_SCHEME'],
|
||||
|
@ -50,14 +55,30 @@ def build_notification_data(notification, event_data, performer_data=None):
|
|||
}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def notification_batch(batch_size=DEFAULT_BATCH_SIZE):
|
||||
"""
|
||||
Context manager implementation which returns a target callable with the same signature
|
||||
as spawn_notification. When the the context block exits the notifications generated by
|
||||
the callable will be bulk inserted into the queue with the specified batch size.
|
||||
"""
|
||||
with notification_queue.batch_insert(batch_size) as queue_put:
|
||||
def spawn_notification_batch(repo, event_name, extra_data={}, subpage=None, pathargs=[],
|
||||
performer_data=None):
|
||||
event_data = build_event_data(repo, extra_data=extra_data, subpage=subpage)
|
||||
|
||||
notifications = model.notification.list_repo_notifications(repo.namespace_name,
|
||||
repo.name,
|
||||
event_name=event_name)
|
||||
path = [repo.namespace_name, repo.name, event_name] + pathargs
|
||||
for notification in list(notifications):
|
||||
notification_data = build_notification_data(notification, event_data, performer_data)
|
||||
queue_put(path, json.dumps(notification_data))
|
||||
|
||||
yield spawn_notification_batch
|
||||
|
||||
|
||||
def spawn_notification(repo, event_name, extra_data={}, subpage=None, pathargs=[],
|
||||
performer_data=None):
|
||||
event_data = build_event_data(repo, extra_data=extra_data, subpage=subpage)
|
||||
|
||||
notifications = model.notification.list_repo_notifications(repo.namespace_name,
|
||||
repo.name,
|
||||
event_name=event_name)
|
||||
for notification in list(notifications):
|
||||
notification_data = build_notification_data(notification, event_data, performer_data)
|
||||
path = [repo.namespace_name, repo.name, event_name] + pathargs
|
||||
notification_queue.put(path, json.dumps(notification_data))
|
||||
with notification_batch(1) as batch_spawn:
|
||||
batch_spawn(repo, event_name, extra_data, subpage, pathargs, performer_data)
|
||||
|
|
Reference in a new issue