Example #1
0
    def clean_value(self):
        """
        Slight improvement to UX by helping them enter a valid cron-format value
        """
        data = self.cleaned_data["value"]

        if data:
            # Make sure both pycron & croniter can parse this cron schedule. Each chokes on different
            # formats - ex. croniter does not like "@ @ @ @ @". pycron can't really deal with that
            # either, it just doesn't raise an exception, so check both here.
            try:
                pycron.is_now(data)
            except ValueError as err:
                raise ValidationError(str(err).capitalize())
            try:
                croniter.croniter(data, datetime.datetime.now())
            except (ValueError, KeyError) as err:
                raise ValidationError(str(err).capitalize())

            # Also make sure it's only scheduled to run on the hour, to match Recurring Job run-time.
            # Apparently it's valid to make the 1st piece multiple 0s (e.g., 00), so take that into account
            minute = data.split(" ")[0]
            if not int(minute) == 0:
                raise ValidationError(
                    "Only schedules with a 0 minute are valid, since the Recurring Job only "
                    "runs on the hour")

        return data
Example #2
0
 def run(now):
     for i in range(0, 7):
         # Test day matching from Sunday onwards...
         now += timedelta(days=1)
         assert pycron.is_now('* * * * %s' % (pycron.DAY_NAMES[i]), now)
         assert pycron.is_now('* * * * %s' % (pycron.DAY_ABBRS[i]), now)
         # Test weekdays
         assert pycron.is_now('* * * * mon,tue,wed,thu,fri',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now(
             '* * * * monday,tuesday,wednesday,thursday,friday',
             now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * mon-fri',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * monday-friday',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * mon,tue,wed,thu-fri',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now(
             '* * * * monday,tuesday,wednesday,thursday-friday',
             now) is (True if i not in [0, 6] else False)
         # Test weekends
         assert pycron.is_now('* * * * sun,sat',
                              now) is (True if i in [0, 6] else False)
         assert pycron.is_now('* * * * sunday,saturday',
                              now) is (True if i in [0, 6] else False)
Example #3
0
 def __check_cron(self, text):
     res = False
     try:
         pycron.is_now(text)
         res = True
     except ValueError:
         pass
     return res
Example #4
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('* * 18 * *', now)
     assert pycron.is_now('* * */6 * *', now)
     assert pycron.is_now('* * 1,16,18 * *', now)
     assert pycron.is_now('* * 19 * *', now) is False
     assert pycron.is_now('* * */4 * *', now) is False
     assert pycron.is_now('* * 1,16 * *', now) is False
     assert pycron.is_now('* * 1,16 * *', now) is False
     assert pycron.is_now('* * 1-20 * *', now)
     assert pycron.is_now('* * 20-31 * *', now) is False
Example #5
0
 def run(now):
     for i in range(0, 7):
         # Test day matching from Sunday onwards...
         now += timedelta(days=1)
         assert pycron.is_now('* * * * %i' % (i), now)
         # Test weekdays
         assert pycron.is_now('* * * * 1,2,3,4,5', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * 1-5', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * 1,2,3,4-5', now) is (True if i not in [0, 6] else False)
         # Test weekends
         assert pycron.is_now('* * * * 0,6', now) is (True if i in [0, 6] else False)
Example #6
0
 def run(now):
     for i in range(0, 7):
         # Test day matching from Sunday onwards...
         now += timedelta(days=1)
         assert pycron.is_now('* * * * %i' % (i), now)
         # Test weekdays
         assert pycron.is_now('* * * * 1,2,3,4,5', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * 1-5', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * 1,2,3,4-5', now) is (True if i not in [0, 6] else False)
         # Test weekends
         assert pycron.is_now('* * * * 0,6', now) is (True if i in [0, 6] else False)
Example #7
0
 def run(now):
     assert pycron.is_now('* * * * * *', now)
     assert pycron.is_now('* * * * * 169', now)
     assert pycron.is_now('* * * * * 1-365/2', now)
     assert pycron.is_now('* * * * * 168,169,170', now)
     assert pycron.is_now('* * * * * 170', now) is False
     assert pycron.is_now('* * * * * */2', now) is False
     assert pycron.is_now('* * * * * 165,166,167', now) is False
     assert pycron.is_now('* * * * 0-170', now)
     assert pycron.is_now('* * * * 170-365', now) is False
Example #8
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('* * * 6 *', now)
     assert pycron.is_now('* * * */2 *', now)
     assert pycron.is_now('* * * 1,4,6,12 *', now)
     assert pycron.is_now('* * * 5 *', now) is False
     assert pycron.is_now('* * * */5 *', now) is False
     assert pycron.is_now('* * * 1,4,12 *', now) is False
     assert pycron.MONTH_CHOICES[now.month - 1][1] == 'June'
     assert pycron.is_now('* * * 5-8 *', now)
     assert pycron.is_now('* * * 8-10 *', now) is False
Example #9
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('* * * * 4', now)
     assert pycron.is_now('* * * * */4', now)
     assert pycron.is_now('* * * * 0,3,4', now)
     assert pycron.is_now('* * * * 3', now) is False
     assert pycron.is_now('* * * * */3', now) is False
     assert pycron.is_now('* * * * 0,3,6', now) is False
     assert pycron.DOW_CHOICES[now.isoweekday()][1] == 'thursday'
     assert pycron.DOW_CHOICES[0][1] == 'sunday'
     assert pycron.is_now('* * * * 0-4', now)
     assert pycron.is_now('* * * * 5-6', now) is False
Example #10
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('* * * * 4', now)
     assert pycron.is_now('* * * * */4', now)
     assert pycron.is_now('* * * * 0,3,4', now)
     assert pycron.is_now('* * * * 3', now) is False
     assert pycron.is_now('* * * * */3', now) is False
     assert pycron.is_now('* * * * 0,3,6', now) is False
     assert pycron.DOW_CHOICES[now.isoweekday()][1] == 'thursday'
     assert pycron.DOW_CHOICES[0][1] == 'sunday'
     assert pycron.is_now('* * * * 0-4', now)
     assert pycron.is_now('* * * * 5-6', now) is False
Example #11
0
    def run(self, timenow=None):
        if timenow is None:
            timenow = datetime.now(pytz.utc)

        logger.info("Running tasks at %s", timenow)
        for task in self.tasks:
            logger.info("* Task: %s" % task.name)
            schedule = task.task_params['schedule']
            timenow_local = timenow.astimezone(task.task_params['timezone'])

            # Check schedule
            if not is_now(schedule, timenow_local):
                logger.info("- Skipped due to schedule")
                continue

            # Run the request
            logger.info("*" * 50)
            logger.info("Tasks started: %s", timenow)

            r = request(**task.request_params)
            logger.info(r.url)
            logger.info(r.status_code)

            logger.debug('\n'.join(html2text(r.text).split('\n')[:50]))
            logger.info("+" * 50)

            # Rate limit sleep
            sleep(self.rate_limit)
Example #12
0
def main(logger, args, settings):
    write_running_log("main", logger)
    write_log("The promotion_sync will be executed daily",
              logger,
              True,
              False,
              print_color="green")
    aws_access_key_id = settings.get("amazon", "aws_access_key_id")
    aws_secret_access_key = settings.get("amazon", "aws_secret_access_key")
    aws_associate_tag = settings.get("amazon", "aws_associate_tag")
    aws_region = settings.get("amazon", "aws_region")
    df_node_file = settings.get("amazon", "df_node_file")
    output_folder = settings.get("amazon", "output_folder")
    tmp_folder = settings.get("amazon", "tmp_folder")
    aws_qps = settings.getfloat("amazon", "aws_qps")

    if args.force == "True":
        amazon = Amazon(logger, aws_access_key_id, aws_secret_access_key,
                        aws_associate_tag, aws_region, aws_qps, df_node_file,
                        output_folder, tmp_folder)
        amazon.get_daily_promotions()
    while True:
        if pycron.is_now('0 10 * * *'):
            amazon = Amazon(logger, aws_access_key_id, aws_secret_access_key,
                            aws_associate_tag, aws_region, aws_qps,
                            df_node_file, output_folder, tmp_folder)
            amazon.get_daily_promotions()
            time.sleep(60)
        else:
            time.sleep(60)
Example #13
0
    def _attr_filter(self, data, strategy: Strategy, wxcampaign: WXCampaign):
        if not strategy:
            return False
        # Check worktime
        if strategy.worktime and (not pycron.is_now(strategy.worktime)):
            return False

        # Check according to the attr of data
        for record in strategy.records:
            key = data[record.key]
            val = _cast_to(record.value, record.type)
            if record.op == OP_EQ:
                if not (key == val):
                    return False
            elif record.op == OP_NEQ:
                if not (key != val):
                    return False
            elif record.op == OP_GT:
                if not (key > val):
                    return False
            elif record.op == OP_EGT:
                if not (key >= val):
                    return False
            elif record.op == OP_LT:
                if not (key < val):
                    return False
            elif record.op == OP_ELT:
                if not (key <= val):
                    return False
            else:
                logger.error('Invalid op of filter[%d]' % record.id)
                return False
        return True
Example #14
0
 def check(self):
     while self.main_thread_running.is_set():
         if self.system_ready.is_set():
             super().check()
             try:
                 if CRON_ENABLED:
                     if pycron.is_now(self.schedule):
                         self.trigger_active.set()
                         super().trigger()
                     else:
                         self.trigger_active.clear()
                 else:
                     Logger.log(
                         LOG_LEVEL["error"],
                         "Error pycron not found."
                     )
             except:
                 Logger.log(
                     LOG_LEVEL["error"],
                     "Error evaluating time trigger schedule."
                 )
             time.sleep(self.trigger_interval)
         else:
             time.sleep(2)
     return
Example #15
0
def schedule_gifs():
    print("Inside scheduler")
    while True:
        if pycron.is_now('* * * * *'):  # True Every Sunday at 02:00
            print('running yoga gif')
            payload = {'gif_name': 'yoga'}
            requests.get("http://localhost:3978/api/notify", params=payload)
            time.sleep(60)  # The process should take at least 60 sec
            # to avoid running twice in one minute
        elif pycron.is_now('45 15 * * *'):  # True Every Sunday at 02:00
            print('running water gif')
            payload = {'gif_name': 'water'}
            requests.get("http://localhost:3978/api/notify", params=payload)
            time.sleep(60)
        else:
            time.sleep(60)  # Check again in 15 seconds
Example #16
0
    def _should_schedule_trigger(self, database_client: DatabaseClient,
                                 schedule: dict,
                                 now: datetime.datetime) -> bool:
        """ Check if a new run should be triggered based on a timed schedule """

        now = now.replace(second=0)
        if not pycron.is_now(schedule["expression"], now):
            return False

        if schedule["last_run"] is None:
            return True

        last_run = self._run_provider.get(database_client, schedule["project"],
                                          schedule["last_run"])
        if last_run is None:
            return True

        if last_run["status"] in ["pending", "running"]:
            return False

        last_trigger_date = self._date_time_provider.deserialize(
            last_run["creation_date"]).replace(second=0)
        if last_trigger_date == now:
            return False

        return True
Example #17
0
 def __scheduler_greenlet(self, main_uid, current_time, task):
     value = task.get("value", None)
     current_type = task.get("type", None)
     file_filepath = task.get("path")
     params = task.get("params", dict())
     uid = str(uuid.uuid4())
     running = self.__jobs.is_running(uid, file_filepath, current_type)
     if (current_type == t_scheduler and value is not None
             and pycron.is_now(value, current_time) and not running):
         self.__logger.info({
             "main_id": str(main_uid),
             "id": uid,
             "type": t_scheduler,
             "status": t_start,
             "filepath": file_filepath,
         })
         self.__jobs.update(uid, file_filepath, t_scheduler, value, params,
                            t_start)
         res = self.__exec_job(uid, task)
         if res.get("error"):
             self.__logger.error({
                 "main_id": str(main_uid),
                 "id": uid,
                 "type": t_scheduler,
                 "status": t_error,
                 "filepath": file_filepath,
                 "duration": res.get("duration"),
                 "error": str(res.get("error")),
             })
             self.__jobs.update(
                 uid,
                 file_filepath,
                 t_scheduler,
                 value,
                 params,
                 t_error,
                 res.get("duration"),
             )
             return
         self.__logger.info({
             "main_id": str(main_uid),
             "id": uid,
             "type": t_scheduler,
             "status": t_health,
             "filepath": file_filepath,
             "duration": res.get("duration"),
         })
         self.__jobs.update(
             uid,
             file_filepath,
             t_scheduler,
             value,
             params,
             t_health,
             res.get("duration"),
         )
Example #18
0
    def run_cron(self) -> None:
        should_run = pycron.is_now(config.CRON)
        if not self.ran_cron and should_run:
            self.ran_cron = True
            result = self.goban.play_move()
            if result:
                self.slack_client.rtm_send_message(config.CHANNEL, result)
                self.save_goban()

        elif not should_run:
            self.ran_cron = False
Example #19
0
def validate_cron(cron_str):
    valid_chars = '-/,*0123456789 '
    e = t('Невалидная нотация. Строка должна содержать только знаки {0}'
          ', и четыре одинарных пробела. Удобно настроить и скопировать строку'
          ' можно здесь{1}')
    e = e.format(valid_chars, ' - https://crontab.guru/')
    try:
        pycron.is_now(cron_str)
        cron_descriptor.get_description(cron_str)
    except Exception:
        raise ValidationError(e)
    spaces_counter = 0
    for char in cron_str:
        if char == ' ':
            spaces_counter += 1
        if char not in valid_chars:
            raise ValidationError(e)
    if spaces_counter != 4:
        raise ValidationError(e)
    return cron_str
 def check_time_rules(self):
     """
     Controls every minute if a change in frequency and probability
     is to be made based on the specified rules
     :return:
     """
     while True:
         for item in self.time_rules:
             if is_now(item["rule"]):
                 self.prob = item['probability']
                 self.freq = item['frequency']
                 break
         time.sleep(60)
Example #21
0
async def parse_crontab(opsdroid):
    """Parse all crontab skills against the current time."""
    while opsdroid.eventloop.is_running():
        await asyncio.sleep(60 - arrow.now().time().second)
        _LOGGER.debug(_("Running crontab skills"))
        for skill in opsdroid.skills:
            if "crontab" in skill:
                if skill["timezone"] is not None:
                    timezone = skill["timezone"]
                else:
                    timezone = opsdroid.config.get("timezone", "UTC")
                if pycron.is_now(skill["crontab"], arrow.now(tz=timezone)):
                    await opsdroid.run_skill(skill["skill"], skill["config"],
                                             None)
Example #22
0
async def parse_crontab(opsdroid):
    """Parse all crontab skills against the current time."""
    while opsdroid.eventloop.is_running():
        await asyncio.sleep(60 - arrow.now().time().second)
        _LOGGER.debug(_("Running crontab skills at %s."), time.asctime())
        for skill in opsdroid.skills:
            for matcher in skill.matchers:
                if "crontab" in matcher:
                    if matcher["timezone"] is not None:
                        timezone = matcher["timezone"]
                    else:
                        timezone = opsdroid.config.get("timezone", "UTC")
                    if pycron.is_now(matcher["crontab"], arrow.now(tz=timezone)):
                        await opsdroid.run_skill(skill, skill.config, None)
Example #23
0
 def run(now):
     for i in range(0, 7):
         # Test day matching from Sunday onwards...
         now += timedelta(days=1)
         assert pycron.is_now('* * * * %s' % (pycron.DAY_NAMES[i]), now)
         assert pycron.is_now('* * * * %s' % (pycron.DAY_ABBRS[i]), now)
         # Test weekdays
         assert pycron.is_now('* * * * mon,tue,wed,thu,fri',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * monday,tuesday,wednesday,thursday,friday',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now(
             '* * * * mon-fri', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now(
             '* * * * monday-friday', now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * mon,tue,wed,thu-fri',
                              now) is (True if i not in [0, 6] else False)
         assert pycron.is_now('* * * * monday,tuesday,wednesday,thursday-friday',
                              now) is (True if i not in [0, 6] else False)
         # Test weekends
         assert pycron.is_now(
             '* * * * sun,sat', now) is (True if i in [0, 6] else False)
         assert pycron.is_now(
             '* * * * sunday,saturday', now) is (True if i in [0, 6] else False)
Example #24
0
 def check(self):
     """ Check trigger schedule thresholds """
     if self.mudpi.is_running:
         try:
             if pycron.is_now(self.schedule):
                 self.trigger()
                 if not self.active:
                     self.active = True
             else:
                 if self.active:
                     self.active = False
         except Exception as error:
             Logger.log(LOG_LEVEL["error"],
                        "Error evaluating time trigger schedule.")
     return
Example #25
0
    def isActive(self):
        if self.stopTime is not None:
            if time.time() > self.stopTime:
                self.stopTime = None
                print("stop")
                return "stop"
            return False

        for l in self.list:
            if pycron.is_now(l[0]):
                self.stopTime = time.time() + 60 * l[1]
                print("playing, will stop in ", l[1], "minutes")
                return "play"

        return False
Example #26
0
async def run_scheduled_cron_jobs(
    *,
    cron_jobs: list[CronJob],
    handler: JobHandler,
    dt_utc: datetime,
):
    """
    Iterate cron jobs to see if any must be run.
    This should be done in its own task.
    """
    to_run = []
    for job in cron_jobs:
        local_time = dt_utc.astimezone(timezone(job.timezone))
        if pycron.is_now(job.cron_schedule, local_time):
            to_run.append(job)
    await asyncio.gather(*[handler(job) for job in to_run])
    return
 def set_last_time_rule(self):
     """
     When crondate time rules are used, it is found which one should have
     been executed last and the probability and frequency are set
     :return None
     """
     now = datetime.now()
     try:
         for _ in range(0, 525600):
             for item in self.time_rules:
                 if is_now(item["rule"], now):
                     self.prob = item['probability']
                     self.freq = item['frequency']
                     raise Exception('found')
             now = now - timedelta(minutes=1)
     except Exception as inst:
         if inst.args[0] != "found":
             raise Exception(inst)
Example #28
0
async def parse_crontab(opsdroid):
    """Parse all crontab skills against the current time."""
    # pylint: disable=broad-except
    # We want to catch all exceptions coming from a skill module and not
    # halt the application. If a skill throws an exception it just doesn't
    # give a response to the user, so an error response should be given.
    while opsdroid.eventloop.is_running():
        await asyncio.sleep(60 - arrow.now().time().second)
        _LOGGER.debug("Running crontab skills")
        for skill in opsdroid.skills:
            if "crontab" in skill:
                if skill["timezone"] is not None:
                    timezone = skill["timezone"]
                else:
                    timezone = opsdroid.config.get("timezone", "UTC")
                if pycron.is_now(skill["crontab"], arrow.now(tz=timezone)):
                    try:
                        await skill["skill"](opsdroid, skill["config"], None)
                    except Exception:
                        _LOGGER.exception(
                            "Exception when executing cron skill.")
Example #29
0
def main():
    api = idigbio.json(
        env="prod",
        user=os.environ.get("IDB_API_UUID"),
        password=os.environ.get("IDB_API_KEY")
    )

    for url in HARVEST_LIST_URLS:
        try:
            r = requests.get(url)
            r.raise_for_status()

            header = None
            for line in csv.reader(StringIO(r.text)):
                if header is None:
                    header = line
                else:
                    line_dict = dict(zip(header,line))

                    mime = "text/csv"
                    if line_dict["archive_format"] == "DwCA":
                        mime = "application/zip"

                    cron_line = line_dict["cron"]
                    if PUSH_ALL or (len(cron_line) > 0 and pycron.is_now(cron_line)):
                        logger.info(line_dict)
                        try:
                            api_r = api.addurl(line_dict["archive_url"], media_type="guoda", mime_type=mime)
                            logger.info(api_r)
                        except:
                            print api_r.content
                            logger.exception("Error Pushing URL")
                        
                    else:
                        logger.debug("Skipping %s", line_dict["name"])

        except:
            logger.exception("Error Fetching %s", url)
Example #30
0
    def run(now):
        assert pycron.is_now('* * * * *', now)
        assert pycron.is_now('* * * * thu', now)
        assert pycron.is_now('* * * * thursday', now)
        assert pycron.is_now('* * * * */thu', now)
        assert pycron.is_now('* * * * */thursday', now)
        assert pycron.is_now('* * * * sun,wed,thu', now)
        assert pycron.is_now('* * * * sunday,wednesday,thursday', now)
        assert pycron.is_now('* * * * wed', now) is False
        assert pycron.is_now('* * * * wednesday', now) is False
        assert pycron.is_now('* * * * */wed', now) is False
        assert pycron.is_now('* * * * */wednesday', now) is False
        assert pycron.is_now('* * * * sun,wed,sat', now) is False
        assert pycron.is_now('* * * * sunday,wednesday,saturday', now) is False
        assert pycron.DOW_CHOICES[now.isoweekday()][1] == 'thursday'
        assert pycron.DOW_CHOICES[0][1] == 'sunday'
        assert pycron.is_now('* * * * sun-thu', now)
        assert pycron.is_now('* * * * sunday-thursday', now)
        assert pycron.is_now('* * * * fri-sat', now) is False
        assert pycron.is_now('* * * * friday-saturday', now) is False
        # Special cases, where the day names are more or less incorrectly set...
        assert pycron.is_now('* * * * thu-sun', now)
        assert pycron.is_now('* * * * thursday-sunday', now)
        assert pycron.is_now('* * * * wed-sun', now)
        assert pycron.is_now('* * * * wednesday-sunday', now)
        assert pycron.is_now('* * * * wed-mon', now)
        assert pycron.is_now('* * * * wednesday-monday', now)
        assert pycron.is_now('* * * * fri-sun', now) is False
        assert pycron.is_now('* * * * friday-sunday', now) is False
        assert pycron.is_now('* * * * fri-wed', now) is False
        assert pycron.is_now('* * * * friday-wednesday', now) is False

        # Test day matching for dividers
        assert pycron.is_now('* * * * monday-sunday/3', now) is False
        assert pycron.is_now('* * * * mon-sun/3', now) is False
        assert pycron.is_now('* * * * tuesday-sunday/2', now) is False
        assert pycron.is_now('* * * * tue-sun/2', now) is False
Example #31
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('59 * * * *', now)
     assert pycron.is_now('*/1 * * * *', now)
     # Issue 14
     assert pycron.is_now('1-59/2 * * * *', now) is True
Example #32
0
def test_no_dt():
    assert pycron.is_now('* * * * *')
Example #33
0
def test_minute_ranges():
    for i in range(1, 59, 2):
        now = datetime(2015, 6, 18, 0, i)
        assert pycron.is_now('1-59/2 * * * *', now)
        assert pycron.is_now('1-59/2 * * * *', now.replace(tzinfo=utc))
        assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now))
        assert pycron.is_now('1-59/2 * * * *', arrow.get(now))
        assert pycron.is_now('1-59/2 * * * *', udatetime.from_string(now.isoformat()))
        assert pycron.is_now('1-59/2 * * * *', Delorean(datetime=now, timezone='UTC').datetime)

    for i in range(0, 59, 2):
        now = datetime(2015, 6, 18, 0, i)
        assert pycron.is_now('1-59/2 * * * *', now) is False
        assert pycron.is_now('1-59/2 * * * *', now.replace(tzinfo=utc)) is False
        assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now)) is False
        assert pycron.is_now('1-59/2 * * * *', arrow.get(now)) is False
        assert pycron.is_now('1-59/2 * * * *', udatetime.from_string(now.isoformat())) is False
        assert pycron.is_now('1-59/2 * * * *', Delorean(datetime=now, timezone='UTC').datetime) is False
