Back to index

nordugrid-arc-nox  1.1.0~rc6
job.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <glibmm.h>
00006 #include "job.h"
00007 #include <arc/GUID.h>
00008 #include <arc/StringConv.h>
00009 #include <iostream>
00010 #include <fstream>
00011 #include <unistd.h>
00012 
00013 namespace Paul {
00014 
00015 Job::Job(void) 
00016 {
00017     finished_reported = false;
00018     timeout = 5;
00019     check = 0;
00020 }
00021 
00022 Job::Job(const Job &j)
00023 {
00024     request = j.request;
00025     sched_meta = j.sched_meta;
00026     timeout = j.timeout;
00027     id = j.id;
00028     db = j.db;
00029     check = j.check;
00030     finished_reported = false;
00031 }
00032 
00033 Job::Job(JobRequest &r)
00034 {
00035     request = r;
00036     finished_reported = false;
00037 }
00038 
00039 Job::Job(JobRequest &r, JobSchedMetaData &m, int t, const std::string &db_path) 
00040 {
00041     request = r;
00042     sched_meta = m;
00043     timeout = t;
00044     id = Arc::UUID();
00045     db = db_path;
00046     check = 0;
00047     finished_reported = false;
00048 }
00049 
00050 Job::Job(const std::string &jobid, const std::string &db_path) 
00051 {
00052     id = jobid;
00053     db = db_path;
00054     check = 0;
00055     timeout = 5;
00056     finished_reported = false;
00057 }
00058 
00059 Job::Job(std::istream &job, const std::string &db_path) 
00060 {
00061     db = db_path;
00062     std::string xml_document;
00063     std::string xml_line;
00064     Arc::XMLNode tmp_xml;
00065     check = 0;
00066     timeout = 5;
00067 
00068     while (getline(job, xml_line)) {
00069         xml_document += xml_line;
00070     }
00071     (Arc::XMLNode (xml_document)).New(tmp_xml);
00072     JobRequest job_desc(tmp_xml);
00073     setJobRequest(job_desc);
00074 }
00075 
00076 Job::~Job(void) 
00077 {
00078     // NOP
00079 }
00080 
00081 bool Job::CheckTimeout(void) 
00082 {
00083     check++;
00084     if (check < timeout) {
00085         return true;
00086     }
00087     
00088     check = 0;
00089     return false;
00090 }
00091 
00092 bool Job::Cancel(void) 
00093 {
00094     status = KILLED;
00095     return true;
00096 }
00097 
00098 inline void write_pair(std::ofstream &f, std::string name,std::string &value) 
00099 {
00100     f << name << '=' << value << std::endl;
00101 }
00102 
00103 bool Job::save(void) 
00104 { 
00105     // write out the jobrequest
00106     Arc::XMLNode jsdl = getJSDL();
00107     std::string jsdl_str;
00108     std::string fname = db + "/" + id + ".jsdl";
00109     jsdl.GetXML(jsdl_str);
00110     std::ofstream f1(fname.c_str(), std::ios::out | std::ios::trunc);
00111     if(!f1.is_open()) {
00112         return false; /* can't open file */
00113     }
00114     f1 << jsdl_str;
00115     f1.close();
00116     // write out job metadata
00117     fname = db + "/" + id + ".metadata";
00118     std::ofstream f2(fname.c_str(), std::ios::out | std::ios::trunc);
00119     if(!f2.is_open()) {
00120         return false; /* can't open file */
00121     }
00122     write_pair(f2, "id", id);
00123     std::string arex_id = getResourceID();
00124     write_pair(f2, "arex_id", arex_id);
00125     std::string status_str = sched_status_to_string(status);
00126     write_pair(f2, "status", status_str);
00127     f2.close();
00128     
00129     // write out arex_job_id
00130     fname = db + "/" + id + ".arex_job_id";
00131     jsdl.GetXML(jsdl_str);
00132     std::ofstream f3(fname.c_str(), std::ios::out | std::ios::trunc);
00133     if(!f3.is_open()) {
00134         return false; /* can't open file */
00135     }
00136     f3 << sched_meta.getResourceID();
00137     f3.close();
00138 
00139     return true;
00140 }
00141 
00142 bool cut(std::string &input, std::string &name, std::string &value) 
00143 {
00144     int size = input.size();
00145     int i = input.find_first_of("=");
00146     if (i == std::string::npos) {
00147         return false;
00148     }
00149     name = input.substr(0, i);
00150     value = input.substr(i+1, size);
00151     return true;
00152 }
00153 
00154 bool Job::load(void) 
00155 {
00156     char buf[250];
00157     std::string fname = db + "/" + id + ".metadata";
00158     std::ifstream f(fname.c_str());
00159     if (!f.is_open()) {
00160         return false;
00161     }
00162     for (;!f.eof();) {
00163         f.getline(buf, 250);
00164         std::string line(buf);
00165         std::string name;
00166         std::string value;
00167         if (!cut(line,name,value)) {
00168             continue;
00169         }
00170         if (name == "id") {
00171             id = value;
00172         } else if (name == "arex_id") {
00173             sched_meta.setResourceID(value);
00174         } else if (name == "status") {
00175             status = sched_status_from_string(value);
00176         }
00177     }
00178     f.close(); 
00179 
00180     // read jsdl
00181     std::string fname_jsdl = db + "/" + id + ".jsdl";
00182     std::ifstream f_jsdl(fname_jsdl.c_str());
00183     std::string xml_document;
00184     std::string xml_line;
00185     Arc::XMLNode tmp_xml;
00186     while (getline(f_jsdl, xml_line)) {
00187         xml_document += xml_line;
00188     }
00189     (Arc::XMLNode (xml_document)).New(tmp_xml);
00190     f_jsdl.close();
00191     JobRequest job_desc(tmp_xml);
00192     setJobRequest(job_desc);
00193     //read arex_job_id
00194     std::string a_id = db + "/" + id + ".arex_job_id";
00195     std::ifstream f_arex(a_id.c_str());
00196     std::string line, tmp;
00197     while (getline(f_arex, line)) {
00198         tmp += line;
00199     }
00200     sched_meta.setResourceID(tmp);
00201 
00202     f_arex.close();
00203     return true;
00204 }
00205 
00206 bool Job::remove(void) 
00207 {
00208     std::string file1 = db + "/" + id + ".metadata";
00209     std::string file2 = db + "/" + id + ".jsdl";
00210     std::string file3 = db + "/" + id + ".arex_job_id";
00211     std::remove(file1.c_str());
00212     std::remove(file2.c_str());
00213     std::remove(file3.c_str());
00214 
00215     return true;
00216 }
00217 
00218 static void Remove(const std::string &path)
00219 {
00220     if (Glib::file_test(path, Glib::FILE_TEST_IS_REGULAR)) {
00221         unlink(path.c_str());
00222         return;
00223     }
00224     if (Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
00225         Glib::Dir dir(path);
00226         std::string d;
00227         while ((d = dir.read_name()) != "") {
00228             Remove(Glib::build_filename(path, d));
00229         }
00230         dir.close();
00231         rmdir(path.c_str());
00232     }
00233 }
00234 
00235 void Job::clean(const std::string &jobroot)
00236 {
00237     std::string wd = Glib::build_filename(jobroot, id);
00238 
00239     // TODO: clean should be delayed
00240 
00241     //Remove(wd);
00242 }
00243 
00244 const std::string Job::getFailure(void)
00245 {
00246     return failure + "/" + sched_meta.getFailure();
00247 }
00248 }