Python croniter.croniter() Examples

The following are 30 code examples of croniter.croniter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module croniter , or try the search function .
Example #1
Source File: BackupHelper.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 7 votes vote down vote up
def backupIfNeeded(self, cluster_object: V1MongoClusterConfiguration) -> bool:
        """
        Checks whether a backup is needed for the cluster, backing it up if necessary.
        :param cluster_object: The cluster object from the YAML file.
        :return: Whether a backup was created or not.
        """
        now = self._utcNow()

        cluster_key = (cluster_object.metadata.name, cluster_object.metadata.namespace)
        last_backup = self._last_backups.get(cluster_key)
        next_backup = croniter(cluster_object.spec.backups.cron, last_backup, datetime).get_next() \
            if last_backup else now

        if next_backup <= now:
            self.backup(cluster_object, now)
            self._last_backups[cluster_key] = now
            return True

        logging.info("Cluster %s @ ns/%s will need a backup at %s.", cluster_object.metadata.name,
                     cluster_object.metadata.namespace, next_backup.isoformat())
        return False 
Example #2
Source File: loop.py    From query-exporter with GNU General Public License v3.0 6 votes vote down vote up
def start(self):
        """Start timed queries execution."""
        for db in self._databases:
            try:
                await db.connect()
            except DataBaseError:
                self._increment_db_error_count(db)

        for query in self._timed_queries:
            if query.interval:
                call = PeriodicCall(self._run_query, query)
                call.start(query.interval)
            else:
                call = TimedCall(self._run_query, query)
                now = datetime.now().replace(tzinfo=gettz())
                cron_iter = croniter(query.schedule, now)

                def times_iter():
                    while True:
                        delta = next(cron_iter) - time.time()
                        yield self._loop.time() + delta

                call.start(times_iter())
            self._timed_calls[query.name] = call 
Example #3
Source File: database.py    From moxie with MIT License 6 votes vote down vote up
def reschedule(self, name):
            state = yield from self.get(name)
            if state.manual:
                raise ValueError("Can't reschedule")
            else:
                local_offset = pytz.timezone(state.timezone).utcoffset(dt.datetime.utcnow())
                cron = croniter(state.crontab, dt.datetime.utcnow() + local_offset)
                reschedule = cron.get_next(dt.datetime) - local_offset

            with (yield from self.db.engine) as conn:
                yield from conn.execute(update(
                    Job.__table__
                ).where(
                    Job.name==name
                ).values(
                    active=True,
                    scheduled=reschedule,
                )) 
Example #4
Source File: cron_app.py    From reddit2telegram with MIT License 6 votes vote down vote up
def read_own_cron(own_cron_filename, config):
    with open(own_cron_filename) as tsv_file:
        tsv_reader = csv.DictReader(tsv_file, delimiter='\t')
        for row in tsv_reader:
            now = datetime.datetime.now()
            cron = croniter(row['MASK'])
            # prev_run = cron.get_current(datetime.datetime)
            prev_run = cron.get_prev(datetime.datetime)
            prev_run = cron.get_next(datetime.datetime)
            diff = now - prev_run
            diff_seconds = diff.total_seconds()
            if 0.0 <= diff_seconds and diff_seconds <= 59.9:
                # print(row['submodule_name'], diff_seconds)
                # supply(row['submodule_name'], config)
                supplying_process = Process(target=supply, args=(row['submodule_name'], config))
                supplying_process.start() 
Example #5
Source File: schedules.py    From incubator-superset with Apache License 2.0 6 votes vote down vote up
def next_schedules(
    crontab: str, start_at: datetime, stop_at: datetime, resolution: int = 0
) -> Iterator[datetime]:
    crons = croniter.croniter(crontab, start_at - timedelta(seconds=1))
    previous = start_at - timedelta(days=1)

    for eta in crons.all_next(datetime):
        # Do not cross the time boundary
        if eta >= stop_at:
            break

        if eta < start_at:
            continue

        # Do not allow very frequent tasks
        if eta - previous < timedelta(seconds=resolution):
            continue

        yield eta
        previous = eta 
