saltstack / saltstack/salt

Job schedule using when with multiple dates not working

Open
#53,604 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug doc-request documentation severity-medium time-estimate-sprint
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description of Issue

It is not possible to define job which must runs multiple times every day using salt schedule with when directive. If only single date/time is specified then everything works as expected.

If job has defined multiple run hours and schedule.reload is executed then only hours after now are scheduled. Propably cron directive could be used to achieve this but it requires python2-croniter package which is not a part of base RHEL/CentOS 7 in opposite to python-dateutil which is a base package.

I've managed to achieve working solutions by modifying salt/utils/schedule.py:

@@ -963,9 +963,10 @@ class Schedule(object):
                 _when.sort()

                 # Copy the list so we can loop through it
+                today = dateutil_parser.parse('0:00')
                 for i in copy.deepcopy(_when):
                     if len(_when) > 1:
-                        if i < now - loop_interval:
+                        if i < today - loop_interval:

But I'm not sure if it is a proper solution and do not break something else. Is it possible to fix it this way or am I misunderstanding something? Maybe just a hint in documentation should be added to clarify usage if it cannot be changed? If You agree to fix it then I could provide pull request with patch.

As a workaround for now I have defined multiple jobs with single when time definition instead of one job with multiple entries in when directive.

Setup

/srv/pillar/top.sls

base:
  '*':
    - schedule

Define hours before now in /srv/pillar/schedule.sls

schedule:
  test:
    function: test.ping
    name: test
    when:
    - 09:00am
    - 10:00am
    - 11:00am
Steps to Reproduce Issue

salt-call pillar.get schedule

local:
    ----------
    test:
        ----------
        function:
            test.ping
        name:
            test
        when:
            - 09:00am
            - 10:00am
            - 11:00am

salt-call schedule.reload

local:
    ----------
    comment:
        - Reloaded schedule from pillar on minion.
    result:
        True

salt-call schedule.list

local:
    schedule:
      test:
        enabled: true
        function: test.ping
        name: test
        when:
        - 09:00am
        - 10:00am
        - 11:00am

salt-call schedule.show_next_fire_time test

local:
    ----------
    next_fire_time:
        None
    result:
        True

When defined hours are after now then it works as expected:
salt-call schedule.list

local:
    schedule:
      test:
        enabled: true
        function: test.ping
        name: test
        splay: null
        when:
        - 17:00pm
        - 18:00pm
        - 19:00pm

salt-call schedule.show_next_fire_time test

local:
    ----------
    next_fire_time:
        2019-06-26T17:00:00
    result:
        True
Versions Report
Salt Version:
           Salt: 2019.2.0

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.5 (default, Oct 30 2018, 23:45:53)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: centos 7.6.1810 Core
         locale: UTF-8
        machine: x86_64
        release: 3.10.0-957.1.3.el7.x86_64
         system: Linux
        version: CentOS Linux 7.6.1810 Core

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in salt/utils/schedule.py around the loop in the reported schedule handling, then reproduce the behavior with schedule.reload and schedule.show_next_fire_time using the supplied multiple-when example. Verify how past and future entries are selected after reload; done means multiple daily times produce a valid next fire time without breaking single-time schedules.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.