Example #34
0
 def run(now):
     assert pycron.is_now('* * * * *', now)
     assert pycron.is_now('* 16 * * *', now)
     assert pycron.is_now('* */4 * * *', now)
     assert pycron.is_now('*/7 16 * * *', now)
     assert pycron.is_now('*/7 */8 * * *', now)
     assert pycron.is_now('* 2,8,16 * * *', now)
     assert pycron.is_now('* */9 * * *', now) is False
     assert pycron.is_now('* */5 * * *', now) is False
     assert pycron.is_now('*/3 */4 * * *', now) is False
     assert pycron.is_now('3 16 * * *', now) is False
     assert pycron.is_now('*/8 */3 * * *', now) is False
     assert pycron.is_now('* 2,8 * * *', now) is False
     assert pycron.is_now('* 16-20 * * *', now)
     assert pycron.is_now('* 0-10 * * *', now) is False
Example #35
0
def test_minute_ranges():
    for i in range(1, 59, 2):
        now = datetime(2015, 6, 18, 0, i)
        assert pycron.is_now('1-59/2 * * * *', now)
        assert pycron.is_now('1-59/2 * * * *', now.replace(tzinfo=utc))
        assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now))
        assert pycron.is_now('1-59/2 * * * *', arrow.get(now))
        assert pycron.is_now('1-59/2 * * * *',
                             udatetime.from_string(now.isoformat()))
        assert pycron.is_now('1-59/2 * * * *',
                             Delorean(datetime=now, timezone='UTC').datetime)

    for i in range(0, 59, 2):
        now = datetime(2015, 6, 18, 0, i)
        assert pycron.is_now('1-59/2 * * * *', now) is False
        assert pycron.is_now('1-59/2 * * * *',
                             now.replace(tzinfo=utc)) is False
        assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now)) is False
        assert pycron.is_now('1-59/2 * * * *', arrow.get(now)) is False
        assert pycron.is_now('1-59/2 * * * *',
                             udatetime.from_string(now.isoformat())) is False
        assert pycron.is_now('1-59/2 * * * *',
                             Delorean(datetime=now,
                                      timezone='UTC').datetime) is False
