Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #166 from jbarrett/queue-scrubber
Browse files Browse the repository at this point in the history
- New script to clear notification queues
- Logger additions
  • Loading branch information
jbarrett committed Jun 13, 2014
2 parents 6faac67 + f1fce37 commit 9e3c1a3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
10 changes: 9 additions & 1 deletion lib/DDGC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use LWP::UserAgent;
use Carp;
use Data::Dumper;
use String::Truncate 'elide';
use feature qw/ state /;
use Time::Piece;
use namespace::autoclean;

our $VERSION ||= '0.000';
Expand Down Expand Up @@ -671,7 +673,13 @@ sub as {

sub errorlog {
my ( $self, $msg ) = @_;
io($self->config->errorlog)->append($msg . "\n");
state $counter = 0;
my $t = localtime;
my $log_id = $$ . $t->epoch . sprintf("%09d", ++$counter);
my ($package, $filename, $line) = caller(1);
my $log_line = "$log_id " . $t->datetime . " - $msg @ $filename:$line\n";
io($self->config->errorlog)->append($log_line);
return $log_id;
}

sub update_password {
Expand Down
51 changes: 26 additions & 25 deletions lib/DDGC/Web/Controller/Cronjob.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ sub base :Chained('/base') :PathPart('cronjob') :CaptureArgs(0) {
}

sub index :Chained('base') :PathPart('') :Args {
my ( $self, $c, @args ) = @_;
if ($args[0] eq 'notify_cycle') {
$self->notify_cycle($c,$args[1]);
my ( $self, $c, %args ) = @_;
if ($args{'notify_cycle'}) {
$self->notify_cycle($c,$args{'notify_cycle'}, $args{'scrub'});
}
$c->response->body('OK');
}

sub notify_cycle {
my ( $self, $c, $cycle ) = @_;
my ( $self, $c, $cycle, $skip_notify ) = @_;
$c->stash->{c} = $c;
$c->stash->{u} = sub {
my @args;
Expand Down Expand Up @@ -62,33 +62,34 @@ sub notify_cycle {
$c->d->as($user,sub {
$c->stash->{unsent_notifications_results} = [$user->unsent_notifications_cycle($cycle)->all];
$c->stash->{unsent_notifications_count} = scalar @{$c->stash->{unsent_notifications_results}};
try {
$c->d->postman->template_mail(
$user->data->{email},
'"DuckDuckGo Community Envoy" <[email protected]>',
'[DuckDuckGo Community] '.$c->stash->{unsent_notifications_count}.' new notifications for you',
'notifications',
$c->stash,
);
}
catch {
unless ($skip_notify) {
try {
$c->d->errorlog((caller(0))[3] . ":" . (caller(0))[2] . " - Mailing notifications to " .
$user->data->{email} . " failed, mailing " . $c->d->config->error_email);
$c->d->postman->mail(
$c->d->config->error_email,
$c->d->postman->template_mail(
$user->data->{email},
'"DuckDuckGo Community Envoy" <[email protected]>',
'[DuckDuckGo Community] ERROR ON ENVOY',
$_,
'[DuckDuckGo Community] '.$c->stash->{unsent_notifications_count}.' new notifications for you',
'notifications',
$c->stash,
);
}
catch {
$c->d->errorlog((caller(0))[3] . ":" . (caller(0))[2] .
" - Failed to mail error report about mailing " .
$c->stash->{unsent_notifications_count} . " notifications to " .
$user->data->{email});
try {
$c->d->errorlog("Mailing notifications to " .
$user->data->{email} . " failed, mailing " . $c->d->config->error_email);
$c->d->postman->mail(
$c->d->config->error_email,
'"DuckDuckGo Community Envoy" <[email protected]>',
'[DuckDuckGo Community] ERROR ON ENVOY',
$_,
);
}
catch {
$c->d->errorlog("Failed to mail error report about mailing " .
$c->stash->{unsent_notifications_count} . " notifications to " .
$user->data->{email});
};
};
};
}
my @ids;
for (@{$c->stash->{unsent_notifications_results}}) {
for ($_->event_notifications) {
Expand Down
6 changes: 3 additions & 3 deletions lib/DDGC/Web/Controller/Duckpan.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ sub upload :Chained('logged_in') :Args(0) {
$c->add_bc('Upload');
if (!$c->user) {
$c->res->code(403);
$c->d->errorlog((caller(0))[3] . ":" . (caller(0))[2] . " - No user");
$c->d->errorlog("No user");
$c->stash->{no_user} = 1;
return $c->detach;
}
Expand All @@ -104,12 +104,12 @@ sub upload :Chained('logged_in') :Args(0) {
} else {
$c->stash->{duckpan_error} = $return || "Let's just say something broke.";
$c->res->code(403);
$c->d->errorlog((caller(0))[3] . ":" . (caller(0))[2] . " - " . $c->stash->{duckpan_error});
$c->d->errorlog($c->stash->{duckpan_error});
}
};
if ($@) {
$c->res->code(403);
$c->d->errorlog((caller(0))[3] . ":" . (caller(0))[2] . " - DuckPAN 403 - " . $@);
$c->d->errorlog("DuckPAN 403 - " . $@);
$c->stash->{duckpan_error} = $@;
}
if (defined $c->stash->{duckpan_error}) {
Expand Down
32 changes: 32 additions & 0 deletions script/ddgc_scrub_envoy_notify_cycle.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env perl

$|=1;

use FindBin;
use lib $FindBin::Dir . "/../lib";

use strict;
use warnings;

use DDGC::Config;

my $cycle = shift @ARGV;

die "Need cycle!" unless $cycle;
die "Unknown cycle!" unless $cycle >= 1 && $cycle <= 4;

print "This will CLEAR ALL pending notifications from cycle $cycle, continue (y/n)? ";
my $continue = lc <STDIN>;
die "Bailing out" unless ($continue =~ /^y/);

$ENV{DDGC_EXECUTE_CRONJOBS} = 'YES';

my $base = DDGC::Config->new->web_base;

use Catalyst::Test qw( DDGC::Web );

my $response = request($base.'/cronjob/notify_cycle/'.$cycle."/scrub/1");

print $response->content;

exit ( $response->is_success ? 0 : 1 );

0 comments on commit 9e3c1a3

Please sign in to comment.