~ubuntu-branches/ubuntu/utopic/nordugrid-arc/utopic-proposed

« back to all changes in this revision

Viewing changes to src/clients/arclib/arcstat.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2013-04-17 06:37:28 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130417063728-h56uqu0belk87zqa
Tags: 3.0.0-1
3.0.0 Release (Closes: #690716)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- indent-tabs-mode: nil -*-
2
 
 
3
 
#ifdef HAVE_CONFIG_H
4
 
#include <config.h>
5
 
#endif
6
 
 
7
 
#include <iostream>
8
 
#include <list>
9
 
#include <string>
10
 
#include <algorithm>
11
 
 
12
 
#include <arc/ArcLocation.h>
13
 
#include <arc/IString.h>
14
 
#include <arc/Logger.h>
15
 
#include <arc/UserConfig.h>
16
 
#include <arc/client/JobSupervisor.h>
17
 
 
18
 
#include "utils.h"
19
 
 
20
 
int RUNMAIN(arcstat)(int argc, char **argv) {
21
 
 
22
 
  setlocale(LC_ALL, "");
23
 
 
24
 
  Arc::Logger logger(Arc::Logger::getRootLogger(), "arcstat");
25
 
  Arc::LogStream logcerr(std::cerr);
26
 
  logcerr.setFormat(Arc::ShortFormat);
27
 
  Arc::Logger::getRootLogger().addDestination(logcerr);
28
 
  Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
29
 
 
30
 
  Arc::ArcLocation::Init(argv[0]);
31
 
 
32
 
  ClientOptions opt(ClientOptions::CO_STAT,
33
 
                    istring("[job ...]"),
34
 
                    istring("The arcstat command is used for "
35
 
                            "obtaining the status of jobs that have\n"
36
 
                            "been submitted to Grid enabled resources."));
37
 
 
38
 
  std::list<std::string> jobidentifiers = opt.Parse(argc, argv);
39
 
 
40
 
  if (opt.showversion) {
41
 
    std::cout << Arc::IString("%s version %s", "arcstat", VERSION)
42
 
              << std::endl;
43
 
    return 0;
44
 
  }
45
 
 
46
 
  // If debug is specified as argument, it should be set before loading the configuration.
47
 
  if (!opt.debug.empty())
48
 
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(opt.debug));
49
 
 
50
 
  if (opt.show_plugins) {
51
 
    std::list<std::string> types;
52
 
    types.push_back("HED:JobControllerPlugin");
53
 
    showplugins("arcstat", types, logger);
54
 
    return 0;
55
 
  }
56
 
 
57
 
  Arc::UserConfig usercfg(opt.conffile, opt.joblist);
58
 
  if (!usercfg) {
59
 
    logger.msg(Arc::ERROR, "Failed configuration initialization");
60
 
    return 1;
61
 
  }
62
 
 
63
 
  if (opt.debug.empty() && !usercfg.Verbosity().empty())
64
 
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
65
 
 
66
 
  for (std::list<std::string>::const_iterator it = opt.jobidinfiles.begin(); it != opt.jobidinfiles.end(); it++) {
67
 
    if (!Arc::Job::ReadJobIDsFromFile(*it, jobidentifiers)) {
68
 
      logger.msg(Arc::WARNING, "Cannot read specified jobid file: %s", *it);
69
 
    }
70
 
  }
71
 
 
72
 
  if (opt.timeout > 0)
73
 
    usercfg.Timeout(opt.timeout);
74
 
 
75
 
  if (!opt.sort.empty() && !opt.rsort.empty()) {
76
 
    logger.msg(Arc::ERROR, "The 'sort' and 'rsort' flags cannot be specified at the same time.");
77
 
    return 1;
78
 
  }
79
 
 
80
 
  if (!opt.rsort.empty()) {
81
 
    opt.sort = opt.rsort;
82
 
  }
83
 
 
84
 
  typedef bool (*JobSorting)(const Arc::Job&, const Arc::Job&);
85
 
  std::map<std::string, JobSorting> orderings;
86
 
  orderings["jobid"] = &Arc::Job::CompareJobID;
87
 
  orderings["submissiontime"] = &Arc::Job::CompareSubmissionTime;
88
 
  orderings["jobname"] = &Arc::Job::CompareJobName;
89
 
 
90
 
  if (!opt.sort.empty() && orderings.find(opt.sort) == orderings.end()) {
91
 
    std::cerr << "Jobs cannot be sorted by \"" << opt.sort << "\", the following orderings are supported:" << std::endl;
92
 
    for (std::map<std::string, JobSorting>::const_iterator it = orderings.begin();
93
 
         it != orderings.end(); it++)
94
 
      std::cerr << it->first << std::endl;
95
 
    return 1;
96
 
  }
97
 
 
98
 
  if ((!opt.joblist.empty() || !opt.status.empty()) && jobidentifiers.empty() && opt.clusters.empty())
99
 
    opt.all = true;
100
 
 
101
 
  if (jobidentifiers.empty() && opt.clusters.empty() && !opt.all) {
102
 
    logger.msg(Arc::ERROR, "No jobs given");
103
 
    return 1;
104
 
  }
105
 
  
106
 
  std::list<std::string> selectedURLs;
107
 
  if (!opt.clusters.empty()) {
108
 
    selectedURLs = getSelectedURLsFromUserConfigAndCommandLine(usercfg, opt.clusters);
109
 
  }
110
 
  std::list<std::string> rejectManagementURLs = getRejectManagementURLsFromUserConfigAndCommandLine(usercfg, opt.rejectmanagement);
111
 
  std::list<Arc::Job> jobs;
112
 
  if (!Arc::Job::ReadJobsFromFile(usercfg.JobListFile(), jobs, jobidentifiers, opt.all, selectedURLs, rejectManagementURLs)) {
113
 
    logger.msg(Arc::ERROR, "Unable to read job information from file (%s)", usercfg.JobListFile());
114
 
    return 1;
115
 
  }
116
 
 
117
 
  for (std::list<std::string>::const_iterator itJIDAndName = jobidentifiers.begin();
118
 
       itJIDAndName != jobidentifiers.end(); ++itJIDAndName) {
119
 
    std::cout << Arc::IString("Warning: Job not found in job list: %s", *itJIDAndName) << std::endl;
120
 
  }
121
 
 
122
 
  Arc::JobSupervisor jobmaster(usercfg, jobs);
123
 
  jobmaster.Update();
124
 
  jobmaster.SelectValid();
125
 
  if (!opt.status.empty()) {
126
 
    jobmaster.SelectByStatus(opt.status);
127
 
  }
128
 
  jobs = jobmaster.GetSelectedJobs();
129
 
 
130
 
  if (jobs.empty()) {
131
 
    std::cout << Arc::IString("No jobs") << std::endl;
132
 
    return 1;
133
 
  }
134
 
 
135
 
  std::vector<Arc::Job> jobsSortable(jobs.begin(), jobs.end());
136
 
 
137
 
  if (!opt.sort.empty()) {
138
 
    opt.rsort.empty() ? std::sort(jobsSortable.begin(),  jobsSortable.end(),  orderings[opt.sort]) :
139
 
                        std::sort(jobsSortable.rbegin(), jobsSortable.rend(), orderings[opt.sort]);
140
 
  }
141
 
 
142
 
  for (std::vector<Arc::Job>::const_iterator it = jobsSortable.begin();
143
 
       it != jobsSortable.end(); it++) {
144
 
    // Option 'long' (longlist) takes precedence over option 'print-jobids' (printids)
145
 
    if (opt.longlist || !opt.printids) {
146
 
      it->SaveToStream(std::cout, opt.longlist);
147
 
    }
148
 
    else {
149
 
      std::cout << it->JobID.fullstr() << std::endl;
150
 
    }
151
 
  }
152
 
 
153
 
  return 0;
154
 
}