Example #36
0
    def run(now):
        assert pycron.is_now('* * * * *', now)
        assert pycron.is_now('* * * * thu', now)
        assert pycron.is_now('* * * * thursday', now)
        assert pycron.is_now('* * * * */thu', now)
        assert pycron.is_now('* * * * */thursday', now)
        assert pycron.is_now('* * * * sun,wed,thu', now)
        assert pycron.is_now('* * * * sunday,wednesday,thursday', now)
        assert pycron.is_now('* * * * wed', now) is False
        assert pycron.is_now('* * * * wednesday', now) is False
        assert pycron.is_now('* * * * */wed', now) is False
        assert pycron.is_now('* * * * */wednesday', now) is False
        assert pycron.is_now('* * * * sun,wed,sat', now) is False
        assert pycron.is_now('* * * * sunday,wednesday,saturday', now) is False
        assert pycron.DOW_CHOICES[now.isoweekday()][1] == 'thursday'
        assert pycron.DOW_CHOICES[0][1] == 'sunday'
        assert pycron.is_now('* * * * sun-thu', now)
        assert pycron.is_now('* * * * sunday-thursday', now)
        assert pycron.is_now('* * * * fri-sat', now) is False
        assert pycron.is_now('* * * * friday-saturday', now) is False
        # Special cases, where the day names are more or less incorrectly set...
        assert pycron.is_now('* * * * thu-sun', now)
        assert pycron.is_now('* * * * thursday-sunday', now)
        assert pycron.is_now('* * * * wed-sun', now)
        assert pycron.is_now('* * * * wednesday-sunday', now)
        assert pycron.is_now('* * * * wed-mon', now)
        assert pycron.is_now('* * * * wednesday-monday', now)
        assert pycron.is_now('* * * * fri-sun', now) is False
        assert pycron.is_now('* * * * friday-sunday', now) is False
        assert pycron.is_now('* * * * fri-wed', now) is False
        assert pycron.is_now('* * * * friday-wednesday', now) is False

        # Test day matching for dividers
        assert pycron.is_now('* * * * monday-sunday/3', now) is False
        assert pycron.is_now('* * * * mon-sun/3', now) is False
        assert pycron.is_now('* * * * tuesday-sunday/2', now) is False
        assert pycron.is_now('* * * * tue-sun/2', now) is False
