def handle(self, *args, **options):
        # Parse the days argument as an integer.
        if options['interval_choice']:
            if options['publish_id'] or options['publish_name'] :
                raise Exception("Three options cannot be used together.")
            else:
                interval = JobInterval.get_interval(options['interval_choice'])
                JobStatemachine.create_jobs(interval)
        elif options['publish_id']:
            if options['publish_name']:
                raise Exception("Three options cannot be used together.")
            else:
                JobStatemachine.create_job(options['publish_id'],Manually.instance())
        elif options['publish_name']:
            JobStatemachine.create_job_by_name(options['publish_name'],Manually.instance())
        else:
            raise Exception("No option is specified")

        return 0
    def handle(self, *args, **options):
        # Parse the days argument as an integer.
        if options['interval_choice']:
            if options['publish_id'] or options['publish_name']:
                raise Exception("Three options cannot be used together.")
            else:
                interval = JobInterval.get_interval(options['interval_choice'])
                JobStatemachine.create_jobs(interval)
        elif options['publish_id']:
            if options['publish_name']:
                raise Exception("Three options cannot be used together.")
            else:
                JobStatemachine.create_job(options['publish_id'],
                                           Manually.instance())
        elif options['publish_name']:
            JobStatemachine.create_job_by_name(options['publish_name'],
                                               Manually.instance())
        else:
            raise Exception("No option is specified")

        return 0
from django.core.management.base import BaseCommand
from optparse import make_option

from borg_utils.jobintervals import JobInterval,Manually
from harvest.jobstatemachine import JobStatemachine

JobInterval._initialize()

class Command(BaseCommand):
    help = 'Create harvest job'
    option_list = BaseCommand.option_list + (
        make_option(
            '-i',
            '--interval',
            action='store',
            dest='interval_choice',
            help='Create harvest job for all the Publish objects marked with this interval (valid args: {})'.format(', '.join(JobInterval._interval_dict.keys()))
        ),
        make_option(
            '-p',
            '--publish-id',
            action='store',
            dest='publish_id',
            help='Create harvest job for the one Publish object with specified id'
        ),
        make_option(
            '-n',
            '--publish-name',
            action='store',
            dest='publish_name',
            help='Create harvest job for the one Publish object with specified name'
Example #4
0
    def handle(self, *args, **options):
        jobs = []

        #add all create jobs
        if options["create_job"]:
            for interval in JobInterval.publish_intervals():
                if interval in [
                        JobInterval.Manually, JobInterval.Realtime,
                        JobInterval.Triggered
                ]:
                    continue
                jobs.append(CreateJob(interval))

        #add run jobs;
        if options["run_job"]:
            jobs.append(HarvestJob(JobInterval.Minutely))

        #check datasource
        if options["check_ds"]:
            jobs.append(CheckDsJob(JobInterval.Hourly))

        #clean outdated jobs
        if options["clean_job"] or options["clean_job_now"]:
            if options['expire_days']:
                try:
                    options['expire_days'] = int(options['expire_days'])
                    if options['expire_days'] <= 0:
                        options['expire_days'] = 90
                except:
                    options['expire_days'] = 90
            else:
                options['expire_days'] = 90

            if options['min_jobs']:
                try:
                    options['min_jobs'] = int(options['min_jobs'])
                    if options['min_jobs'] <= 0:
                        options['min_jobs'] = 1
                except:
                    options['min_jobs'] = 1
            else:
                options['min_jobs'] = 1

            if options["clean_job_now"]:
                HarvestJobCleaner(options["expire_days"],
                                  options["min_jobs"]).clean()

            if options["clean_job"]:
                jobs.append(CleanJob(JobInterval.Daily, options))

        if not jobs:
            #no repeated jobs to run
            return

        #begin to run jobs
        next_run_time = None
        min_next_run_time = None
        now = None
        while (True):
            min_next_run_time = None
            now = timezone.now()
            RepeatedJob.job_batch_id = JobInterval.Daily.job_batch_id(now)
            for job in jobs:
                next_run_time = job.run(now)
                if min_next_run_time:
                    min_next_run_time = next_run_time if next_run_time < min_next_run_time else min_next_run_time
                else:
                    min_next_run_time = next_run_time

            sleep_times = (min_next_run_time - timezone.now()).total_seconds()
            if sleep_times > 0:
                logger.info("sleep until {}".format(
                    timezone.localtime(min_next_run_time)))
                time.sleep(sleep_times)
from django.core.management.base import BaseCommand
from optparse import make_option

from borg_utils.jobintervals import JobInterval, Manually
from harvest.jobstatemachine import JobStatemachine

JobInterval._initialize()


class Command(BaseCommand):
    help = 'Create harvest job'
    option_list = BaseCommand.option_list + (
        make_option(
            '-i',
            '--interval',
            action='store',
            dest='interval_choice',
            help=
            'Create harvest job for all the Publish objects marked with this interval (valid args: {})'
            .format(', '.join(JobInterval._interval_dict.keys()))),
        make_option(
            '-p',
            '--publish-id',
            action='store',
            dest='publish_id',
            help=
            'Create harvest job for the one Publish object with specified id'),
        make_option(
            '-n',
            '--publish-name',
            action='store',
    def handle(self, *args, **options):
        jobs = []

        #add all create jobs
        if options["create_job"]:
            for interval in JobInterval.publish_intervals():
                if interval in [JobInterval.Manually, JobInterval.Realtime, JobInterval.Triggered]:
                    continue
                jobs.append(CreateJob(interval))

        #add run jobs;
        if options["run_job"]:
            jobs.append(HarvestJob(JobInterval.Minutely))

        #check datasource
        if options["check_ds"]:
            jobs.append(CheckDsJob(JobInterval.Hourly))

        #clean outdated jobs
        if options["clean_job"] or options["clean_job_now"]:
            if options['expire_days']:
                try:
                    options['expire_days'] = int(options['expire_days'])
                    if options['expire_days'] <= 0:
                        options['expire_days'] = 90
                except:
                    options['expire_days'] = 90
            else:
                options['expire_days'] = 90

            if options['min_jobs']:
                try:
                    options['min_jobs'] = int(options['min_jobs'])
                    if options['min_jobs'] <= 0:
                        options['min_jobs'] = 1
                except:
                    options['min_jobs'] = 1
            else:
                options['min_jobs'] = 1

            if options["clean_job_now"]:
                HarvestJobCleaner(options["expire_days"],options["min_jobs"]).clean()

            if options["clean_job"]:
                jobs.append(CleanJob(JobInterval.Daily,options))

        if not jobs:
            #no repeated jobs to run
            return

        #begin to run jobs
        next_run_time = None
        min_next_run_time = None
        now = None
        while(True):
            min_next_run_time = None
            now = timezone.now()
            RepeatedJob.job_batch_id = JobInterval.Daily.job_batch_id(now)
            for job in jobs:
                next_run_time = job.run(now)
                if min_next_run_time:
                    min_next_run_time = next_run_time if next_run_time < min_next_run_time else min_next_run_time
                else:
                    min_next_run_time = next_run_time

            sleep_times = (min_next_run_time - timezone.now()).total_seconds()
            if sleep_times > 0:
                logger.info("sleep until {}".format(timezone.localtime(min_next_run_time)))
                time.sleep(sleep_times)