Example #6
Source File: __init__.py    From aodh with Apache License 2.0 6 votes vote down vote up
def within_time_constraint(cls, alarm):
        """Check whether the alarm is within at least one of its time limits.

        If there are none, then the answer is yes.
        """
        if not alarm.time_constraints:
            return True

        now_utc = timeutils.utcnow().replace(tzinfo=pytz.utc)
        for tc in alarm.time_constraints:
            tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None
            now_tz = now_utc.astimezone(tz) if tz else now_utc
            start_cron = croniter.croniter(tc['start'], now_tz)
            if cls._is_exact_match(start_cron, now_tz):
                return True
            # start_cron.cur has changed in _is_exact_match(),
            # croniter cannot recover properly in some corner case.
            start_cron = croniter.croniter(tc['start'], now_tz)
            latest_start = start_cron.get_prev(datetime.datetime)
            duration = datetime.timedelta(seconds=tc['duration'])
            if latest_start <= now_tz <= latest_start + duration:
                return True
        return False 
Example #7
Source File: views.py    From healthchecks with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def cron_preview(request):
    schedule = request.POST.get("schedule", "")
    tz = request.POST.get("tz")
    ctx = {"tz": tz, "dates": []}

    try:
        zone = pytz.timezone(tz)
        now_local = timezone.localtime(timezone.now(), zone)

        if len(schedule.split()) != 5:
            raise ValueError()

        it = croniter(schedule, now_local)
        for i in range(0, 6):
            ctx["dates"].append(it.get_next(datetime))

        ctx["desc"] = str(ExpressionDescriptor(schedule, use_24hour_time_format=True))
    except UnknownTimeZoneError:
        ctx["bad_tz"] = True
    except:
        ctx["bad_schedule"] = True

    return render(request, "front/cron_preview.html", ctx) 
Example #8
Source File: dag.py    From airflow with Apache License 2.0 6 votes vote down vote up
def is_fixed_time_schedule(self):
        """
        Figures out if the DAG schedule has a fixed time (e.g. 3 AM).

        :return: True if the schedule has a fixed time, False if not.
        """
        now = datetime.now()
        cron = croniter(self.normalized_schedule_interval, now)

        start = cron.get_next(datetime)
        cron_next = cron.get_next(datetime)

        if cron_next.minute == start.minute and cron_next.hour == start.hour:
            return True

        return False 
Example #9
Source File: dagbag.py    From airflow with Apache License 2.0 6 votes vote down vote up
def collect_dags_from_db(self):
        """Collects DAGs from database."""
        from airflow.models.serialized_dag import SerializedDagModel
        start_dttm = timezone.utcnow()
        self.log.info("Filling up the DagBag from database")

        # The dagbag contains all rows in serialized_dag table. Deleted DAGs are deleted
        # from the table by the scheduler job.
        self.dags = SerializedDagModel.read_all_dags()

        # Adds subdags.
        # DAG post-processing steps such as self.bag_dag and croniter are not needed as
        # they are done by scheduler before serialization.
        subdags = {}
        for dag in self.dags.values():
            for subdag in dag.subdags:
                subdags[subdag.dag_id] = subdag
        self.dags.update(subdags)

        Stats.timing('collect_db_dags', timezone.utcnow() - start_dttm) 
Example #10
Source File: periodictask.py    From privacyidea with GNU Affero General Public License v3.0 6 votes vote down vote up
def calculate_next_timestamp(ptask, node, interval_tzinfo=None):
    """
    Calculate the timestamp of the next scheduled run of task ``ptask`` on node ``node``.
    We do not check if the task is even scheduled to run on the specified node.
    Malformed cron expressions may throw a ``ValueError``.

    The next timestamp is calculated based on the last time the task was run on the given node.
    If the task has never run on the node, the last update timestamp of the periodic tasks
    is used as a reference timestamp.

    :param ptask: Dictionary describing the periodic task, as from ``PeriodicTask.get()``
    :param node: Node on which the periodic task is scheduled
    :type node: unicode
    :param interval_tzinfo: Timezone in which the cron expression should be interpreted. Defaults to local time.
    :type interval_tzinfo: tzinfo
    :return: a timezone-aware (UTC) datetime object
    """
    if interval_tzinfo is None:
        interval_tzinfo = tzlocal()
    timestamp = ptask["last_runs"].get(node, ptask["last_update"])
    local_timestamp = timestamp.astimezone(interval_tzinfo)
    iterator = croniter(ptask["interval"], local_timestamp)
    next_timestamp = iterator.get_next(datetime)
    # This will again be a timezone-aware datetime, but we return a timezone-aware UTC timestamp
    return next_timestamp.astimezone(tzutc()) 
