def lock(self): """Lock the file-viewer. If locked, no one is allowed to run any extraction in `self.src` to avoid files / directories get messed up. There is a potential race-condition between getting and setting the lock. TODO: This should be re-implemented once we're on Django 1.9+ which has support for get_or_set. :return: `True` if the lock was attained, `False` if there is an already existing lock. """ cache_key = self._cache_key(LOCKED) msg = Message(cache_key) if msg.get(): # Already locked yield False else: # Not yet locked, save flag and delete on exit. msg.save(True, time=LOCKED_TIMEOUT) try: yield True finally: msg.delete()
def test_poll_failed(self): msg = Message('file-viewer:%s' % self.file_viewer) msg.save('I like cheese.') res = self.client.get(self.poll_url()) eq_(res.status_code, 200) data = json.loads(res.content) eq_(data['status'], False) eq_(data['msg'], ['I like cheese.'])
def test_poll_failed(self): msg = Message('file-viewer:%s' % self.file_viewer) msg.save('I like cheese.') res = self.client.get(self.poll_url()) assert res.status_code == 200 data = json.loads(res.content) assert not data['status'] assert data['msg'] == ['I like cheese.']
def test_extract_file_locked_message(self): self.viewer.src = get_file('dictionary-test.xpi') assert not self.viewer.is_extracted() msg = Message(self.viewer._cache_key(LOCKED)) msg.save(True) msg = extract_file(self.viewer) assert str(msg.get()).startswith(u'File viewer is locked') msg.delete()
def test_locked(self): self.viewer.src = get_file('dictionary-test.xpi') # Lock was successfully attained assert self.viewer.extract() msg = Message(self.viewer._cache_key(LOCKED)) msg.save(True) # Not extracting, the viewer is locked, lock could not be attained assert not self.viewer.extract()
def extract_file(viewer, **kw): # This message is for end users so they'll see a nice error. msg = Message("file-viewer:%s" % viewer) msg.delete() # This flag is so that we can signal when the extraction is completed. flag = Message(viewer._extraction_cache_key()) task_log.debug("[1@%s] Unzipping %s for file viewer." % (extract_file.rate_limit, viewer)) try: flag.save("extracting") # Set the flag to a truthy value. viewer.extract() except Exception, err: if settings.DEBUG: msg.save(_("There was an error accessing file %s. %s.") % (viewer, err)) else: msg.save(_("There was an error accessing file %s.") % viewer) task_log.error("[1@%s] Error unzipping: %s" % (extract_file.rate_limit, err))
def extract_file(viewer, **kw): # This message is for end users so they'll see a nice error. msg = Message('file-viewer:%s' % viewer) msg.delete() # This flag is so that we can signal when the extraction is completed. flag = Message(viewer._extraction_cache_key()) task_log.debug('[1@%s] Unzipping %s for file viewer.' % (extract_file.rate_limit, viewer)) try: flag.save('extracting') # Set the flag to a truthy value. viewer.extract() except Exception, err: if settings.DEBUG: msg.save( _('There was an error accessing file %s. %s.') % (viewer, err)) else: msg.save(_('There was an error accessing file %s.') % viewer) task_log.error('[1@%s] Error unzipping: %s' % (extract_file.rate_limit, err))
def extract_file(file_id, **kw): # This message is for end users so they'll see a nice error. viewer = FileViewer(File.objects.get(pk=file_id)) msg = Message('file-viewer:%s' % viewer) msg.delete() # This flag is so that we can signal when the extraction is completed. flag = Message(viewer._extraction_cache_key()) task_log.debug('[1@%s] Unzipping %s for file viewer.' % ( extract_file.rate_limit, viewer)) try: flag.save('extracting') # Set the flag to a truthy value. viewer.extract() except Exception, err: if settings.DEBUG: msg.save(_('There was an error accessing file %s. %s.') % (viewer, err)) else: msg.save(_('There was an error accessing file %s.') % viewer) task_log.error('[1@%s] Error unzipping: %s' % (extract_file.rate_limit, err))
def extract_file(viewer, **kw): # This message is for end users so they'll see a nice error. msg = Message('file-viewer:%s' % viewer) msg.delete() task_log.debug('Unzipping %s for file viewer.' % viewer) try: lock_attained = viewer.extract() if not lock_attained: info_msg = _( 'File viewer is locked, extraction for %s could be ' 'in progress. Please try again in approximately 5 minutes.' % viewer) msg.save(info_msg) except Exception, err: error_message = _('There was an error accessing file %s.') % viewer if settings.DEBUG: msg.save(error_message + ' ' + err) else: msg.save(error_message) task_log.error('Error unzipping: %s' % err)