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
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
package Bugzilla::Extension::Push::BacklogQueue;
use 5.10.1;
use strict;
use warnings;
use Bugzilla;
use Bugzilla::Extension::Push::BacklogMessage;
sub new {
my ($class, $connector) = @_;
my $self = {};
bless($self, $class);
$self->{connector} = $connector;
return $self;
}
sub count {
my ($self) = @_;
my $dbh = Bugzilla->dbh;
return $dbh->selectrow_array("
SELECT COUNT(*)
FROM push_backlog
WHERE connector = ?", undef, $self->{connector});
}
sub oldest {
my ($self) = @_;
my @messages = $self->list(
limit => 1,
filter => 'AND ((next_attempt_ts IS NULL) OR (next_attempt_ts <= NOW()))',
);
return scalar(@messages) ? $messages[0] : undef;
}
sub by_id {
my ($self, $id) = @_;
my @messages = $self->list(limit => 1, filter => "AND (log.id = $id)",);
return scalar(@messages) ? $messages[0] : undef;
}
sub list {
my ($self, %args) = @_;
$args{limit} ||= 10;
$args{filter} ||= '';
my @result;
my $dbh = Bugzilla->dbh;
my $filter_sql = $args{filter} || '';
my $sth = $dbh->prepare("
SELECT log.id, message_id, push_ts, payload, change_set, routing_key, attempt_ts, log.attempts
FROM push_backlog log
LEFT JOIN push_backoff off ON off.connector = log.connector
WHERE log.connector = ? " . $args{filter} . "
ORDER BY push_ts " . $dbh->sql_limit($args{limit}));
$sth->execute($self->{connector});
while (my $row = $sth->fetchrow_hashref()) {
push @result,
Bugzilla::Extension::Push::BacklogMessage->new({
id => $row->{id},
message_id => $row->{message_id},
push_ts => $row->{push_ts},
payload => $row->{payload},
change_set => $row->{change_set},
routing_key => $row->{routing_key},
connector => $self->{connector},
attempt_ts => $row->{attempt_ts},
attempts => $row->{attempts},
});
}
return @result;
}
#
# backoff
#
sub backoff {
my ($self) = @_;
if (!$self->{backoff}) {
my $ra
= Bugzilla::Extension::Push::Backoff->match({connector => $self->{connector}
});
if (@$ra) {
$self->{backoff} = $ra->[0];
}
else {
$self->{backoff}
= Bugzilla::Extension::Push::Backoff->create({connector => $self->{connector}
});
}
}
return $self->{backoff};
}
sub reset_backoff {
my ($self) = @_;
my $backoff = $self->backoff;
$backoff->reset();
$backoff->update();
return;
}
sub inc_backoff {
my ($self) = @_;
my $backoff = $self->backoff;
$backoff->inc();
$backoff->update();
return;
}
sub connector {
my ($self) = @_;
return $self->{connector};
}
1;
|