Example #37
0
fig, axes = plt.subplots(ncols=2)
for ax, markers in zip(axes, split_list(Line2D.filled_markers)):
    for y, marker in enumerate(markers):
        ax.text(-0.5, y, repr(marker), **text_style)
        ax.plot(y * points, marker=marker, **marker_style)
    format_axes(ax)
fig.suptitle('filled markers', fontsize=14)

plt.show()

import pycron
import time

while True:
    if pycron.is_now('0 2 * * 0'):   # True Every Sunday at 02:00
        print('running backup')
    time.sleep(60)
 
import matplotlib
import matplotlib.pyplot as plt
import csv
with open('MaxMin.txt','r') as f_input:
    csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True)
    x = []
    y = []
    for cols in csv_input:
        x.append(matplotlib.dates.datestr2num(cols[0]))
        y.append(float(cols[1]))
# naming the x axis 
plt.xlabel('Real-Time') 
Example #38
0
    def _on_mqtt_message(self, client, userdata, message):
        logging.debug('mqtt_on_message %s %s', message.topic, message.payload)

        msg = None

        for point in self._points:

            if topic_matches_sub(point['topic'], message.topic):
                if not msg:
                    payload = message.payload.decode('utf-8')

                    if payload == '':
                        payload = 'null'
                    try:
                        # convert to string
                        if isinstance(payload, (bytes, bytearray)):
                            payload.decode()
                        # is it json?
                        if payload.strip().startswith('{'):
                            payload = json.loads(payload)
                    except Exception as e:
                        logging.error('parse json: %s topic: %s payload: %s',
                                      e, message.topic, message.payload)
                        return
                    msg = {
                        "topic": message.topic.split('/'),
                        "payload": payload,
                        "timestamp": message.timestamp,
                        "qos": message.qos
                    }
                if 'schedule' in point:
                    # check if current time is valid in schedule
                    if not pycron.is_now(point['schedule']):
                        logging.info('Skipping %s due to schedule %s' %
                                     (message.topic, point['schedule']))
                        continue

                measurement = self._get_value_from_str_or_JSONPath(
                    point['measurement'], msg)
                if measurement is None:
                    logging.warning('unknown measurement')
                    return

                record = {
                    'measurement': measurement,
                    'time': datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
                    'tags': {},
                    'fields': {}
                }

                if 'base64decode' in self._config:
                    data = self._get_value_from_str_or_JSONPath(
                        self._config['base64decode']["source"], msg)
                    dataDecoded = base64.b64decode(data)
                    msg.update({
                        "base64decoded": {
                            self._config['base64decode']["target"]: {
                                "raw": dataDecoded
                            }
                        }
                    })
                    dataDecoded = dataDecoded.hex()
                    msg.update({
                        "base64decoded": {
                            self._config['base64decode']["target"]: {
                                "hex": dataDecoded
                            }
                        }
                    })

                if 'fields' in point:

                    if isinstance(point['fields'], jsonpath_ng.JSONPath):
                        record[
                            'fields'] = self._get_value_from_str_or_JSONPath(
                                point['fields'], msg)

                    else:
                        for key in point['fields']:
                            if isinstance(point['fields'][key], dict):
                                val = self._get_value_from_str_or_JSONPath(
                                    point['fields'][key]['value'], msg)
                                convFunc = getattr(
                                    builtins, point['fields'][key]['type'],
                                    None)
                                if convFunc:
                                    try:
                                        val = convFunc(val)
                                    except ValueError:
                                        val = None
                                        logging.warning(
                                            'invalid conversion function key')
                            else:
                                val = self._get_value_from_str_or_JSONPath(
                                    point['fields'][key], msg)
                                if key == 'value':
                                    if isinstance(val, bool):
                                        if 'type' in point['fields'] and point[
                                                'fields'][
                                                    'type'] == 'booltoint':
                                            val = int(val)
                                elif key == 'type':
                                    if val == 'booltoint':
                                        val = 'int'
                            if val is None:
                                logging.warning('Unable to get value for %s' %
                                                point['fields'][key])
                                continue
                            record['fields'][key] = val
                        if len(record['fields']) != len(point['fields']):
                            logging.warning('different number of fields')

                if not record['fields']:
                    logging.warning('empty fields')
                    return

                if 'tags' in point:
                    for key in point['tags']:
                        val = self._get_value_from_str_or_JSONPath(
                            point['tags'][key], msg)
                        if val is None:
                            logging.warning('Unable to get value for tag %s' %
                                            point['tags'][key])
                            continue
                        record['tags'][key] = val

                    if len(record['tags']) != len(point['tags']):
                        logging.warning('different number of tags')

                logging.debug('influxdb write %s', record)

                self._influxdb.write_points([record],
                                            database=point.get(
                                                'database', None))

                if 'http' in self._config:
                    http_record = {}
                    for key in point['httpcontent']:
                        val = self._get_value_from_str_or_JSONPath(
                            point['httpcontent'][key], msg)
                        if val is None:
                            continue
                        http_record.update({key: val})

                    action = getattr(requests, self._config['http']['action'],
                                     None)
                    if action:
                        r = action(url=self._config['http']['destination'],
                                   data=http_record,
                                   auth=HTTPBasicAuth(
                                       self._config['http']['username'],
                                       self._config['http']['password']))
                    else:
                        logging.error("Invalid HTTP method key!")