Example #11
Source File: badpenny.py    From build-relengapi with Mozilla Public License 2.0 5 votes vote down vote up
def cron_task(cron_spec):
    """Decorator for a task that executes on a cron-like schedule"""
    # test the cron spec before the function is called
    croniter.croniter(cron_spec)

    def runnable_now(task, now):
        last_run = max(j.created_at for j in task.jobs) if task.jobs else None
        ci = croniter.croniter(cron_spec, last_run)
        return now >= ci.get_next(datetime)
    return _task_decorator(runnable_now, "cron: %s" % cron_spec) 
Example #12
Source File: jobs.py    From qinling with Apache License 2.0 5 votes vote down vote up
def get_next_execution_time(pattern, start_time):
    return croniter.croniter(pattern, start_time).get_next(
        datetime.datetime
    ) 
Example #13
Source File: jobs.py    From qinling with Apache License 2.0 5 votes vote down vote up
def validate_job(params):
    first_time = params.get('first_execution_time')
    pattern = params.get('pattern')
    count = params.get('count')
    start_time = timeutils.utcnow()

    if not (first_time or pattern):
        raise exc.InputException(
            'pattern or first_execution_time must be specified.'
        )

    if first_time:
        first_time = validate_next_time(first_time)
        if not pattern and count and count > 1:
            raise exc.InputException(
                'pattern must be provided if count is greater than 1.'
            )

        next_time = first_time
        if not (pattern or count):
            count = 1
    if pattern:
        validate_pattern(pattern)

        if first_time:
            start_time = first_time - datetime.timedelta(minutes=1)

        next_time = croniter.croniter(pattern, start_time).get_next(
            datetime.datetime
        )
        first_time = next_time

    return first_time, next_time, count 
Example #14
Source File: jobs.py    From qinling with Apache License 2.0 5 votes vote down vote up
def validate_pattern(pattern):
    try:
        croniter.croniter(pattern)
    except (ValueError, KeyError):
        raise exc.InputException(
            'The specified pattern is not valid: {}'.format(pattern)
        ) 
Example #15
Source File: alarms.py    From aodh with Apache License 2.0 5 votes vote down vote up
def validate(value):
        # raises ValueError if invalid
        croniter.croniter(value)
        return value 
Example #16
Source File: __init__.py    From aodh with Apache License 2.0 5 votes vote down vote up
def _is_exact_match(cron, ts):
        """Handle edge in case when both parameters are equal.

        Handle edge case where if the timestamp is the same as the
        cron point in time to the minute, croniter returns the previous
        start, not the current. We can check this by first going one
        step back and then one step forward and check if we are
        at the original point in time.
        """
        cron.get_prev()
        diff = (ts - cron.get_next(datetime.datetime)).total_seconds()
        return abs(diff) < 60  # minute precision 
Example #17
Source File: schedules.py    From flytekit with Apache License 2.0 5 votes vote down vote up
def _validate_expression(cron_expression):
        """
        Ensures that the set value is a valid cron string.  We use the format used in Cloudwatch and the best
        explanation can be found here:
            https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions
        :param Text cron_expression: cron expression
        """
        # We use the croniter lib to validate our cron expression.  Since on the admin side we use Cloudwatch,
        # we have a couple checks in order to line up Cloudwatch with Croniter.
        tokens = cron_expression.split()
        if len(tokens) != 6:
            raise _user_exceptions.FlyteAssertion(
                "Cron expression is invalid.  A cron expression must have 6 fields.  Cron expressions are in the "
                "format of: `minute hour day-of-month month day-of-week year`.  Received: `{}`".format(
                    cron_expression
                )
            )

        if tokens[2] != '?' and tokens[4] != '?':
            raise _user_exceptions.FlyteAssertion(
                "Scheduled string is invalid.  A cron expression must have a '?' for either day-of-month or "
                "day-of-week.  Please specify '?' for one of those fields.  Cron expressions are in the format of: "
                "minute hour day-of-month month day-of-week year.\n\n"
                "For more information: "
                "https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions"
            )

        try:
            # Cut to 5 fields and just assume year field is good because croniter treats the 6th field as seconds.
            # TODO: Parse this field ourselves and check
            _croniter.croniter(" ".join(cron_expression.replace('?', '*').split()[:5]))
        except:
            raise _user_exceptions.FlyteAssertion(
                "Scheduled string is invalid.  The cron expression was found to be invalid."
                " Provided cron expr: {}".format(
                    cron_expression
                )
            ) 
