Back to index

nordugrid-arc-nox  1.1.0~rc6
bes.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "job.h"
00006 #include "grid_sched.h"
00007 
00008 namespace GridScheduler {
00009 
00010 Arc::MCC_Status 
00011 GridSchedulerService::CreateActivity(Arc::XMLNode& in, Arc::XMLNode& out) 
00012 {
00013     if (IsAcceptingNewActivities == false) {
00014         // send fault if we not accepting activity
00015         Arc::SOAPEnvelope fault(ns_, true);
00016         if (fault) {
00017             fault.Fault()->Code(Arc::SOAPFault::Sender);
00018             fault.Fault()->Reason("The BES is not currently accepting new activities.");
00019             Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:NotAcceptingNewActivities");
00020             out.Replace(fault.Child());
00021         } else {
00022             logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00023         }
00024         return Arc::MCC_Status();
00025     }
00026     
00027     std::string delegation;
00028     Arc::XMLNode delegated_token = in["deleg:DelegatedToken"];
00029     if (delegated_token) {
00030         // Client wants to delegate credentials
00031         if (!delegations_.DelegatedToken(delegation, delegated_token)) {
00032             logger_.msg(Arc::ERROR, "Failed to accept delegation");
00033             Arc::SOAPEnvelope fault(ns_, true);
00034             if (fault) {
00035                 fault.Fault()->Code(Arc::SOAPFault::Receiver);
00036                 fault.Fault()->Reason("Failed to accept delegation");
00037                 out.Replace(fault.Child());
00038             } else {
00039                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00040             }
00041             return Arc::MCC_Status();
00042         }
00043     }  
00044     
00045     Arc::XMLNode jsdl;
00046     for (int i = 0; (jsdl = in["ActivityDocument"][i]["JobDefinition"]) != false; i++) {
00047     /* 
00048     if (!jsdl) {
00049         logger_.msg(Arc::ERROR, "CreateActivity: no job description found");
00050         Arc::SOAPEnvelope fault(ns_, true);
00051         if (fault) {
00052             fault.Fault()->Code(Arc::SOAPFault::Sender);
00053             fault.Fault()->Reason("Can't find JobDefinition element in request");
00054             Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:InvalidRequestMessageFault");
00055             f.NewChild("bes-factory:InvalidElement") = "jsdl:JobDefinition";
00056             f.NewChild("bes-factory:Message") = "Element is missing";
00057             out.Replace(fault.Child());
00058         } else {
00059             logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00060         }
00061         return Arc::MCC_Status();
00062     } */
00063     
00064         // Create job
00065         Arc::JobRequest job_request(jsdl);
00066         Arc::JobSchedMetaData sched_meta;
00067         Arc::Job job(job_request, sched_meta);
00068 
00069         if (!job) {
00070             std::string failure = job.getFailure();
00071             logger_.msg(Arc::ERROR, "CreateActivity: Failed to create new job: %s", failure);
00072             Arc::SOAPEnvelope fault(ns_,true);
00073             if(fault) {
00074                 fault.Fault()->Code(Arc::SOAPFault::Receiver);
00075                 fault.Fault()->Reason("Can't create new activity: " + failure);
00076                 out.Replace(fault.Child());
00077             } else {
00078                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00079             }
00080             return Arc::MCC_Status();
00081         }
00082         
00083         // save job
00084         job.setStatus(Arc::JOB_STATUS_SCHED_NEW);
00085         jobq.refresh(job);
00086     
00087         // Make SOAP response
00088         Arc::WSAEndpointReference identifier(out.NewChild("bes-factory:ActivityIdentifier"));
00089         // Make job's ERP
00090         identifier.Address(endpoint); // address of service
00091         identifier.ReferenceParameters().NewChild("sched:JobID") = job.getID();
00092         identifier.ReferenceParameters().NewChild("a-rex:JobSessionDir")=endpoint+"/"+job.getID();
00093         identifier.ReferenceParameters().NewChild("a-rex:JobID") = job.getID();
00094         out.NewChild(in["ActivityDocument"]);
00095     }
00096     logger_.msg(Arc::VERBOSE, "CreateActivity finished successfully");
00097     return Arc::MCC_Status(Arc::STATUS_OK);
00098 }
00099 
00100 
00101 Arc::MCC_Status 
00102 GridSchedulerService::StartAcceptingNewActivities(Arc::XMLNode& /*in*/, Arc::XMLNode& /*out*/) 
00103 {
00104     IsAcceptingNewActivities = true;
00105     return Arc::MCC_Status(Arc::STATUS_OK);
00106 }
00107 
00108 Arc::MCC_Status 
00109 GridSchedulerService::StopAcceptingNewActivities(Arc::XMLNode& /*in*/, Arc::XMLNode& /*out*/) 
00110 {
00111     IsAcceptingNewActivities = false;
00112     return Arc::MCC_Status(Arc::STATUS_OK);
00113 }
00114 
00115 Arc::MCC_Status 
00116 GridSchedulerService::TerminateActivities(Arc::XMLNode &in, Arc::XMLNode &out) 
00117 {
00118     Arc::XMLNode id;
00119     for (int n = 0; (id = in["ActivityIdentifier"][n]) != false; n++) {
00120         Arc::XMLNode resp = out.NewChild("bes-factory:Response");
00121         resp.NewChild(id);
00122         std::string jobid = Arc::WSAEndpointReference(id).ReferenceParameters()["sched:JobID"];
00123         if (jobid.empty()) {
00124             // EPR is wrongly formated
00125             continue;
00126         }
00127         
00128         try {
00129             Arc::Job *j = jobq[jobid];
00130             j->setStatus(Arc::JOB_STATUS_SCHED_KILLING);
00131             jobq.refresh(*j); // save job
00132             delete j;
00133             resp.NewChild("bes-factory:Terminated") = "true";
00134         } catch(Arc::JobNotFoundException &e) {
00135             logger_.msg(Arc::ERROR, "GetActivityStatuses: job %s not found", jobid);
00136             Arc::SOAPEnvelope fault(ns_, true);
00137             if (fault) {
00138                 fault.Fault()->Code(Arc::SOAPFault::Sender);
00139                 fault.Fault()->Reason("Unknown activity");
00140                 Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:UnknownActivityIdentifierFault");
00141                 out.Replace(fault.Child());
00142             } else {
00143                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00144             }
00145             return Arc::MCC_Status();
00146         } catch (std::exception &e) {
00147             logger_.msg(Arc::ERROR, "Fatal error: %s", e.what());
00148             return Arc::MCC_Status();
00149         }
00150     }
00151     return Arc::MCC_Status(Arc::STATUS_OK);
00152 }
00153 
00154 Arc::MCC_Status 
00155 GridSchedulerService::GetActivityStatuses(Arc::XMLNode& in, Arc::XMLNode& out) 
00156 {
00157     Arc::XMLNode id;
00158     for (int n = 0; (id = in["ActivityIdentifier"][n]) != false; n++) {
00159         // Create place for response
00160         Arc::XMLNode resp = out.NewChild("bes-factory:Response");
00161         resp.NewChild(id);
00162         std::string jobid = Arc::WSAEndpointReference(id).ReferenceParameters()["sched:JobID"];
00163         if(jobid.empty()) {
00164             // EPR is wrongly formated
00165             continue;
00166         }
00167         try {
00168             Arc::Job *j = jobq[jobid];
00169             // Make response
00170             Arc::SchedJobStatus s = j->getStatus();
00171             Arc::XMLNode state = resp.NewChild("bes-factory:ActivityStatus");
00172             state.NewAttribute("bes-factory:state") = sched_status_to_string(s);
00173             Arc::JobSchedMetaData *m = j->getJobSchedMetaData();
00174             state.NewChild("sched:ResourceID") = m->getResourceID();
00175             Arc::Time &t1 = m->getCreatedTime();
00176             Arc::Time &t2 = m->getStartTime();
00177             Arc::Time &t3 = m->getEndTime();
00178             state.NewChild("sched:CreatedTime") = (std::string)t1;
00179             if (t1 != t2) {
00180                 state.NewChild("sched:StartTime") = (std::string)t2;
00181             }
00182             if (t3 > t2) {
00183                 state.NewChild("sched:EndTime") = (std::string)t3;
00184             }
00185             delete j;
00186         } catch (Arc::JobNotFoundException &e) {
00187             logger_.msg(Arc::ERROR, "GetActivityStatuses: job %s not found", jobid);
00188             Arc::SOAPEnvelope fault(ns_, true);
00189             if (fault) {
00190                 fault.Fault()->Code(Arc::SOAPFault::Sender);
00191                 fault.Fault()->Reason("Unknown activity");
00192                 Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:UnknownActivityIdentifierFault");
00193                 out.Replace(fault.Child());
00194             } else {
00195                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00196             }
00197             return Arc::MCC_Status();
00198         } catch (std::exception &e) {
00199             logger_.msg(Arc::ERROR, "Fatal error: %s", e.what());
00200             return Arc::MCC_Status();
00201         }
00202     }
00203     
00204     return Arc::MCC_Status(Arc::STATUS_OK);
00205 }
00206 
00207 Arc::MCC_Status 
00208 GridSchedulerService::ChangeActivityStatus(Arc::XMLNode& in, Arc::XMLNode& out) 
00209 {
00210     Arc::XMLNode id;
00211     for (int n = 0; (id = in["ActivityIdentifier"][n]) != false; n++) {
00212         std::string jobid = Arc::WSAEndpointReference(id).ReferenceParameters()["sched:JobID"];
00213         if (jobid.empty()) {
00214             // EPR is wrongly formated
00215             continue;
00216         }
00217         std::string old_state = (std::string)in["NewStatus"][n];
00218         std::string new_state = (std::string)in["OldStatus"][n];
00219         if (old_state.empty() || new_state.empty()) {
00220             // Not defined status
00221             continue;
00222         }
00223         try {
00224             Arc::Job *j = jobq[jobid];
00225             Arc::SchedJobStatus state = Arc::sched_status_from_string(new_state);
00226             j->setStatus(state);
00227             jobq.refresh(*j);
00228             // Create place for response
00229             Arc::XMLNode resp = out.NewChild("bes-factory:Response");
00230             resp.NewChild(id);
00231             Arc::XMLNode n_status = resp.NewChild("bes-factory:NewStatus");
00232             n_status = new_state;
00233             delete j;
00234         } catch (Arc::JobNotFoundException &e) {
00235             logger_.msg(Arc::ERROR, "ChangeActivityStatuses: job %s not found", jobid);
00236             Arc::SOAPEnvelope fault(ns_, true);
00237             if (fault) {
00238                 fault.Fault()->Code(Arc::SOAPFault::Sender);
00239                 fault.Fault()->Reason("Unknown activity");
00240                 Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:UnknownActivityIdentifierFault");
00241                 out.Replace(fault.Child());
00242             } else {
00243                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00244             }
00245             return Arc::MCC_Status();
00246         } catch (std::exception &e) {
00247             logger_.msg(Arc::ERROR, "Fatal error: %s", e.what());
00248             return Arc::MCC_Status();
00249         }
00250     }
00251     return Arc::MCC_Status(Arc::STATUS_OK);
00252 }
00253 
00254 Arc::MCC_Status 
00255 GridSchedulerService::GetActivityDocuments(Arc::XMLNode &in, Arc::XMLNode &out) 
00256 {
00257     Arc::XMLNode id;
00258     for (int n = 0; (id = in["ActivityIdentifier"][n]) != false; n++) {
00259         // Create place for response
00260         Arc::XMLNode resp = out.NewChild("bes-factory:Response");
00261         resp.NewChild(id);
00262         std::string jobid = Arc::WSAEndpointReference(id).ReferenceParameters()["sched:JobID"];
00263         if (jobid.empty()) {
00264             // wrong ERP
00265             continue;
00266         }
00267         try {
00268             // Read JSDL of job
00269             Arc::XMLNode jsdl = resp.NewChild("bes-factory:JobDefinition");
00270             Arc::Job *j = jobq[jobid];
00271             jsdl = j->getJSDL();
00272             jsdl.Name("bes-factory:JobDefinition"); // Recovering namespace of element
00273             delete j;
00274         } catch (Arc::JobNotFoundException &e) {
00275             logger_.msg(Arc::ERROR, "GetActivityDocuments: job %s not found", jobid);
00276             Arc::SOAPEnvelope fault(ns_, true);
00277             if (fault) {
00278                 fault.Fault()->Code(Arc::SOAPFault::Sender);
00279                 fault.Fault()->Reason("Unknown activity");
00280                 Arc::XMLNode f = fault.Fault()->Detail(true).NewChild("bes-factory:UnknownActivityIdentifierFault");
00281                 out.Replace(fault.Child());
00282             } else {
00283                 logger_.msg(Arc::ERROR, "Cannot create SOAP fault");
00284             }
00285             return Arc::MCC_Status();
00286         } catch (std::exception &e) {
00287             logger_.msg(Arc::ERROR, "Fatal error: %s", e.what());
00288             return Arc::MCC_Status();
00289         }
00290     }
00291     return Arc::MCC_Status(Arc::STATUS_OK);
00292 }
00293 
00294 } // namespace 
00295