How can I make cfengine do things during a specific time window?
You can find "time based classes" in the cf-promises --show-classes
output,
they are easily identified because they are tagged with time_based
. For
example:
|
|
| Day2 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Afternoon time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Day2 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Hr14 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Hr14_Q1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Lcycle_1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_March time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Min10 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Min10_15 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Q1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Thursday time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | GMT_Yr2017 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Hr08 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Hr08_Q1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Hr8 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Lcycle_1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | March time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Min10 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Min10_15 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Morning time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Q1 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Thursday time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass | | Yr2017 time_based | cfengine_internal_time_based_autoremove | source=agent | hardclass |
You can guard your promise with a time based class expression so that it can
only execute during the 7:00 hour. It's important to understand that body
action if_elapsed
is based on promise locking. So that restriction will not be
maintained if you run the agent without locks. Usually that's fine, but in some
cases you might want a bit more protection to be sure that the promise does not
execute more than once within a given time period. If you need higher assurance
you can use persistent classes.
|
|
> $ sudo cf-agent -KIf ./main.cf R: Hello, its during the 8:00am hour here. Specifically its Thu Mar 2 08:30:55 2017 R: This report will only be emitted once during the 8:00 hour. R: This report will only be emitted once during the 8:00 hour. Even if locks are skipped. > $ sudo cf-agent -KIf ./main.cf R: Hello, its during the 8:00am hour here. Specifically its Thu Mar 2 08:30:59 2017 R: This report will only be emitted once during the 8:00 hour.
Generally I do not recommend using time based classes as a direct guard because it can complicate testing or ad-hoc execution. I typically recommend setting a class based on the time class, and then using that soft class as your guard. I think this example will make it more clear:
|
|
# Note its during 8am hour, so no reports > $ sudo cf-agent -KIf ./main.cf R: Hello, its Thu Mar 2 08:40:21 2017 # We get both reports becasue I manually defined # the maintance_window class which would normally # only be defined during the 8am hour. # This is useful for testing or ad-hoc execution. > $ sudo cf-agent -KIf ./main.cf --define maintanance_window R: Hello, its Thu Mar 2 08:42:41 2017 R: This report will only be emitted once during the maintanaince window unless locks are cleared. R: This report will only be emitted once during the maintanance window. Even if locks are skipped. # Note how the persistent class is effective even when # promise locks are cleared. > $ sudo cf-agent -KIf ./main.cf --define maintanance_window R: Hello, its Thu Mar 2 08:42:56 2017 R: This report will only be emitted once during the maintanaince window unless locks are cleared.
Hope this helps.