def test_ClockedSchedule_schedule(self):
        due_datetime = make_aware(datetime(day=26,
                                           month=7,
                                           year=3000,
                                           hour=1,
                                           minute=0))  # future time
        s = ClockedSchedule(clocked_time=due_datetime)
        dt = datetime(day=25, month=7, year=2050, hour=1, minute=0)
        dt_lastrun = make_aware(dt)

        assert s.schedule is not None
        isdue, nextcheck = s.schedule.is_due(dt_lastrun)
        assert isdue is False  # False means task isn't due, but keep checking.
        assert (nextcheck > 0) and (isdue is False) or \
            (nextcheck == s.max_interval) and (isdue is True)

        due_datetime = make_aware(datetime.now())
        s = ClockedSchedule(clocked_time=due_datetime)
        dt2_lastrun = make_aware(datetime.now())

        assert s.schedule is not None
        isdue2, nextcheck2 = s.schedule.is_due(dt2_lastrun)
        assert isdue2 is True  # True means task is due and should run.
        assert (nextcheck2 is None) and (isdue2 is True)
        print(s.schedule.enabled)

        assert s.schedule is not None
        isdue3, nextcheck3 = s.schedule.is_due(dt2_lastrun)
        print(s.schedule.clocked_time, s.schedule.enabled)
        print(isdue3, nextcheck3)
        # False means task isn't due, but keep checking.
        assert isdue3 is False
        assert (nextcheck3 is None) and (isdue3 is False)
Exemple #2
0
    def update(self, instance, validated_data):
        current_comments = (instance.comments).all()
        current_comments = current_comments.values()
        instance.subject = validated_data.get('subject', instance.subject)
        instance.calendar = validated_data.get('calendar', instance.calendar)
        instance.text = validated_data.get('text', instance.text)
        instance.image = validated_data.get('image', instance.image)
        if instance.status == ('Scheduled' or 'Draft'):
            if 'publishDateTime' in validated_data:
                instance.publishDateTime = validated_data.get('publishDateTime', instance.publishDateTime)
                instance.status = 'Scheduled'
                publish_task = instance.publishTask
                schedule = publish_task.clocked
                if schedule.enabled == True:
                    schedule.clocked_time = instance.publishDateTime
                    schedule.save()
                else:
                    new_schedule = ClockedSchedule(clocked_time=instance.publishDateTime)
                    new_schedule.save()
                    publish_task.clocked = new_schedule
                
                publish_task.enabled = True
                publish_task.one_off = True
                publish_task.save()

        instance.save()
        return instance
Exemple #3
0
 def create_publish_task(self):
     clocked = ClockedSchedule(clocked_time=self.publish_date)
     clocked.save()
     publish_task = PeriodicTask(
         clocked=clocked,
         name=f'Publish confession with id:{self.pk}',
         task='publish_confession_task',
         kwargs={"instance_id": self.pk},
         one_off=True)
     publish_task.save()
Exemple #4
0
 def test_save_raises_for_multiple_schedules(self):
     schedules = [('crontab', CrontabSchedule()),
                  ('interval', IntervalSchedule()),
                  ('solar', SolarSchedule()),
                  ('clocked', ClockedSchedule())]
     for i, options in enumerate(combinations(schedules, 2)):
         with self.assertRaises(ValidationError):
             PeriodicTask(name='task{}'.format(i), **dict(options)).save()
Exemple #5
0
 def test_validate_unique_raises_for_multiple_schedules(self):
     schedules = [('crontab', CrontabSchedule()),
                  ('interval', IntervalSchedule()),
                  ('solar', SolarSchedule()),
                  ('clocked', ClockedSchedule())]
     for options in combinations(schedules, 2):
         with self.assertRaises(ValidationError):
             PeriodicTask(**dict(options)).validate_unique()
Exemple #6
0
def create_tweet_task(post_id):
    """
    Create a django celery beat PeriodicTask with ClockedSchedule for each post.
    """
    post = Post.objects.get(pk=post_id)
    clocked_schedule = ClockedSchedule(clocked_time=post.publishDateTime)
    clocked_schedule.save()

    task_data = dict(name="PublishTweet{}".format(str(post_id)),
                     task="socials.tasks.publish_tweet_job",
                     clocked=clocked_schedule,
                     kwargs=json.dumps({"post_id": str(post_id)}))
    periodic_task = PeriodicTask(**task_data)
    periodic_task.enabled = True
    periodic_task.one_off = True
    periodic_task.save()
    post.publishTask = periodic_task
    post.save()
 def test_validate_unique_raises_for_multiple_schedules(self):
     schedules = [('crontab', CrontabSchedule()),
                  ('interval', IntervalSchedule()),
                  ('solar', SolarSchedule()),
                  ('clocked', ClockedSchedule())]
     expected_error_msg = (
         'Only one of clocked, interval, crontab, or solar '
         'must be set')
     for i, options in enumerate(combinations(schedules, 2)):
         name = 'task{}'.format(i)
         options_dict = dict(options)
         with self.assertRaises(ValidationError) as cm:
             PeriodicTask(name=name, **options_dict).validate_unique()
         errors = cm.exception.args[0]
         self.assertEqual(errors.keys(), options_dict.keys())
         for error_msg in errors.values():
             self.assertEqual(error_msg, [expected_error_msg])
Exemple #8
0
 def create_model_clocked(self, schedule, **kwargs):
     clocked = ClockedSchedule.from_schedule(schedule)
     clocked.save()
     return self.create_model(clocked=clocked, one_off=True, **kwargs)
Exemple #9
0
 def test_validate_unique_not_raises(self):
     PeriodicTask(crontab=CrontabSchedule()).validate_unique()
     PeriodicTask(interval=IntervalSchedule()).validate_unique()
     PeriodicTask(solar=SolarSchedule()).validate_unique()
     PeriodicTask(clocked=ClockedSchedule(), one_off=True).validate_unique()