|
1 | 1 | require 'digest/md5'
|
2 | 2 |
|
3 | 3 | Puppet::Parser::Functions::newfunction(:fqdn_rand, :arity => -2, :type => :rvalue, :doc =>
|
4 |
| - "Generates random numbers based on the node's fqdn. Generated random values |
5 |
| - will be a range from 0 up to and excluding n, where n is the first parameter. |
6 |
| - The second argument specifies a number or string to add to the seed and is optional. |
| 4 | + "Usage: `fqdn_rand(MAX, [SEED])`. MAX is required and must be a positive |
| 5 | + integer; SEED is optional and may be any number or string. |
7 | 6 |
|
8 |
| - Examples: |
| 7 | + Generates a random whole number greater than or equal to 0 and less than MAX, |
| 8 | + combining the `$fqdn` fact and the value of SEED for repeatable randomness. |
| 9 | + (That is, each node will get a different random number from this function, but |
| 10 | + a given node's result will be the same every time unless its hostname changes.) |
9 | 11 |
|
10 |
| - $random_number = fqdn_rand(30) |
11 |
| - $random_number_seeded = fqdn_rand(30,30) |
12 |
| - $random_number_seeded2 = fqdn_rand(30,'foo')") do |args| |
| 12 | + This function is usually used for spacing out runs of resource-intensive cron |
| 13 | + tasks that run on many nodes, which could cause a thundering herd or degrade |
| 14 | + other services if they all fire at once. Adding a SEED can be useful when you |
| 15 | + have more than one such task and need several unrelated random numbers per |
| 16 | + node. (For example, `fqdn_rand(30)`, `fqdn_rand(30, 'expensive job 1')`, and |
| 17 | + `fqdn_rand(30, 'expensive job 2')` will produce totally different numbers.)") do |args| |
13 | 18 | max = args.shift.to_i
|
14 | 19 | seed = Digest::MD5.hexdigest([self['::fqdn'],args].join(':')).hex
|
15 | 20 | Puppet::Util.deterministic_rand(seed,max)
|
|
0 commit comments