Back to index

nordugrid-arc-nox  1.1.0~rc6
send_mail.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 #include <string>
00005 
00006 #include <arc/Logger.h>
00007 #include "../files/info_types.h"
00008 #include "../files/info_files.h"
00009 #include "../run/run_parallel.h"
00010 #include "../conf/environment.h"
00011 #include "send_mail.h"
00012 
00013 
00014 static Arc::Logger& logger = Arc::Logger::getRootLogger();
00015 
00016 /* check if have to send mail and initiate sending */
00017 bool send_mail(const JobDescription &desc,JobUser &user) {
00018   char flag = states_all[desc.get_state()].mail_flag;
00019   if(flag == ' ') return true;
00020   std::string notify = "";
00021   std::string jobname = "";
00022   JobLocalDescription *job_desc = desc.get_local();
00023   if(job_desc == NULL) {
00024     job_desc = new JobLocalDescription;
00025     if(!job_local_read_file(desc.get_id(),user,*job_desc)) {
00026       logger.msg(Arc::ERROR,"Failed reading local information");
00027       delete job_desc; job_desc=NULL;
00028     };
00029   };
00030   if(job_desc != NULL) {
00031     jobname=job_desc->jobname;
00032     notify=job_desc->notify;
00033     if(desc.get_local() == NULL) { delete job_desc; };
00034   };
00035 //  job_local_read_notify(desc.get_id(),user,notify);
00036   if(notify.length() == 0) return true; /* save some time */
00037   Arc::Run* child = NULL;
00038   std::string failure_reason=desc.GetFailure();
00039   if(job_failed_mark_check(desc.get_id(),user)) {
00040     if(failure_reason.length() == 0) failure_reason="<unknown>";
00041   };
00042   for(std::string::size_type n=0;;) {
00043     n=failure_reason.find('\n',n);
00044     if(n == std::string::npos) break;
00045     failure_reason[n]='.';
00046   };
00047   std::string cmd(nordugrid_libexec_loc()+"/smtp-send.sh");
00048   std::string from_addr = support_mail_address();
00049   char* args[11] ={ /* max 3 mail addresses */
00050        (char*)cmd.c_str(),
00051        (char*)states_all[desc.get_state()].name,
00052        (char*)desc.get_id().c_str(),
00053        (char*)user.ControlDir().c_str(),
00054        (char*)from_addr.c_str(),
00055        (char*)jobname.c_str(),
00056        (char*)failure_reason.c_str(),
00057        NULL,
00058        NULL,
00059        NULL,
00060        NULL
00061   };
00062   /* go through mail addresses and flags */
00063   std::string::size_type pos=0;
00064   std::string::size_type pos_s=0;
00065   std::string mails[3];
00066   int mail_n=0;
00067   bool right_flag = false;
00068   /* by default mail is sent when job enters states PREPARING and FINISHED */
00069   if((flag == 'b') || (flag == 'e')) right_flag=true;
00070   for(;;) {
00071     if(pos_s >= notify.length()) break;
00072     if((pos = notify.find(' ',pos_s)) == std::string::npos) pos=notify.length();
00073     if(pos==pos_s) { pos++; pos_s++; continue; };
00074     std::string word(notify.substr(pos_s,pos-pos_s));
00075     if(word.find('@') == std::string::npos) { /* flags */
00076       if(word.find(flag) == std::string::npos) { right_flag=false; }
00077       else { right_flag=true; };
00078       pos_s=pos+1;
00079       continue;
00080     };
00081     if(right_flag) { mails[mail_n]=word; mail_n++; };
00082     if(mail_n >= 3) break;
00083     pos_s=pos+1;
00084   };
00085   if(mail_n == 0) return true; /* not sending to anyone */
00086   for(;mail_n>=0;mail_n--) {
00087     args[7+mail_n]=(char*)(mails[mail_n].c_str());
00088   };
00089   if(!RunParallel::run(user,desc,args,&child)) {
00090     logger.msg(Arc::ERROR,"Failed running mailer");
00091     return false;
00092   };
00093   delete child;
00094   return true;
00095 }