Make the repositoryactioncount worker disconnect from the DB between runs
This commit is contained in:
parent
75e32ad700
commit
6eaf1dbb3f
1 changed files with 30 additions and 28 deletions
|
@ -3,7 +3,8 @@ import logging
|
||||||
from apscheduler.schedulers.blocking import BlockingScheduler
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
from data.database import Repository, LogEntry, RepositoryActionCount, db_random_func, fn
|
from data.database import (Repository, LogEntry, RepositoryActionCount, db_random_func, fn,
|
||||||
|
UseThenDisconnect)
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
|
|
||||||
POLL_PERIOD_SECONDS = 30
|
POLL_PERIOD_SECONDS = 30
|
||||||
|
@ -15,36 +16,37 @@ sched = BlockingScheduler()
|
||||||
def count_repository_actions():
|
def count_repository_actions():
|
||||||
""" Counts actions for a random repository for the previous day. """
|
""" Counts actions for a random repository for the previous day. """
|
||||||
|
|
||||||
try:
|
with UseThenDisconnect(app.config):
|
||||||
# Get a random repository to count.
|
|
||||||
today = date.today()
|
|
||||||
yesterday = today - timedelta(days=1)
|
|
||||||
has_yesterday_actions = (RepositoryActionCount.select(RepositoryActionCount.repository)
|
|
||||||
.where(RepositoryActionCount.date == yesterday))
|
|
||||||
|
|
||||||
to_count = (Repository.select()
|
|
||||||
.where(~(Repository.id << (has_yesterday_actions)))
|
|
||||||
.order_by(db_random_func()).get())
|
|
||||||
|
|
||||||
logger.debug('Counting: %s', to_count.id)
|
|
||||||
|
|
||||||
actions = (LogEntry.select()
|
|
||||||
.where(LogEntry.repository == to_count,
|
|
||||||
LogEntry.datetime >= yesterday,
|
|
||||||
LogEntry.datetime < today)
|
|
||||||
.count())
|
|
||||||
|
|
||||||
# Create the row.
|
|
||||||
try:
|
try:
|
||||||
RepositoryActionCount.create(repository=to_count, date=yesterday, count=actions)
|
# Get a random repository to count.
|
||||||
except:
|
today = date.today()
|
||||||
logger.exception('Exception when writing count')
|
yesterday = today - timedelta(days=1)
|
||||||
|
has_yesterday_actions = (RepositoryActionCount.select(RepositoryActionCount.repository)
|
||||||
|
.where(RepositoryActionCount.date == yesterday))
|
||||||
|
|
||||||
return True
|
to_count = (Repository.select()
|
||||||
|
.where(~(Repository.id << (has_yesterday_actions)))
|
||||||
|
.order_by(db_random_func()).get())
|
||||||
|
|
||||||
except Repository.DoesNotExist:
|
logger.debug('Counting: %s', to_count.id)
|
||||||
logger.debug('No further repositories to count')
|
|
||||||
return False
|
actions = (LogEntry.select()
|
||||||
|
.where(LogEntry.repository == to_count,
|
||||||
|
LogEntry.datetime >= yesterday,
|
||||||
|
LogEntry.datetime < today)
|
||||||
|
.count())
|
||||||
|
|
||||||
|
# Create the row.
|
||||||
|
try:
|
||||||
|
RepositoryActionCount.create(repository=to_count, date=yesterday, count=actions)
|
||||||
|
except:
|
||||||
|
logger.exception('Exception when writing count')
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Repository.DoesNotExist:
|
||||||
|
logger.debug('No further repositories to count')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Reference in a new issue