2014-09-08 20:43:17 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from tempfile import SpooledTemporaryFile
|
2014-09-11 19:33:10 +00:00
|
|
|
from gzip import GzipFile
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
from data import model
|
2014-09-11 19:33:10 +00:00
|
|
|
from data.archivedlogs import JSON_MIMETYPE
|
2015-12-04 20:51:53 +00:00
|
|
|
from data.database import CloseForLongOperation
|
|
|
|
from app import build_logs, log_archive, app
|
2014-09-08 20:43:17 +00:00
|
|
|
from util.streamingjsonencoder import StreamingJSONEncoder
|
2015-07-28 21:25:12 +00:00
|
|
|
from workers.worker import Worker
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
POLL_PERIOD_SECONDS = 30
|
2014-09-16 04:03:04 +00:00
|
|
|
MEMORY_TEMPFILE_SIZE = 64 * 1024 # Large enough to handle approximately 99% of builds in memory
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-28 21:25:12 +00:00
|
|
|
class ArchiveBuildLogsWorker(Worker):
|
|
|
|
def __init__(self):
|
|
|
|
super(ArchiveBuildLogsWorker, self).__init__()
|
|
|
|
self.add_operation(self._archive_redis_buildlogs, POLL_PERIOD_SECONDS)
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2015-07-28 21:25:12 +00:00
|
|
|
def _archive_redis_buildlogs(self):
|
|
|
|
""" Archive a single build, choosing a candidate at random. This process must be idempotent to
|
|
|
|
avoid needing two-phase commit. """
|
2015-09-09 17:59:45 +00:00
|
|
|
# Get a random build to archive
|
|
|
|
to_archive = model.build.get_archivable_build()
|
|
|
|
if to_archive is None:
|
|
|
|
logger.debug('No more builds to archive')
|
|
|
|
return
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2015-09-09 17:59:45 +00:00
|
|
|
logger.debug('Archiving: %s', to_archive.uuid)
|
2014-09-11 19:33:10 +00:00
|
|
|
|
2015-09-09 17:59:45 +00:00
|
|
|
length, entries = build_logs.get_log_entries(to_archive.uuid, 0)
|
|
|
|
to_encode = {
|
|
|
|
'start': 0,
|
|
|
|
'total': length,
|
|
|
|
'logs': entries,
|
|
|
|
}
|
2014-09-11 19:33:10 +00:00
|
|
|
|
2015-12-04 20:51:53 +00:00
|
|
|
with CloseForLongOperation(app.config):
|
|
|
|
with SpooledTemporaryFile(MEMORY_TEMPFILE_SIZE) as tempfile:
|
|
|
|
with GzipFile('testarchive', fileobj=tempfile) as zipstream:
|
|
|
|
for chunk in StreamingJSONEncoder().iterencode(to_encode):
|
|
|
|
zipstream.write(chunk)
|
2014-09-11 19:33:10 +00:00
|
|
|
|
2015-12-04 20:51:53 +00:00
|
|
|
tempfile.seek(0)
|
|
|
|
log_archive.store_file(tempfile, JSON_MIMETYPE, content_encoding='gzip',
|
|
|
|
file_id=to_archive.uuid)
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2015-12-04 20:51:53 +00:00
|
|
|
to_update = model.build.get_repository_build(to_archive.uuid)
|
|
|
|
to_update.logs_archived = True
|
|
|
|
to_update.save()
|
2015-07-28 21:25:12 +00:00
|
|
|
|
2016-06-20 15:51:30 +00:00
|
|
|
build_logs.expire_status(to_update.uuid)
|
2017-03-17 14:35:47 +00:00
|
|
|
build_logs.delete_log_entries(to_update.uuid)
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-07-28 21:25:12 +00:00
|
|
|
worker = ArchiveBuildLogsWorker()
|
|
|
|
worker.start()
|