ensembl-hive  2.6
NotifyByEmail.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9 This is a RunnableDB module that implements Bio::EnsEMBL::Hive::Process interface
10 and is ran by Workers during the execution of eHive pipelines.
11 It is not generally supposed to be instantiated and used outside of this framework.
12 
13 Please refer to Bio::EnsEMBL::Hive::Process documentation to understand the basics of the RunnableDB interface.
14 
15 Please refer to Bio::EnsEMBL::Hive::PipeConfig::* pipeline configuration files to understand how to configure pipelines.
16 
17 =head1 DESCRIPTION
18 
19 This RunnableDB module will send you a short notification email message per
20 each job. You can either dataflow into it, or simply create standalone
21 jobs.
22 
23 The main body of the email is expected in the "text" parameter. If the
24 "is_html" parameter is set, the body is expected to be in HTML.
25 
26 Attachments such as diagrams, images, PDFs have to be listed in the
27 'attachments' parameter.
28 
29 C<format_table> provides a simple method to stringify a table of data. If
30 you need more options to control the separators, the alignment, etc, have a
31 look at the very comprehensive L<Text::Table>.
32 
33 Note: this module uses L<Email::Sender> to send the email, which by default
34 uses C<sendmail> but has other backends configured. As such, it depends
35 heavily on the implementation of your compute farm. Please make sure it
36 works as intended before using this module in complex pipelines.
37 
38 =head1 LICENSE
39 
40  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
41  Copyright [2016-2024] EMBL-European Bioinformatics Institute
42 
43  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
44  You may obtain a copy of the License at
45 
46  http://www.apache.org/licenses/LICENSE-2.0
47 
48  Unless required by applicable law or agreed to in writing, software distributed under the License
49  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50  See the License for the specific language governing permissions and limitations under the License.
51 
52 =head1 CONTACT
53 
54  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
55 
56 =cut
57 
58 
59 package Bio::EnsEMBL::Hive::RunnableDB::NotifyByEmail;
60 
61 use strict;
62 use warnings;
63 
64 use Email::Stuffer;
65 
66 use base ('Bio::EnsEMBL::Hive::Process');
67 
68 sub param_defaults {
69  return {
70  'is_html' => 0,
71  'subject' => 'An automatic message from your pipeline',
72  'attachments' => [],
73  };
74 }
75 
76 
77 =head2 fetch_input
78 
79  Description : Implements fetch_input() interface method of Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
80  Here we have nothing to do.
81 
82 =cut
83 
84 sub fetch_input {
85 }
86 
87 =head2 run
88 
89  Description : Implements run() interface method of Bio::EnsEMBL::Hive::Process that is used to perform the main bulk of the job (minus input and output).
90  Here the actual sending of the email message happens in run() though one may argue it is technically 'output'.
91 
92  param('email'): The email address to send the message to.
93 
94  param('subject'): The (optional) 'Subject:' line.
95 
96  param('text'): Text of the email message. It will undergo parameter substitution.
97 
98  param('is_html'): Boolean. Whether the content of 'text' is in HTML
99 
100  param('attachments'): Array of paths for files to attach.
101 
102  param('*'): Any other parameters can be freely used for parameter substitution.
103 
104 =cut
105 
106 sub run {
107  my $self = shift;
108 
109  my $email = $self->param_required('email');
110  my $subject = $self->param_required('subject');
111  my $text = $self->param_required('text');
112  my $attachments = $self->param('attachments');
113 
114  my $msg = Email::Stuffer->from($email)
115  ->to($email)
116  ->subject($subject);
117 
118  if ($self->param('is_html')) {
119  $msg->html_body($text);
120  } else {
121  $msg->text_body($text);
122  }
123 
124  if ($attachments and @$attachments) {
125  $msg->attach_file($_) for @$attachments;
126  }
127 
128  $msg->send();
129 }
130 
131 =head2 write_output
132 
133  Description : Implements write_output() interface method of Bio::EnsEMBL::Hive::Process that is used to deal with job's output after the execution.
134  Here we have nothing to do.
135 
136 =cut
137 
138 sub write_output {
139 }
140 
141 
142 
143 ######################
144 ## Internal methods ##
145 ######################
146 
147 # Present data in a nice table, like what mySQL does.
148 
149 
150 =head2 format_table
151 
152 The same type of table could be generated with L<Text::Table>:
153 
154  my $first_column_name = shift @$columns;
155  my $tb = Text::Table->new(\'| ', $first_column_name, (map { +(\' | ', $_) } @$columns), \' |',);
156  $tb->load(@$results);
157  my $rule = $tb->rule('-', '+');
158  return $title . "\n" . $rule . $tb->title() . $rule . $tb->body() . $rule;
159 
160 =cut
161 
162 sub format_table {
163  my ($self, $title, $columns, $results) = @_;
164 
165  my @lengths;
166  foreach (@$columns) {
167  push @lengths, length($_) + 2;
168  }
169 
170  foreach (@$results) {
171  for (my $i=0; $i < scalar(@$_); $i++) {
172  my $len = length($$_[$i] // 'N/A') + 2;
173  $lengths[$i] = $len if $len > $lengths[$i];
174  }
175  }
176 
177  my $table = "$title\n";
178  $table .= '+'.join('+', map {'-' x $_ } @lengths).'+'."\n";
179 
180  for (my $i=0; $i < scalar(@lengths); $i++) {
181  my $column = $$columns[$i];
182  my $padding = $lengths[$i] - length($column) - 2;
183  $table .= '| '.$column.(' ' x $padding).' ';
184  }
185 
186  $table .= '|'."\n".'+'.join('+', map {'-' x $_ } @lengths).'+'."\n";
187 
188  foreach (@$results) {
189  for (my $i=0; $i < scalar(@lengths); $i++) {
190  my $value = $$_[$i] // 'N/A';
191  my $padding = $lengths[$i] - length($value) - 2;
192  $table .= '| '.$value.(' ' x $padding).' ';
193  }
194  $table .= '|'."\n"
195  }
196 
197  $table .= '+'.join('+', map {'-' x $_ } @lengths).'+'."\n";
198 
199  return $table;
200 }
201 
202 1;
map
public map()
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Process
Definition: Process.pm:77
Bio::EnsEMBL::Hive::PipeConfig
Definition: EnsemblGeneric_conf.pm:5
main
public main()
Bio::EnsEMBL::Hive::RunnableDB::NotifyByEmail
Definition: NotifyByEmail.pm:40
run
public run()
Bio::EnsEMBL::Hive
Definition: Hive.pm:38
Bio
Definition: AltAlleleGroup.pm:4