Ejemplo n.º 1
0
    def _create_job(publish,
                    job_interval=JobInterval.Manually,
                    job_batch_id=None):
        """
        manually create a job
        return (true,'OK'), if create a job, otherwise return (False,message)
        """
        if not publish.publish_status.publish_enabled:
            #publish is disabled, ignore
            return (False, "Disabled")

        job = None
        job = Job.objects.filter(publish=publish,
                                 state=Waiting.instance().name).first()
        if job > 0:
            #already have one waiting harvest job, create another is meanless.
            return (True, job.pk)

        if not job_batch_id:
            job_batch_id = job_interval.job_batch_id()

        with transaction.atomic():
            if publish.waiting > 0:
                #already have one waiting harvest job, create another is meanless.
                return

            publish.waiting = models.F("waiting") + 1
            job = Job(batch_id=job_batch_id,
                      publish=publish,
                      state=Waiting.instance().name,
                      previous_state=None,
                      message=None,
                      created=timezone.now(),
                      launched=None,
                      finished=None,
                      job_type=job_interval.name)
            publish.save(update_fields=['waiting'])
            job.save()
            #add log
            log = JobLog(
                job_id=job.id,
                state="Create",
                outcome="Create",
                message="Created by custodian" if job_interval
                == JobInterval.Manually else "Created by other application",
                next_state=job.state,
                start_time=timezone.now(),
                end_time=timezone.now())
            log.save()

        return (True, job.id)
    def _create_job(publish,job_interval=JobInterval.Manually,job_batch_id=None):
        """
        manually create a job
        return (true,'OK'), if create a job, otherwise return (False,message)
        """
        if not publish.publish_status.publish_enabled:
            #publish is disabled, ignore
            return (False,"Disabled")

        job = None
        job = Job.objects.filter(publish=publish,state=Waiting.instance().name).first()
        if job > 0:
            #already have one waiting harvest job, create another is meanless.
            return (True,job.pk)

        if not job_batch_id:
            job_batch_id = job_interval.job_batch_id()

        with transaction.atomic():
            if publish.waiting > 0:
                #already have one waiting harvest job, create another is meanless.
                return;

            publish.waiting = models.F("waiting") + 1
            job = Job(
                        batch_id = job_batch_id,
                        publish = publish,
                        state = Waiting.instance().name,
                        previous_state = None,
                        message = None,
                        created = timezone.now(),
                        launched = None,
                        finished = None,
                        job_type = job_interval.name
                    )
            publish.save(update_fields=['waiting'])
            job.save()
            #add log
            log = JobLog(
                        job_id = job.id,
                        state = "Create",
                        outcome = "Create",
                        message = "Created by custodian" if job_interval == JobInterval.Manually else "Created by other application",
                        next_state = job.state,
                        start_time = timezone.now(),
                        end_time = timezone.now())
            log.save()

        return (True,job.id)
Ejemplo n.º 3
0
    def create_jobs(interval_choice):
        """
        create the jobs based on publish status
        All jobs will be sorted agaist with publish.priority
        """
        job = None
        log = None
        jobs = []
        job_batch_id = interval_choice.job_batch_id

        check_job = Job(
            id=-1, batch_id="CK" +
            job_batch_id) if interval_choice == Realtime.instance() else None
        up_to_date = False

        for p in Publish.objects.filter(interval=interval_choice.name,
                                        waiting=0).order_by('priority'):
            if not p.publish_status.publish_enabled:
                #publish is disabled, ignore
                continue

            if interval_choice == Realtime.instance():
                #Realtime publish, check whether input is up to date.
                up_to_date = True
                for i in p.inputs:
                    if not i.is_up_to_date(check_job, False):
                        #input is not up to date, clear importing info to avoid double check for foreign table.
                        up_to_date = False
                        break
                if up_to_date:
                    continue

            p.waiting = models.F("waiting") + 1
            job = Job(batch_id=job_batch_id,
                      publish=p,
                      state=Waiting.instance().name,
                      previous_state=None,
                      message=None,
                      created=timezone.now(),
                      launched=None,
                      finished=None,
                      job_type=interval_choice.name)

            #add log
            log = JobLog(
                state="Create",
                outcome="Create",
                message="Create by {0} cron job".format(interval_choice),
                next_state=job.state,
                start_time=timezone.now(),
                end_time=timezone.now())

            jobs.append((p, job, log))

        with transaction.atomic():
            for p, job, log in jobs:
                p.save(update_fields=['waiting'])
                job.save()
                log.job_id = job.id
                log.save()
Ejemplo n.º 4
0
    def create_jobs(interval_choice):
        """
        create the jobs based on publish status
        All jobs will be sorted agaist with publish.priority
        """
        job = None
        log = None
        jobs = []
        job_batch_id = interval_choice.job_batch_id

        check_job = Job(id=-1,batch_id="CK" + job_batch_id) if interval_choice == Realtime.instance() else None
        up_to_date = False
        
        for p in Publish.objects.filter(interval = interval_choice.name, waiting = 0).order_by('priority'):
            if not p.publish_status.publish_enabled:
                #publish is disabled, ignore
                continue

            if interval_choice == Realtime.instance():
                #Realtime publish, check whether input is up to date.
                up_to_date = True
                for i in p.inputs:
                    if not i.is_up_to_date(check_job,False):
                        #input is not up to date, clear importing info to avoid double check for foreign table.
                        up_to_date = False
                        break
                if up_to_date:
                    continue

            p.waiting = models.F("waiting") + 1
            job = Job(
                        batch_id = job_batch_id,
                        publish = p,
                        state = Waiting.instance().name,
                        previous_state = None,
                        message = None,
                        created = timezone.now(),
                        launched = None,
                        finished = None,
                        job_type = interval_choice.name
                    )

            #add log
            log = JobLog(
                        state = "Create",
                        outcome = "Create",
                        message = "Create by {0} cron job".format(interval_choice),
                        next_state = job.state,
                        start_time = timezone.now(),
                        end_time = timezone.now())

            jobs.append((p,job,log))

        with transaction.atomic():
            for p,job,log in jobs:
                p.save(update_fields=['waiting'])
                job.save()
                log.job_id = job.id
                log.save()