summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/lib/Reports/EmailQueue.pm
blob: 1bf2ca0039ef67029b6917d0eee3c9aa8146522b (plain)
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
# 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::BMO::Reports::EmailQueue;
use strict;
use warnings;

use Bugzilla::Error;
use Scalar::Util qw(blessed);
use Storable ();

sub report {
    my ($vars, $filter) = @_;
    my $dbh = Bugzilla->dbh;
    my $user = Bugzilla->user;

    $user->in_group('admin') || $user->in_group('infra')
        || ThrowUserError('auth_failure', { group  => 'admin',
                                            action => 'run',
                                            object => 'email_queue' });

    my $query = "
        SELECT j.jobid,
               j.arg,
               j.insert_time,
               j.run_after AS run_time,
               COUNT(e.jobid) AS error_count,
               MAX(e.error_time) AS error_time,
               e.message AS error_message
          FROM ts_job j
               LEFT JOIN ts_error e ON e.jobid = j.jobid
      GROUP BY j.jobid
      ORDER BY j.run_after";

    $vars->{'jobs'} = $dbh->selectall_arrayref($query, { Slice => {} });
    foreach my $job (@{ $vars->{'jobs'} }) {
        eval {
            my $msg = _cond_thaw(delete $job->{'arg'})->{msg};
            if (ref($msg) && blessed($msg) eq 'Email::MIME') {
                $job->{'subject'} = $msg->header('subject');
            } else {
                ($job->{'subject'}) = $msg =~ /\nSubject: ([^\n]+)/;
            }
        };
    }
    $vars->{'now'} = (time);
}

sub _cond_thaw {
    my $data = shift;
    my $magic = eval { Storable::read_magic($data); };
    if ($magic && $magic->{major} && $magic->{major} >= 2 && $magic->{major} <= 5) {
        my $thawed = eval { Storable::thaw($data) };
        if ($@) {
            # false alarm... looked like a Storable, but wasn't.
            return $data;
        }
        return $thawed;
    } else {
        return $data;
    }
}


1;