1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- def save_archival(resource, status_id, reason, url_redirected_to,
- download_result, archive_result, log):
- '''Writes to the archival table the result of an attempt to download
- the resource.
- May propagate a CkanError.
- '''
- now = datetime.datetime.now()
- from ckanext.archiver.model import Archival, Status
- from ckan import model
- archival = Archival.get_for_resource(resource['id'])
- first_archival = not archival
- previous_archival_was_broken = None
- if not archival:
- archival = Archival.create(resource['id'])
- model.Session.add(archival)
- else:
- log.info('Archival from before: %r', archival)
- previous_archival_was_broken = archival.is_broken
- try:
- revision = model.Session.query(model.Revision) \
- .get(resource['revision_id'])
- archival.resource_timestamp = revision.timestamp
- except AttributeError:
- # CKAN 2.9 doesn't have revisions, so we can't get a timestamp
- pass
- # Details of the latest archival attempt
- archival.status_id = status_id
- archival.is_broken = Status.is_status_broken(status_id)
- archival.reason = reason
- archival.url_redirected_to = url_redirected_to
- # Details of successful archival
- if archival.is_broken is False:
- archival.cache_filepath = archive_result['cache_filepath']
- archival.cache_url = archive_result['cache_url']
- archival.size = download_result['size']
- archival.mimetype = download_result['mimetype']
- archival.hash = download_result['hash']
- archival.etag = download_result['headers'].get('etag')
- archival.last_modified = download_result['headers'].get('last-modified')
- # History
- if archival.is_broken is False:
- archival.last_success = now
- archival.first_failure = None
- archival.failure_count = 0
- else:
- log.info('First_archival=%r Previous_broken=%r Failure_count=%r' %
- (first_archival, previous_archival_was_broken,
- archival.failure_count))
- if first_archival or previous_archival_was_broken is False:
- # i.e. this is the first failure (or the first archival)
- archival.first_failure = now
- archival.failure_count = 1
- else:
- archival.failure_count += 1
- archival.updated = now
- log.info('Archival saved: %r', archival)
- model.repo.commit_and_remove()
|