From f1fce37d5806f03af23cdc785a57a04b798fc9ab Mon Sep 17 00:00:00 2001 From: John Barrett Date: Fri, 13 Jun 2014 12:22:36 +0100 Subject: [PATCH] New script to "scrub" the envoy notice queue for a given notify cycle. This is basically the same as the envoy cron job, except it skips mailing. --- lib/DDGC/Web/Controller/Cronjob.pm | 50 +++++++++++++------------ script/ddgc_scrub_envoy_notify_cycle.pl | 32 ++++++++++++++++ 2 files changed, 58 insertions(+), 24 deletions(-) create mode 100755 script/ddgc_scrub_envoy_notify_cycle.pl diff --git a/lib/DDGC/Web/Controller/Cronjob.pm b/lib/DDGC/Web/Controller/Cronjob.pm index 279d68c23..0c51ee504 100644 --- a/lib/DDGC/Web/Controller/Cronjob.pm +++ b/lib/DDGC/Web/Controller/Cronjob.pm @@ -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; @@ -62,32 +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" ', - '[DuckDuckGo Community] '.$c->stash->{unsent_notifications_count}.' new notifications for you', - 'notifications', - $c->stash, - ); - } - catch { + unless ($skip_notify) { 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, + $c->d->postman->template_mail( + $user->data->{email}, '"DuckDuckGo Community Envoy" ', - '[DuckDuckGo Community] ERROR ON ENVOY', - $_, + '[DuckDuckGo Community] '.$c->stash->{unsent_notifications_count}.' new notifications for you', + 'notifications', + $c->stash, ); } catch { - $c->d->errorlog("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" ', + '[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) { diff --git a/script/ddgc_scrub_envoy_notify_cycle.pl b/script/ddgc_scrub_envoy_notify_cycle.pl new file mode 100755 index 000000000..cb57d8134 --- /dev/null +++ b/script/ddgc_scrub_envoy_notify_cycle.pl @@ -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 ; +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 );