def test_atomic_lock_with(): lock = flufl.lock.Lock('/tmp/test-atomic-lock1.lock') assert not lock.is_locked lock.lock() assert lock.is_locked with utils.atomic_lock('/tmp/', 'test-atomic-lock1') as lock_attained: assert not lock_attained lock.unlock() with utils.atomic_lock('/tmp/', 'test-atomic-lock1') as lock_attained: assert lock_attained
def handle(self, *args, **options): """Command entry point.""" self.dry_run = options.get('dry_run', False) self.successful_verdict = ( amo.WOULD_HAVE_BEEN_AUTO_APPROVED if self.dry_run else amo.AUTO_APPROVED) self.stats = Counter() # Get a lock before doing anything, we don't want to have multiple # instances of the command running in parallel. lock = atomic_lock(settings.TMP_PATH, LOCK_NAME, lifetime=15 * 60) with lock as lock_attained: if lock_attained: qs = self.fetch_candidates() self.stats['total'] = len(qs) for version in qs: self.process(version) self.log_final_summary(self.stats) else: # We didn't get the lock... log.error('auto-approve lock present, aborting.')
def test_prevent_multiple_runs_in_parallel(self): # Create a lock manually, the command should exit immediately without # doing anything. with atomic_lock(settings.TMP_PATH, auto_approve.LOCK_NAME): call_command('auto_approve') assert self.log_final_summary_mock.call_count == 0 assert self.file.reload().status == amo.STATUS_AWAITING_REVIEW
def extract(self): """ Will make all the directories and extract the files. Raises error on nasty files. :returns: `True` if successfully extracted, `False` in case of an existing lock. """ lock = atomic_lock( settings.TMP_PATH, 'file-viewer-%s' % self.file.pk, lifetime=LOCKED_LIFETIME) with lock as lock_attained: if lock_attained: if self.is_extracted(): # Be vigilent with existing files. It's better to delete # and re-extract than to trust whatever we have # lying around. task_log.warning( 'cleaning up %s as there were files lying around' % self.dest) self.cleanup() try: os.makedirs(self.dest) except OSError as err: task_log.error( 'Error (%s) creating directories %s' % (err, self.dest)) raise if self.is_search_engine() and self.src.endswith('.xml'): shutil.copyfileobj( storage.open(self.src, 'rb'), open( os.path.join(self.dest, self.file.filename), 'wb')) else: try: extracted_files = extract_xpi(self.src, self.dest) self._verify_files(extracted_files) except Exception as err: task_log.error( 'Error (%s) extracting %s' % (err, self.src)) raise return lock_attained
def handle(self, *args, **options): """Command entry point.""" self.post_review = waffle.switch_is_active('post-review') self.dry_run = options.get('dry_run', False) self.max_average_daily_users = int( get_config('AUTO_APPROVAL_MAX_AVERAGE_DAILY_USERS') or 0) self.min_approved_updates = int( get_config('AUTO_APPROVAL_MIN_APPROVED_UPDATES') or 0) if self.min_approved_updates <= 0 or self.max_average_daily_users <= 0: # Auto approval are shut down if one of those values is not present # or <= 0. url = '%s%s' % ( settings.SITE_URL, reverse('admin:zadmin_config_changelist')) raise CommandError( 'Auto-approvals are deactivated because either ' 'AUTO_APPROVAL_MAX_AVERAGE_DAILY_USERS or ' 'AUTO_APPROVAL_MIN_APPROVED_UPDATES have not been ' 'set or were set to 0. Use the admin tools Config model to ' 'set them by going to %s.' % url) self.successful_verdict = ( amo.WOULD_HAVE_BEEN_AUTO_APPROVED if self.dry_run else amo.AUTO_APPROVED) self.stats = Counter() # Get a lock before doing anything, we don't want to have multiple # instances of the command running in parallel. lock = atomic_lock(settings.TMP_PATH, LOCK_NAME, lifetime=15 * 60) with lock as lock_attained: if lock_attained: qs = self.fetch_candidates() self.stats['total'] = len(qs) for version in qs: self.process(version) self.log_final_summary(self.stats) else: # We didn't get the lock... log.error('auto-approve lock present, aborting.')
def _get_lock(): return utils.atomic_lock('/tmp/', 'test-atomic-lock3', lifetime=1)
def test_atomic_lock_with_lock_attained(): with utils.atomic_lock('/tmp/', 'test-atomic-lock2') as lock_attained: assert lock_attained