def should_run_now(self, force=False):
        from django_cron.models import CronJobLog
        cron_job = self.cron_job
        """
        Returns a boolean determining whether this cron should run now or not!
        """

        self.user_time = None
        self.previously_ran_successful_cron = None

        # If we pass --force options, we force cron run
        if force:
            return True
        if cron_job.schedule.run_every_mins is not None:

            # We check last job - success or not
            last_job = None
            try:
                last_job = CronJobLog.objects.filter(code=cron_job.code).latest('start_time')
            except CronJobLog.DoesNotExist:
                pass
            if last_job:
                if not last_job.is_success and cron_job.schedule.retry_after_failure_mins:
                    if get_current_time() > last_job.start_time + timedelta(minutes=cron_job.schedule.retry_after_failure_mins):
                        return True
                    else:
                        return False

            try:
                self.previously_ran_successful_cron = CronJobLog.objects.filter(
                    code=cron_job.code,
                    is_success=True,
                    ran_at_time__isnull=True
                ).latest('start_time')
            except CronJobLog.DoesNotExist:
                pass

            if self.previously_ran_successful_cron:
                if get_current_time() > self.previously_ran_successful_cron.start_time + timedelta(minutes=cron_job.schedule.run_every_mins):
                    return True
            else:
                return True

        if cron_job.schedule.run_at_times:
            for time_data in cron_job.schedule.run_at_times:
                user_time = time.strptime(time_data, "%H:%M")
                now = get_current_time()
                actual_time = time.strptime("%s:%s" % (now.hour, now.minute), "%H:%M")
                if actual_time >= user_time:
                    qset = CronJobLog.objects.filter(
                        code=cron_job.code,
                        ran_at_time=time_data,
                        is_success=True
                    ).filter(
                        Q(start_time__gt=now) | Q(end_time__gte=now.replace(hour=0, minute=0, second=0, microsecond=0))
                    )
                    if not qset:
                        self.user_time = time_data
                        return True

        return False
def should_run_now ( self , force = False ) : from django_cron . models import CronJobLog cron_job = self . cron_job self . user_time = None self . previously_ran_successful_cron = None if force : return True if cron_job . schedule . run_every_mins is not None : last_job = None try : last_job = CronJobLog . objects . filter ( code = cron_job . code ) . latest ( 'start_time' ) except CronJobLog . DoesNotExist : pass if last_job : if not last_job . is_success and cron_job . schedule . retry_after_failure_mins : if get_current_time ( ) > last_job . start_time + timedelta ( minutes = cron_job . schedule . retry_after_failure_mins ) : return True else : return False try : self . previously_ran_successful_cron = CronJobLog . objects . filter ( code = cron_job . code , is_success = True , ran_at_time__isnull = True ) . latest ( 'start_time' ) except CronJobLog . DoesNotExist : pass if self . previously_ran_successful_cron : if get_current_time ( ) > self . previously_ran_successful_cron . start_time + timedelta ( minutes = cron_job . schedule . run_every_mins ) : return True else : return True if cron_job . schedule . run_at_times : for time_data in cron_job . schedule . run_at_times : user_time = time . strptime ( time_data , "%H:%M" ) now = get_current_time ( ) actual_time = time . strptime ( "%s:%s" % ( now . hour , now . minute ) , "%H:%M" ) if actual_time >= user_time : qset = CronJobLog . objects . filter ( code = cron_job . code , ran_at_time = time_data , is_success = True ) . filter ( Q ( start_time__gt = now ) | Q ( end_time__gte = now . replace ( hour = 0 , minute = 0 , second = 0 , microsecond = 0 ) ) ) if not qset : self . user_time = time_data return True return False
Returns a boolean determining whether this cron should run now or not!