forked from openSUSE/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhermesworker.pl
executable file
·220 lines (183 loc) · 6.71 KB
/
hermesworker.pl
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl -w
#
# Copyright (c) 2008 Klaas Freitag <freitag@suse.de>, Novell Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
# Contributors:
# Klaas Freitag <freitag@suse.de>
#
# This script diggs through the hermes database and sends the due
# messages, sleeping again for a short time.
BEGIN {
my ($wd) = $0 =~ m-(.*)/- ;
$wd ||= '.';
chdir "$wd";
unshift @INC, ".";
}
use strict;
use Getopt::Std;
use Hermes::MessageSender;
use Hermes::DB;
use Hermes::Util;
# use Hermes::Delivery::Jabber;
use Time::HiRes qw( gettimeofday tv_interval );
use Hermes::Log;
use vars qw ( $opt_h $opt_c $opt_d $opt_t $opt_o $opt_m $gotTermSignal );
sub gotSignalTerm
{
$gotTermSignal = 1;
}
sub usage()
{
print<<END
hermesworker.pl
Script to send out all kinds of hermes messages. It has to run
regularly started with the -o option or it runs forever in a loop.
To stop it smoothly just send a TERM signal.
-o: send only immediate messages once and stop after that
-m: send only minute digests and stop after that
-t: database name as of the Config.pm file
-h: help text
-d: switch on debug
-c: give some output on console
END
;
exit;
}
# ---------------------------------------------------------------------------
# Process the commandline arguments.
getopts('omdcht:');
setLogFileName('hermesworker');
usage() if ($opt_h );
connectDB( $opt_t );
$gotTermSignal = 0;
$SIG{TERM} = \&gotSignalTerm;
my $debug = 0;
if( $opt_d ) {
$debug = 1;
$Hermes::Config::Debug = 2;
}
if( $Hermes::Config::WorkerInitJabber ) {
Hermes::Delivery::Jabber::initCommunication();
Hermes::Delivery::Jabber::sendJabber( { subject => "Hello World", body => 'Hermes talks to you' } );
}
# Sending time for daily digests, defaults to midnight
my $dailyHour = $Hermes::Config::DailySendHour || 0;
my $dailyMin = $Hermes::Config::DailySendHourMinute || 0;
my $weekDay = $Hermes::Config::SendWeekDay || 0;
my ($t0, $elapsed);
my $cnt;
print "hermesworker started\n" if( $opt_c );
if( $opt_m ) {
my $notificationIdsRef = sendMessageDigest( SendMinutely() );
$cnt = @{$notificationIdsRef};
print "Sent $cnt messages (minute digests)\n" if( $opt_c );
foreach my $notiId ( @{$notificationIdsRef} ) {
log('info', "Sent notification <$notiId>" );
}
if( $Hermes::Config::WorkerInitJabber ) {
Hermes::Delivery::Jabber::quitCommunication();
}
exit;
}
my $lastMinutely = 0;
my $lastHourly = 0;
my $lastDaily = 0;
my $lastWeekly = 0;
my %digeststosend;
while( 1 ) {
# First, send out the messages that were marked with sendImmediately.
$t0 = [gettimeofday];
$cnt = sendImmediateMessages();
$elapsed = tv_interval ($t0);
log 'info', "Sent due messages: $cnt in $elapsed sec.";
print "Sent immediate due messages: $cnt in $elapsed sec.\n" if( $opt_c );
exit if( $opt_o );
my @ids = keys %digeststosend;
log 'info', @ids . " digests left to send";
# lets have a check for new stuff every 15 seconds
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $interval = 0;
$interval = 15-$sec if( $sec >=0 && $sec < 15 );
$interval = 30-$sec if( $sec >=15 && $sec < 30 );
$interval = 45-$sec if( $sec >=30 && $sec < 45 );
$interval = 60-$sec if( $sec >=45 && $sec < 60 );
if (@ids) {
my $cid = shift @ids;
my $ret = sendOneMessageDigest($cid);
log 'info', "sendOneMessageDigest $cid return $ret";
delete $digeststosend{$cid} if ($ret);
} else {
log( 'info', "Sleeping for $interval seconds" );
print "Now sleeping for $interval seconds, current second is $sec.\n" if( $opt_c );
sleep( $interval );
}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ctime = time;
my $dtime = $ctime - $lastMinutely;
if( (0+$sec >= 0 && 0+$sec < 10 && $dtime > 10 ) || ($dtime > 65)) {
$t0 = [gettimeofday];
my $notificationIdsRef = getDigestSubscribtions( SendMinutely() );
$cnt = @{$notificationIdsRef};
foreach my $id (@{$notificationIdsRef}) { $digeststosend{$id} = 1; }
$elapsed = tv_interval( $t0 );
log 'info', "Checked Minute digests at <$min/$sec>: $cnt in $elapsed sec.";
$lastMinutely = $ctime;
}
$dtime = $ctime - $lastHourly;
if ( ( 0+$sec >= 0 && 0+$sec < 10 && 0+$min == 0 && $dtime > 10) || ($dtime > 3700)) {
$t0 = [gettimeofday];
my $notificationIdsRef = getDigestSubscribtions( SendHourly() );
$cnt = @{$notificationIdsRef};
foreach my $id (@{$notificationIdsRef}) { $digeststosend{$id} = 1; }
$elapsed = tv_interval($t0);
log 'info', "Checked Hourly digest at <$min/$sec>: $cnt in $elapsed sec.";
$lastHourly = $ctime;
}
$dtime = $ctime - $lastDaily;
if ( ( 0+$sec >= 0 && 0+$sec < 10 && 0+$hour == $dailyHour && 0+$min == $dailyMin && $dtime > 10)
|| ($dtime > 3600 * 24 + 100))
{
$t0 = [gettimeofday];
my $notificationIdsRef = getDigestSubscribtions( SendDaily() );
$cnt = @{$notificationIdsRef};
foreach my $id (@{$notificationIdsRef}) { $digeststosend{$id} = 1; }
$elapsed = tv_interval($t0);
log 'info', "Checked Daily Digest at <$hour/$min/$sec>: $cnt in $elapsed sec.";
$lastDaily = $ctime;
}
$dtime = $ctime - $lastWeekly;
if( ( 0+$sec >= 0 && 0+$sec < 10 && 0+$hour == $dailyHour && 0+$min == $dailyMin && 0+$weekDay == 0+$wday && $dtime > 10 )
|| ($dtime > 3600 * 24 * 7 + 100))
{ # it's sunday and we send the weekly digest
$t0 = [gettimeofday];
my $notificationIdsRef = getDigestSubscribtions( SendWeekly() );
$cnt = @{$notificationIdsRef};
foreach my $id (@{$notificationIdsRef}) { $digeststosend{$id} = 1; }
$elapsed = tv_interval($t0);
log 'info', "Checked Weekly Digest at <$hour/$min/$sec>: $cnt in $elapsed sec.";
$lastWeekly = $ctime;
}
if( $gotTermSignal ) {
log 'info', "Got the term signal, I go outta here...";
print "## Got the term signal, I go outta here...\n" if( $opt_c );
exit 0;
}
}
if( $Hermes::Config::WorkerInitJabber ) {
Hermes::Delivery::Jabber::quitCommunication();
}