def handle(self, *args, **options): invalidated = Task.invalidate_tasks() self.stdout.write('%s: %s tasks were invalidated via bug data\n' % (datetime.now().isoformat(), invalidated)) closed = TaskAttempt.close_stale_onetime_attempts() self.stdout.write('%s: %s stale one-time attempts were closed\n' % (datetime.now().isoformat(), closed)) closed = TaskAttempt.close_expired_task_attempts() self.stdout.write('%s: %s attempts for expired tasks were closed\n' % (datetime.now().isoformat(), closed))
def test_close_stale_onetime_attempts(self): """ The close_stale_onetime_attempts routine should close all expired one-time attempts, set them as requiring notification, and return the number that were closed. """ task = TaskFactory.create(repeatable=False) user = UserFactory.create() recent_attempt, expired_attempt_1, expired_attempt_2 = TaskAttemptFactory.create_batch( 3, user=user, state=TaskAttempt.STARTED, task=task) recent_attempt.created = aware_datetime(2014, 1, 29) recent_attempt.save() expired_attempt_1.created = aware_datetime(2014, 1, 1) expired_attempt_1.save() expired_attempt_2.created = aware_datetime(2014, 1, 1) expired_attempt_2.save() eq_(task.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 3) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = aware_datetime(2014, 1, 31) eq_(TaskAttempt.close_stale_onetime_attempts(), 2) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.STARTED).count(), 1) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2)
def test_close_expired_task_attempts(self): """ The close_expired_task_attempts routine should close all attempts for tasks that are no longer available, set them as requiring notification, and return the number that were closed. """ task_no_expire = TaskFactory.create() task = TaskFactory.create(end_date=timezone.now() + timedelta(days=1)) future_date = timezone.now() + timedelta(days=2) user1, user2, user3 = UserFactory.create_batch(3) TaskAttemptFactory.create( user=user1, state=TaskAttempt.STARTED, task=task) TaskAttemptFactory.create( user=user2, state=TaskAttempt.STARTED, task=task) TaskAttemptFactory.create( user=user3, state=TaskAttempt.STARTED, task=task_no_expire) eq_(task.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 2) eq_(task_no_expire.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 1) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = future_date eq_(TaskAttempt.close_expired_task_attempts(), 2) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.STARTED).count(), 0) eq_(TaskAttempt.objects.filter(task=task, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2) eq_(TaskAttempt.objects.filter(task=task_no_expire, state=TaskAttempt.STARTED).count(), 1)
def test_close_stale_onetime_attempts(self): """ The close_stale_onetime_attempts routine should close all expired one-time attempts, set them as requiring notification, and return the number that were closed. """ task = TaskFactory.create(repeatable=False) user = UserFactory.create() recent_attempt, expired_attempt_1, expired_attempt_2 = TaskAttemptFactory.create_batch( 3, user=user, state=TaskAttempt.STARTED, task=task) recent_attempt.created = aware_datetime(2014, 1, 29) recent_attempt.save() expired_attempt_1.created = aware_datetime(2014, 1, 1) expired_attempt_1.save() expired_attempt_2.created = aware_datetime(2014, 1, 1) expired_attempt_2.save() eq_(task.taskattempt_set.filter(state=TaskAttempt.STARTED).count(), 3) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = aware_datetime(2014, 1, 31) eq_(TaskAttempt.close_stale_onetime_attempts(), 2) eq_( TaskAttempt.objects.filter(task=task, state=TaskAttempt.STARTED).count(), 1) eq_( TaskAttempt.objects.filter(task=task, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2)
def test_close_expired_onetime_attempts(self): """ The close_expired_onetime_attempts routine should close all expired one-time attempts and return the number that were closed. """ user = UserFactory.create() recent_attempt, expired_attempt_1, expired_attempt_2 = TaskAttemptFactory.create_batch( 3, user=user, state=TaskAttempt.STARTED, task=self.task_not_repeatable_no_attempts) recent_attempt.created = aware_datetime(2014, 1, 29) recent_attempt.save() expired_attempt_1.created = aware_datetime(2014, 1, 1) expired_attempt_1.save() expired_attempt_2.created = aware_datetime(2014, 1, 1) expired_attempt_2.save() eq_( self.task_not_repeatable_no_attempts.taskattempt_set.filter( state=TaskAttempt.STARTED).count(), 3) with patch('oneanddone.tasks.models.timezone.now') as now: now.return_value = aware_datetime(2014, 1, 31) eq_(TaskAttempt.close_expired_onetime_attempts(), 2) eq_( TaskAttempt.objects.filter( task=self.task_not_repeatable_no_attempts, state=TaskAttempt.STARTED).count(), 1) eq_( TaskAttempt.objects.filter( task=self.task_not_repeatable_no_attempts, state=TaskAttempt.CLOSED).count(), 2)
def test_close_expired_task_attempts(self): """ The close_expired_task_attempts routine should close all attempts for tasks that are no longer available, set them as requiring notification, and return the number that were closed. """ user1, user2, user3 = UserFactory.create_batch(3) TaskAttemptFactory.create(user=user1, state=TaskAttempt.STARTED, task=self.task_end_jan) TaskAttemptFactory.create(user=user2, state=TaskAttempt.STARTED, task=self.task_end_jan) TaskAttemptFactory.create(user=user3, state=TaskAttempt.STARTED, task=self.task_no_draft) eq_( self.task_end_jan.taskattempt_set.filter( state=TaskAttempt.STARTED).count(), 2) eq_( self.task_no_draft.taskattempt_set.filter( state=TaskAttempt.STARTED).count(), 1) eq_(TaskAttempt.close_expired_task_attempts(), 2) eq_( TaskAttempt.objects.filter(task=self.task_end_jan, state=TaskAttempt.STARTED).count(), 0) eq_( TaskAttempt.objects.filter(task=self.task_end_jan, state=TaskAttempt.CLOSED, requires_notification=True).count(), 2) eq_( TaskAttempt.objects.filter(task=self.task_no_draft, state=TaskAttempt.STARTED).count(), 1)
def get_queryset(self): self.sort_headers = SortHeaders(self.request, self.list_headers) qs = TaskAttempt.objects.extra( select={ 'state_display': TaskAttempt.choice_display_extra_expression('state'), 'elapsed_time': 'TIMESTAMPDIFF(SECOND, tasks_taskattempt.created, tasks_taskattempt.modified)' }) return qs.order_by(self.sort_headers.get_order_by())
def get_queryset(self): self.sort_headers = SortHeaders(self.request, self.list_headers) qs = TaskAttempt.objects.extra( select={ 'state_display': TaskAttempt.choice_display_extra_expression('state'), 'elapsed_time': 'EXTRACT(EPOCH FROM (tasks_taskattempt.modified - tasks_taskattempt.created))' }) return qs.order_by(self.sort_headers.get_order_by())
def handle(self, *args, **options): closed = TaskAttempt.close_expired_onetime_attempts() self.stdout.write('%s expired one-time attempts were closed\n' % closed)