1
2
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#!/usr/bin/env perl
#
# send-daemon
# FixMyStreet daemon for sending reports and updates.
use strict;
use warnings;
use v5.14;
BEGIN {
use File::Basename qw(dirname);
use File::Spec;
my $d = dirname(File::Spec->rel2abs($0));
require "$d/../setenv.pl";
}
use Getopt::Long::Descriptive;
use Parallel::ForkManager;
use CronFns;
use FixMyStreet;
use FixMyStreet::DB;
use FixMyStreet::Script::Reports;
use FixMyStreet::Queue::Item::Report;
use Open311::PostServiceRequestUpdates;
my ($opts, $usage) = describe_options(
'%c %o',
['verbose|v+', 'more verbose output'],
['nomail', 'do not send any email, print instead'],
['debug', 'always try and send reports (no back-off skipping)'],
['help|h', "print usage message and exit" ],
[],
['Send a USR1 signal to the parent to cycle through verbose levels.'],
);
$usage->die if $opts->help;
my $verbose = $opts->verbose || 0;
my $site = CronFns::site(FixMyStreet->config('BASE_URL'));
my $states = [ FixMyStreet::DB::Result::Problem::open_states() ];
$states = [ 'submitted', 'confirmed', 'in progress', 'feedback pending', 'external', 'wish' ] if $site eq 'zurich';
my $db = FixMyStreet::DB->schema->storage;
my %children;
my $exit = 0;
$SIG{TERM} = $SIG{INT} = sub { $exit = 1; };
my $changeverboselevel = 0;
$SIG{USR1} = sub {
kill 'USR1', keys %children;
++$changeverboselevel;
};
my $procs = FixMyStreet->config('QUEUE_DAEMON_PROCESSES') || 4;
my $pm = Parallel::ForkManager->new($procs);
$pm->run_on_start(sub {
my $pid = shift;
$children{$pid} = time();
});
$pm->run_on_finish(sub {
my $pid = shift;
if ($children{$pid} > time() - 10) {
# It didn't live very long, let's wait a bit
sleep(5);
}
delete $children{$pid};
});
# The parent loop
while (!$exit) {
while (keys %children < $procs) {
$pm->start and next;
srand;
$SIG{USR1} = sub { ++$changeverboselevel; };
while (!$exit) {
$0 = "fmsd (running queue)";
$db->txn_do(\&look_for_report);
$db->txn_do(\&look_for_update);
$0 = "fmsd";
sleep(5 + rand(10));
}
$pm->finish;
}
if (!keys %children) { # Very high load, something wrong
sleep(10);
next;
}
$pm->wait_for_available_procs;
}
sub look_for_report {
my $params = FixMyStreet::Script::Reports::construct_query($opts->debug);
my $unsent = FixMyStreet::DB->resultset('Problem')->search($params, {
for => \'UPDATE SKIP LOCKED',
rows => 1,
} )->single or return;
print_log('debug', "Trying to send report " . $unsent->id);
my $item = FixMyStreet::Queue::Item::Report->new(
report => $unsent,
verbose => $verbose,
nomail => $opts->nomail,
);
$item->process;
}
sub look_for_update {
my $updates = Open311::PostServiceRequestUpdates->new(
verbose => $verbose,
);
my $bodies = $updates->fetch_bodies;
my $params = $updates->construct_query($opts->debug);
my $comment = FixMyStreet::DB->resultset('Comment')
->to_body([ keys %$bodies ])
->search($params, { for => \'UPDATE SKIP LOCKED', rows => 1 })
->single or return;
print_log('debug', "Trying to send update " . $comment->id);
my ($body) = grep { $bodies->{$_} } @{$comment->problem->bodies_str_ids};
$body = $bodies->{$body};
$updates->process_update($body, $comment);
}
sub print_log {
my $prio = shift;
if ($changeverboselevel) {
$verbose = ($verbose + $changeverboselevel) % 3;
STDERR->print("fmsd: info: verbose level now $verbose\n");
$changeverboselevel = 0;
}
if ($verbose < 2) {
return if ($prio eq 'noise');
return if ($verbose < 1 && $prio eq 'debug');
return if ($verbose < 0 && $prio eq 'info');
}
STDERR->print("[fmsd] [$prio] ", join("", @_), "\n");
}
|