Puppet Function: rand_cron

Defined in:
lib/puppet/parser/functions/rand_cron.rb
Function type:
Ruby 3.x API

Overview

rand_cron(String $modifier, Integer $occurs, Integer $scope)Variant[Integer[0,59], Array[Integer[0,59], Integer[0,23]]]

Provides a 'random' value to cron based on the passed Integer value.

Used to avoid starting a certain cron job at the same time on all servers.

If used with no parameters, it will return a single value between 0-59 first argument is the occurrence within a timeframe, for example if you want it to run 2 times per hour the second argument is the timeframe, by default its 60 minutes, but it could also be 24 hours etc

Based on: projects.puppetlabs.com/projects/puppet/wiki/Cron_Patterns/8/diff

  • Author: ohadlevy@gmail.com

  • License: None Posted

Examples:


int_to_cron('100')    - returns one value between 0..59 based on the value 100
int_to_cron(100,2)    - returns an array of two values between 0..59 based on the value 100
int_to_cron(100,2,24) - returns an array of two values between 0..23 based on the value 100

Parameters:

  • modifier (String)

    Input range modifier

  • occurs (Integer)

    How many values to return

  • scope (Integer)

    Top range of randomly generated number

Returns:

  • (Variant[Integer[0,59], Array[Integer[0,59], Integer[0,23]]])


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/parser/functions/rand_cron.rb', line 3

newfunction(:rand_cron, :type => :rvalue, :arity => -1, :doc => <<-'ENDHEREDOC') do |args|

  modifier = Array(args[0]).flatten.first
  Provides a 'random' value to `cron` based on the passed `Integer` value.

  Used to avoid starting a certain `cron` job at the same time on all
  servers.

  If used with no parameters, it will return a single value between `0-59`
  first argument is the occurrence within a timeframe, for example if you
  want it to run `2` times per hour the second argument is the timeframe,
  by default its `60` minutes, but it could also be `24` hours etc

  Based on: http://projects.puppetlabs.com/projects/puppet/wiki/Cron_Patterns/8/diff

    * Author: ohadlevy@gmail.com
    * License: None Posted

  @example

    int_to_cron('100')    - returns one value between 0..59 based on the value 100
    int_to_cron(100,2)    - returns an array of two values between 0..59 based on the value 100
    int_to_cron(100,2,24) - returns an array of two values between 0..23 based on the value 100

  @param modifier [String]
    Input range modifier

  @param occurs [Integer]
    How many values to return

  @param scope [Integer]
    Top range of randomly generated number

  @return [Variant[Integer[0,59], Array[Integer[0,59], Integer[0,23]]]]
  ENDHEREDOC
  occurs   = (args[1] || 1).to_i
  scope    = (args[2] || 60).to_i

  # We're making a special case for IP Addresses since they are most
  # likely to be passed in here and you want to let them act
  # linearly.
  begin
    modifier = IPAddr.new(modifier).to_i
  rescue ArgumentError
    require 'zlib'
    modifier = Zlib.crc32("#{modifier}")
  end

  base    = modifier % scope

  if occurs == 1
    base
  else
    cron = Array.new
    (1..occurs).each do |i|
      cron << ((base - (scope / occurs * i)) % scope)
    end
    return cron.sort
  end
end