Example #39
0
    def run(now):
        assert pycron.is_now('* * * * *', now)
        assert pycron.is_now('9 * * * *', now)
        assert pycron.is_now('*/1 * * * *', now)
        assert pycron.is_now('*/3 * * * *', now)
        assert pycron.is_now('*/9 * * * *', now)
        assert pycron.is_now('3,9,25,16 * * * *', now)
        assert pycron.is_now('*/2 * * * *', now) is False
        assert pycron.is_now('*/4 * * * *', now) is False
        assert pycron.is_now('*/5 * * * *', now) is False
        assert pycron.is_now('*/12 * * * *', now) is False
        assert pycron.is_now('3,25,16 * * * *', now) is False
        assert pycron.is_now('0-10 * * * *', now)
        assert pycron.is_now('0-10 0-10 * * *', now)
        assert pycron.is_now('10-20 * * * *', now) is False
        assert pycron.is_now('10-20 10-20 * * *', now) is False
        assert pycron.is_now('1,2,5-10 * * * *', now)
        assert pycron.is_now('9,5-8 * * * *', now)
        assert pycron.is_now('10,20-30 * * * *', now) is False

        # Issue 14
        assert pycron.is_now('1-59/2 * * * *', now) is True
        assert pycron.is_now('1-59/4 * * * *', now) is True
        assert pycron.is_now('1-59/8 * * * *', now) is True