kill implementation fixed
[gp-arc-client-c.git] / ext / arc_stat.cpp
1 /*
2  *  arc_stat.cpp
3  *  arcclientc
4  *
5  *  Created by Tamas Jung on 10/25/10.
6  *  Copyright 2010 Apple. All rights reserved.
7  *
8  */
9
10 #include "arc_stat.h"
11
12 // -*- indent-tabs-mode: nil -*-
13
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 #include "logger.h"
18 #include <iostream>
19 #include <list>
20 #include <string>
21 #include <algorithm>
22 #include "arc_libs.h"
23
24 stat_result_t*  arc_stat(int argc, char **argv) {
25         
26         stat_result_t* result = (stat_result_t*)malloc(sizeof(stat_result_t));
27         if (!result) {
28                 return NULL;//TBD: throw sth?
29         }
30         result->message = NULL;
31         result->stat = NULL;
32         
33         
34         Arc::OptionParser options(istring("[job ...]"),
35                                                           istring("The arcstat command is used for "
36                                                                           "obtaining the status of jobs that have\n"
37                                                                           "been submitted to Grid enabled resources."),
38                                                           istring("Argument to -c has the format "
39                                                                           "Flavour:URL e.g.\n"
40                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
41                                                                           "nordugrid-cluster-name=grid.tsl.uu.se,"
42                                                                           "Mds-Vo-name=local,o=grid"));
43         
44         
45         std::string joblist;
46         options.AddOption('j', "joblist",
47                                           istring("file containing a list of jobs"),
48                                           istring("filename"),
49                                           joblist);
50         
51         std::list<std::string> clusters;
52         options.AddOption('c', "cluster",
53                                           istring("explicitly select or reject a specific resource"),
54                                           istring("[-]name"),
55                                           clusters);
56         
57         
58         int timeout = -1;
59         options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
60                                           istring("seconds"), timeout);
61         
62         std::string conffile;
63         options.AddOption('z', "conffile",
64                                           istring("configuration file (default ~/.arc/client.conf)"),
65                                           istring("filename"), conffile);
66                 
67         
68         std::list<std::string> jobs = options.Parse(argc, argv);
69         
70         
71                 
72         Arc::UserConfig usercfg(conffile, joblist);
73         if (!usercfg) {
74                 //result  -> message = new_str("Failed configuration initialization");
75                 logger.msg(Arc::ERROR, "Failed configuration initialization");
76                 return result;
77         }
78                 
79         if (timeout > 0)
80         usercfg.Timeout(timeout);
81         
82
83         
84         if (!jobs.empty())
85         usercfg.ClearSelectedServices();
86         
87         if (!clusters.empty()) {
88                 usercfg.ClearSelectedServices();
89                 usercfg.AddServices(clusters, Arc::COMPUTING);
90         }
91         
92         Arc::JobSupervisor jobmaster(usercfg, jobs);
93         if (!jobmaster.JobsFound()) {
94                 //result -> message = new_str("No jobs");
95     logger.msg(Arc::INFO, "No jobs");
96                 return result;
97         }
98         std::list<Arc::JobController*> jobcont = jobmaster.GetJobControllers();
99         
100         if (jobcont.empty()) {
101                 //result -> message = new_str("No job controller plugins loaded");
102     logger.msg(Arc::INFO, "No job controller plugins loaded");
103                 return result;
104         }
105         
106         if (jobcont.size() != 1){
107                 //result -> message = new_str("Too many jobs found");
108     logger.msg(Arc::ERROR, "Too many jobs found");
109                 return result;
110         } 
111         Arc::JobController* it = jobcont.front();
112         
113         it->GetJobInformation();
114         
115         std::list<Arc::Job> jobs_list = it->GetJobs();
116         
117         if (jobs_list.size() != 1) {
118                 //result -> message = new_str("Ambiguous job");
119     logger.msg(Arc::ERROR, "Ambiguous job");
120                 return result;
121         }
122         
123         Arc::Job& job = jobs_list.front();
124         
125         result -> stat = new_str(job.State().c_str());
126         
127         return result;
128         
129         
130 }