blib/lib/App/Netdisco/Backend/Queues.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 122 CC 9 SCAR 24.7
CC
9
Cov
0%
worker_queues
30.0
queue_for
12.0
_tasks
6.0
CRAP 11.8
1 package App::Netdisco::Backend::Queues;
2  
3 use strict;
4 use warnings;
5  
6 use Dancer qw/:moose :syntax :script/;
7 use App::Netdisco::Util::MCE 'parse_max_workers';
8  
9 use base 'Exporter';
10 our @EXPORT = ();
11 our @EXPORT_OK = qw/
12 worker_queues
13 queue_for
14 total_workers
15 /;
16 our %EXPORT_TAGS = (all => \@EXPORT_OK);
17  
18 =head1 NAME
19  
20 App::Netdisco::Backend::Queues
21  
22 =head1 DESCRIPTION
23  
24 The backend normally runs all jobs on one pool of workers (C<workers.tasks>).
25 The C<workers.queues> setting reserves workers for particular actions, so that
26 a long queue of one kind of job (say, discovery) cannot keep another (say,
27 C<portcontrol>) waiting for a free worker:
28  
29 workers:
30 tasks: 'AUTO * 2'
31 queues:
32 portcontrol:
33 tasks: 2
34 actions: [portcontrol, vlan]
35  
36 An action named in a queue is run only by the workers of that queue, and the
37 general pool never takes it. A queue without workers or actions is ignored,
38 and its actions stay with the general pool.
39  
40 =head1 EXPORT_OK
41  
42 =head2 worker_queues
43  
44 Returns a list of hash references, each with C<name>, C<tasks> (the number of
45 workers), C<actions> and C<only>. The first is always the general pool, which
46 lists the reserved actions it must leave alone (C<only> false). The others are
47 the reserved queues in name order, which list the actions they take (C<only>
48 true). Dies if an action is in more than one queue.
49  
50 =head2 queue_for( \@queues, $action )
51  
52 Returns the entry of C<@queues> (as from C<worker_queues>) which runs the
53 given action. Actions not reserved go to the general pool, or if that has no
54 workers, to the first reserved queue.
55  
56 =head2 total_workers
57  
58 The number of workers over all queues.
59  
60 =cut
61  
62 sub worker_queues {
63 my $conf = setting('workers')->{queues};
64 $conf = {} unless ref {} eq ref $conf;
65  
66 my (@reserved, %owner);
67  
68 foreach my $name (sort keys %$conf) {
69 my $queue = $conf->{$name};
70 next unless ref {} eq ref $queue;
71  
72 my $tasks = _tasks($queue->{tasks});
73 my @actions = _base_actions($queue->{actions});
74 next unless $tasks and scalar @actions;
75  
76 foreach my $action (@actions) {
77 die sprintf "workers: action '%s' is in both queue '%s' and queue '%s'\n",
78 $action, $owner{$action}, $name if exists $owner{$action};
79 $owner{$action} = $name;
80 }
81  
82 push @reserved, { name => $name, tasks => $tasks,
83 actions => \@actions, only => 1 };
84 }
85  
86 return ({
87 name => 'general',
88 tasks => _tasks(setting('workers')->{tasks}),
89 actions => [ sort keys %owner ],
90 only => 0,
91 }, @reserved);
92 }
93  
94 sub queue_for {
95 my ($queues, $action) = @_;
96 $action = _base_action($action);
97  
98 foreach my $queue (@$queues) {
99 return $queue if $queue->{only}
100 and scalar grep { $_ eq $action } @{ $queue->{actions} };
101 }
102  
103 # a node with only reserved queues still has to run its internal jobs
104 return $queues->[0] if $queues->[0]->{tasks};
105 return $queues->[1] || $queues->[0];
106 }
107  
108 sub total_workers {
109 my $total = 0;
110 $total += $_->{tasks} for worker_queues();
111 return $total;
112 }
113  
114 # parse_max_workers can hand back a negative number, or the setting unparsed
115 sub _tasks {
116 my $tasks = parse_max_workers(shift);
117 return ($tasks =~ m/^\d+$/ ? $tasks : 0);
118 }
119  
120 # a job for 'action::stage' is run by the queue of 'action'
121 sub _base_action {
122 my $action = lc(shift || '');
123 $action =~ s/::.*//;
124 return $action;
125 }
126  
127 sub _base_actions {
128 my $actions = shift;
129 $actions = [ $actions ] if defined $actions and ref [] ne ref $actions;
130  
131 my %seen;
132 return grep { length($_) and not $seen{$_}++ }
133 map { _base_action($_) } @{ $actions || [] };
134 }
135  
136 1;