Example #18
Source File: crontab_time.py    From karbor with Apache License 2.0 5 votes vote down vote up
def check_time_format(cls, pattern):
        if not pattern:
            msg = (_("The trigger pattern is None"))
            raise exception.InvalidInput(msg)

        try:
            croniter(pattern)
        except Exception:
            msg = (_("The trigger pattern(%s) is invalid") % pattern)
            raise exception.InvalidInput(msg) 
Example #19
Source File: crontab_time.py    From karbor with Apache License 2.0 5 votes vote down vote up
def compute_next_time(self, current_time):
        time = current_time if current_time >= self._start_time else (
            self._start_time)
        return croniter(self._pattern, time).get_next(datetime) 
Example #20
Source File: views.py    From crmint with Apache License 2.0 5 votes vote down vote up
def _its_time(self, cron_format):
    """Returns True if current time matches cron time spec."""
    now = int(time.time())
    itr = croniter(cron_format, now - 60)
    nxt = itr.get_next()
    return now / 60 * 60 == nxt 
Example #21
Source File: continuous.py    From watcher with Apache License 2.0 5 votes vote down vote up
def _next_cron_time(audit):
        if utils.is_cron_like(audit.interval):
            return croniter(audit.interval, datetime.datetime.utcnow()
                            ).get_next(datetime.datetime) 
Example #22
Source File: models.py    From django-rq-scheduler with MIT License 5 votes vote down vote up
def clean_cron_string(self):
        try:
            croniter.croniter(self.cron_string)
        except ValueError as e:
            raise ValidationError({
                'cron_string': ValidationError(
                    _(str(e)), code='invalid')
            }) 
Example #23
Source File: cron_sensor.py    From ARK with MIT License 5 votes vote down vote up
def next(self, now=None):
        """
        获取下次触发时间

        :return: 下次的触发时间
        :rtype: float
        """
        if now is None:
            now = time.time()

        self._current = croniter(self._timer_str, now).get_next()
        return self._current 
Example #24
Source File: __init__.py    From trader with Apache License 2.0 5 votes vote down vote up
def _register_param(self):
        self.datetime = datetime.datetime.now().replace(tzinfo=pytz.FixedOffset(480))
        self.time = time.time()
        self.loop_time = self.io_loop.time()
        for fun_name, args in self.module_arg_dict.items():
            if 'crontab' in args:
                key = args['crontab']
                self.crontab_router[key]['func'] = getattr(self, fun_name)
                self.crontab_router[key]['iter'] = croniter(args['crontab'], self.datetime)
                self.crontab_router[key]['handle'] = None
            elif 'channel' in args:
                self.channel_router[args['channel']] = getattr(self, fun_name) 
Example #25
Source File: periodictask.py    From privacyidea with GNU Affero General Public License v3.0 5 votes vote down vote up
def set_periodic_task(name, interval, nodes, taskmodule, ordering=0, options=None, active=True, id=None):
    """
    Set a periodic task configuration. If ``id`` is None, this creates a new database entry.
    Otherwise, an existing entry is overwritten. We actually ensure that such
    an entry exists and throw a ``ParameterError`` otherwise.

    This also checks if ``interval`` is a valid cron expression, and throws
    a ``ParameterError`` if it is not.

    :param name: Unique name of the periodic task
    :type name: unicode
    :param interval: Periodicity as a string in crontab format
    :type interval: unicode
    :param nodes: List of nodes on which this task should be run
    :type nodes: list of unicode
    :param taskmodule: Name of the task module
    :type taskmodule: unicode
    :param ordering: Ordering of the periodic task (>= 0). Lower numbers are executed first.
    :type ordering: int
    :param options: Additional options for the task module
    :type options: Dictionary mapping unicodes to values that can be converted to unicode or None
    :param active: Flag determining whether the periodic task is active
    :type active: bool
    :param id: ID of the existing entry, or None
    :type id: int or None
    :return: ID of the entry
    """
    try:
        croniter(interval)
    except ValueError as e:
        raise ParameterError("Invalid interval: {!s}".format(e))
    if ordering < 0:
        raise ParameterError("Invalid ordering: {!s}".format(ordering))
    if id is not None:
        # This will throw a ParameterError if there is no such entry
        get_periodic_task_by_id(id)
    periodic_task = PeriodicTask(name, active, interval, nodes, taskmodule, ordering, options, id)
    return periodic_task.id 
