Ejemplo n.º 1
0
def createCrontab(branch):
    ctab = CronTab()
    if branch.cron_enabled and branch.cron_type == 'h':
        if branch.cron_interval > 1:
            interval_list = [str(x) for x in range(0, 23, branch.cron_interval)]
            interval_str = ','.join(interval_list)
        else:
            interval_str = '*'
        cronline = "%s %s * * * %s %s %s >/var/sftmp/config_cronjob.out 2>&1" % (
            branch.cron_start, interval_str, os.path.join(settings.BASE_DIR, 'config_cronjob.sh'), branch.repo.name,
            branch.name)
        logger.debug('Creating cron tab with line ' + cronline)
        item = CronItem(line=cronline + ' #' + (CRON_COMMENT + ' %d' % branch.id))
        ctab.add(item)
        ctab.write()
    if branch.code_cron_enabled and branch.code_cron_type == 'h':
        if branch.code_cron_interval > 1:
            interval_list = [str(x) for x in range(0, 23, branch.code_cron_interval)]
            interval_str = ','.join(interval_list)
        else:
            interval_str = '*'
        cronline = "%s %s * * * %s %s %s >/var/sftmp/code_cronjob.out 2>&1" % (
            branch.code_cron_start, interval_str, os.path.join(settings.BASE_DIR, 'code_cronjob.sh'), branch.repo.name,
            branch.name)
        logger.debug('Creating cron tab with line ' + cronline)
        item = CronItem(line=cronline + ' #' + (CRON_COMMENT + ' %d' % branch.id))
        ctab.add(item)
        ctab.write()
Ejemplo n.º 2
0
def is_crontab_valid(form, field):
    item = CronItem(field.data + ' /bin/echo # Just a test', cron=CronTab())
    # May be I will refactor to this:
    #item = cron.new(command='/bin/echo', comment='Aaaaa')
    #item.hour.every(4)
    #item.minute.during(5,50).every(2)
    #item.day.on(4,5,6)
    #item.dow.on(1)
    #item.month.during(1,2)
    if not item.is_valid():
        raise ValidationError(gettext('%(job)s is not a correct crontab entry.',
                              job=field.data))
Ejemplo n.º 3
0
        async def schedule(self, ctx):
            await ctx.send(f"Schedule?")

            def valid_cron(message):
                return CronSlices.is_valid(message.content) and message.author.id == ctx.author.id

            def valid_msg(message):
                return message.author.id == ctx.author.id

            msg = await self.bot.wait_for("message", check=valid_cron)
            schedule = msg.content

            await ctx.send("Message?")
            msg = await self.bot.wait_for("message", check=valid_msg)
            await msg.add_reaction("✅")

            job_id = uuid.uuid4()
            job_dir = self.config.task_base / str(job_id)
            job_dir.mkdir(parents=True, exist_ok=True)
            with open(job_dir / 'content.txt', 'w') as f:
                f.write(msg.content)

            for attachment in msg.attachments:
                with open(job_dir / attachment.filename, 'wb') as f:
                    await attachment.save(f)
            files = " ".join(f'"{(job_dir / attachment.filename).resolve()}"' for attachment in msg.attachments)

            with CronTab(user=True) as cron:
                line = f'{schedule} {self.config.send} "{ctx.message.channel.id}" "{(job_dir / "content.txt").resolve()}" {files}'
                job = CronItem.from_line(line, cron=cron)
                cron.append(job)
Ejemplo n.º 4
0
def add_job(job):
    root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    command = '. /etc/profile; cd {}; export PYTHONPATH=$PYTHONPATH:{}; python3 app/job_runner.py --job_name "{}" >> /tmp/schedule_jobs.log 2>&1'.format(
        root_path, root_path, job['name'])
    job_string = job_to_string(job=job, command=command)
    cron = CronTab(user=USERNAME)
    item = CronItem.from_line(job_string, cron=cron)
    cron.append(item)
    cron.write()
Ejemplo n.º 5
0
    def __call__(self, form, field, message=None):
        schedule = (field.data or '').strip()
        if not schedule:
            return

        try:
            CronItem().setall(schedule)
        except (KeyError, ValueError):
            message = message or self.message or field.gettext('Invalid cron')
            raise ValidationError(message)
Ejemplo n.º 6
0
def schedule_cron(command, interval, crontab=None):
    from crontab import CronTab, CronItem
    if not os.path.exists(crontab):
        with open(crontab, 'w'):
            pass

    if crontab:
        c = CronTab(tabfile=crontab)
    else:
        c = CronTab(user=getpass.getuser())

    job = CronItem.from_line(interval + ' ' + command, cron=c)

    c.append(job)
    c.write()
    return c, job
Ejemplo n.º 7
0
def create_crontab(uts):
    ctab = CronTab()
    if uts.cron_type == 'h':
        if uts.cron_interval > 1:
            interval_list = [str(x) for x in range(0, 23, uts.cron_interval)]
            interval_str = ','.join(interval_list)
        else:
            interval_str = '*'
        cronline = "%s %s * * * %s runtests %s %s >/tmp/unitTestCronjob%s.out 2>&1" % (
            uts.cron_start, interval_str,
            os.path.join(settings.BASE_DIR, 'runmanage.sh'),
            uts.branch.repo.name, uts.branch.name, uts.id)
        logger.debug('Creating cron tab with line ' + cronline)
        item = CronItem(line=cronline + ' #' + ('%s %d' %
                                                (CRON_COMMENT, uts.id)))
        ctab.add(item)
        ctab.write()
Ejemplo n.º 8
0
    def schedule(self, job, reports, job_dir, interval):
        from crontab import CronTab, CronItem

        # must occur in gunicorn process
        if self.runner is None:
            self.runner = threading.Thread(target=run_tasks,
                                           args=(self.add_queue,
                                                 self.delete_queue))
            self.runner.daemon = True
            self.runner.start()

        c = CronTab(user=getpass.getuser())
        cjob = CronItem.from_line(interval + ' echo test', cron=c)

        self.tasks[job.id] = (job, reports, job_dir, interval, cjob)
        logging.critical('Scheduling job: %s' % str(job.id))
        self.add_queue.put((job, reports, job_dir, interval, cjob))
Ejemplo n.º 9
0
cfg.stop_required(True)

n = 120
while not cfg.is_stopped():
    sleep(1)
    n = n - 1
    print('s', flush=True)

    if (n < 0):
        print('Flashing failed waiting for Device to stop', flush=True)
        cfg.stop_clear()
        exit()

# Prevent CRON from starting the AudioMoth Sync
ct = CronTab(user=True)
ci = CronItem()

jobs = ct.find_comment("Job0")
for job in jobs:
    job.enable(False)

ct.write_to_user(user=True)

try:
    am = audiomoth()
    am.flash()

    for job in jobs:
        job.enable(True)

    ct.write_to_user(user=True)