universal isa is deprecated and throws compile error
[ITAG.git] / bin / itag_cron_email_notify.pl
blob079b69c968703a169af345078f207d17c9c964d0
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use English;
5 use Carp;
6 use FindBin;
7 use Getopt::Std;
9 use Mail::Sendmail;
11 use CXGN::IndexedLog;
12 use CXGN::ITAG::Pipeline;
13 use CXGN::Tools::List qw/distinct/;
15 #use Data::Dumper;
17 sub usage {
18 my $message = shift || '';
19 $message = "Error: $message\n" if $message;
20 die <<EOU;
21 $message
22 Usage:
23 $FindBin::Script -d itag_dir email_address
25 Scans ITAG pipeline repository for changes, emails the given email
26 address about them if it hasn't already.
28 Uses .email_log files in the pipeline repository to log what it's
29 already emailed about.
31 Options:
33 -d <dir>
34 REQUIRED: base directory to search for ITAG pipelines.
36 EOU
38 sub HELP_MESSAGE {usage()}
40 our %opt;
41 getopts('d:',\%opt) or usage();
42 $opt{d} or usage();
44 my ($itag_email) = @ARGV;
45 die "must provide an email address\n"
46 unless $itag_email;
47 die "invalid email address '$itag_email'\n"
48 unless $itag_email =~ /^\w+@[\w\.]+\w$/;
50 my $pipe = CXGN::ITAG::Pipeline->open( basedir => $opt{d} );
52 #open our email log
53 our $log = CXGN::IndexedLog->open( File => $pipe->email_log_filename );
55 #now go through all the things we might send an email about, check
56 #that we haven't already sent an email about it yet, then send a
57 #single email telling about all the things that happened to the
58 #pipeline
60 our @email_descriptions;
61 our @email_summaries;
62 our @log_appends;
64 sub send_if_needed {
65 my ($tagwords, $summary, $message, $logmessage) = @_;
67 $logmessage ||= $message;
68 $logmessage = "$tagwords $logmessage";
69 my %entry = $log->lookup( content => $tagwords );
71 return if %entry && $entry{content} eq $logmessage;
73 push @email_descriptions, $message;
74 push @email_summaries, $summary;
75 push @log_appends, $logmessage;
78 #send about a new pipeline
79 send_if_needed('NEW_PIPELINE '.$pipe->version, 'new pipeline version', "NEW PIPELINE VERSION ".$pipe->version." created");
81 #send about new batches
82 foreach my $bn ($pipe->list_batches) {
83 my $batch = $pipe->batch($bn);
84 my $seq_cnt = $batch->seqlist;
85 send_if_needed("NEW_BATCH $bn", 'new batch', "NEW BATCH $bn created containing $seq_cnt sequences");
87 #send about new analysis state changes
88 foreach my $tag ($pipe->list_analyses) {
89 my $an = $pipe->analysis($tag);
90 my $status = $an->status($batch);
91 my $pipever = $pipe->version;
92 if( $status eq 'ready' ) {
93 send_if_needed("ASTATE $bn:$tag",'analyses ready', "analysis $tag is ready to run in batch $bn","ready");
95 elsif( $status eq 'error' ) {
96 send_if_needed("ASTATE $bn:$tag",'errors', "analysis $tag has problems in batch $bn, see http://sgn.cornell.edu/sequencing/itag/status_html.pl?pipe=$pipever&batch=$bn for details","error");
101 if( @email_descriptions ) {
102 my $action_string = join '', map "* $_\n", @email_descriptions;
103 my $email_body = <<EOM;
104 This is the ITAG pipeline management system, informing you of the following:
106 $action_string
108 Regards,
110 The ITAG Pipeline Managment System
112 sendmail( From => 'itagpipeline@upload.sgn.cornell.edu',
113 To => $itag_email,
114 Subject => 'status - '.join(', ',distinct @email_summaries),
115 Body => $email_body,
117 # warn "sent email:\n".$email_body;
119 # else {
120 # warn "no changes detected, no email sent\n";
123 $log->append($_) foreach @log_appends;