Example #26
Source File: dag.py    From airflow with Apache License 2.0 5 votes vote down vote up
def following_schedule(self, dttm):
        """
        Calculates the following schedule for this dag in UTC.

        :param dttm: utc datetime
        :return: utc datetime
        """
        if isinstance(self.normalized_schedule_interval, str):
            # we don't want to rely on the transitions created by
            # croniter as they are not always correct
            dttm = pendulum.instance(dttm)
            naive = timezone.make_naive(dttm, self.timezone)
            cron = croniter(self.normalized_schedule_interval, naive)

            # We assume that DST transitions happen on the minute/hour
            if not self.is_fixed_time_schedule():
                # relative offset (eg. every 5 minutes)
                delta = cron.get_next(datetime) - naive
                following = dttm.in_timezone(self.timezone) + delta
            else:
                # absolute (e.g. 3 AM)
                naive = cron.get_next(datetime)
                tz = pendulum.timezone(self.timezone.name)
                following = timezone.make_aware(naive, tz)
            return timezone.convert_to_utc(following)
        elif self.normalized_schedule_interval is not None:
            return timezone.convert_to_utc(dttm + self.normalized_schedule_interval) 
Example #27
Source File: dag.py    From airflow with Apache License 2.0 5 votes vote down vote up
def previous_schedule(self, dttm):
        """
        Calculates the previous schedule for this dag in UTC

        :param dttm: utc datetime
        :return: utc datetime
        """
        if isinstance(self.normalized_schedule_interval, str):
            # we don't want to rely on the transitions created by
            # croniter as they are not always correct
            dttm = pendulum.instance(dttm)
            naive = timezone.make_naive(dttm, self.timezone)
            cron = croniter(self.normalized_schedule_interval, naive)

            # We assume that DST transitions happen on the minute/hour
            if not self.is_fixed_time_schedule():
                # relative offset (eg. every 5 minutes)
                delta = naive - cron.get_prev(datetime)
                previous = dttm.in_timezone(self.timezone) - delta
            else:
                # absolute (e.g. 3 AM)
                naive = cron.get_prev(datetime)
                tz = pendulum.timezone(self.timezone.name)
                previous = timezone.make_aware(naive, tz)
            return timezone.convert_to_utc(previous)
        elif self.normalized_schedule_interval is not None:
            return timezone.convert_to_utc(dttm - self.normalized_schedule_interval) 
Example #28
Source File: test_enqueue.py    From fastlane with MIT License 5 votes vote down vote up
def test_enqueue5(client):
    """Test enqueue a job using cron"""

    with client.application.app_context():
        app = client.application
        app.redis.flushall()

        task_id = str(uuid4())

        data = {"image": "ubuntu", "command": "ls", "cron": "*/10 * * * *"}
        options = dict(
            data=dumps(data),
            headers={"Content-Type": "application/json"},
            follow_redirects=True,
        )

        response = client.post(f"/tasks/{task_id}/", **options)
        expect(response.status_code).to_equal(200)
        obj = loads(response.data)
        job_id = obj["jobId"]
        expect(job_id).not_to_be_null()
        expect(obj["queueJobId"]).not_to_be_null()

        res = app.redis.zcard(Queue.SCHEDULED_QUEUE_NAME)
        expect(res).to_equal(1)

        res = app.redis.zrangebyscore(
            Queue.SCHEDULED_QUEUE_NAME, "-inf", "+inf", withscores=True
        )
        expect(res).to_length(1)

        _, timestamp = res[0]

        cron = croniter("*/10 * * * *", datetime.now())
        expected = cron.get_next(datetime)
        expect(timestamp).to_equal(expected.timestamp()) 
Example #29
Source File: utils.py    From fastlane with MIT License 5 votes vote down vote up
def get_next_cron_timestamp(cron):
    itr = croniter.croniter(cron, datetime.utcnow())
    next_dt = itr.get_next(datetime)

    return next_dt 
Example #30
Source File: trafaret.py    From airflow-declarative with Apache License 2.0 5 votes vote down vote up
def cast_crontab_or_interval(value):
    try:
        return cast_interval(value)
    except t.DataError:
        if not isinstance(value, str):
            raise
        # This is definitely not a valid time interval.
        # Perhaps this is a cron expression?
        try:
            # Airflow uses `croniter` as well.
            croniter(value)
        except ValueError as e:
            raise t.DataError("Invalid timedelta or crontab expression: %s" % str(e))
        else:
            return value