1 package fr.in2p3.jsaga.command;
2
3 import org.apache.commons.cli.*;
4 import org.ogf.saga.error.*;
5 import org.ogf.saga.job.*;
6 import org.ogf.saga.session.Session;
7 import org.ogf.saga.session.SessionFactory;
8 import org.ogf.saga.url.URL;
9 import org.ogf.saga.url.URLFactory;
10
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class JobInfo extends AbstractCommand {
27 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
28
29 protected JobInfo() {
30 super("jsaga-job-info", new String[]{"jobId"}, new String[]{OPT_HELP, LONGOPT_HELP});
31 }
32
33 public static void main(String[] args) throws Exception {
34 JobInfo command = new JobInfo();
35 CommandLine line = command.parse(args);
36 if (line.hasOption(OPT_HELP))
37 {
38 command.printHelpAndExit(null);
39 }
40 else
41 {
42
43 URL serviceURL;
44 String nativeJobId;
45 Pattern pattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]");
46 Matcher matcher = pattern.matcher(command.m_nonOptionValues[0]);
47 if (matcher.find()) {
48 serviceURL = URLFactory.createURL(matcher.group(1));
49 nativeJobId = matcher.group(2);
50 } else {
51 throw new BadParameterException("Job ID does not match regular expression: "+pattern.pattern());
52 }
53
54
55 Session session = SessionFactory.createSession(true);
56 JobService service = JobFactory.createJobService(session, serviceURL);
57 Job job = service.getJob(nativeJobId);
58
59
60 System.out.println("State: "+job.getState().toString());
61 System.out.println("Exit code: "+getAttribute(job, Job.EXITCODE));
62 System.out.println("Failure cause: "+ getFailureCause(job));
63 System.out.println("Created time: "+getAttribute(job, Job.CREATED));
64 System.out.println("Started time: "+getAttribute(job, Job.STARTED));
65 System.out.println("Finished time: "+getAttribute(job, Job.FINISHED));
66 System.out.println("Execution hosts:");
67 String[] hosts = getVectorAttribute(job, Job.EXECUTIONHOSTS);
68 for (int i=0; i<hosts.length; i++) {
69 System.out.println("\t"+hosts[i]);
70 }
71 System.exit(0);
72 }
73 }
74 private static String getAttribute(Job job, String key) throws SagaException {
75 try {
76 return job.getAttribute(key);
77 } catch (IncorrectStateException e) {
78 return "[not initialized yet]";
79 } catch (NotImplementedException e) {
80 return "[not supported for this backend]";
81 }
82 }
83 private static String[] getVectorAttribute(Job job, String key) throws SagaException {
84 try {
85 return job.getVectorAttribute(key);
86 } catch (IncorrectStateException e) {
87 return new String[]{"[not initialized yet]"};
88 } catch (NotImplementedException e) {
89 return new String[]{"[not supported for this backend]"};
90 }
91 }
92 private static String getFailureCause(Job job) {
93 try {
94 job.rethrow();
95 return null;
96 } catch (SagaException e) {
97 return e.getMessage();
98 }
99 }
100
101 protected Options createOptions() {
102 Options opt = new Options();
103
104
105 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
106 .withLongOpt(LONGOPT_HELP)
107 .create(OPT_HELP));
108
109
110 return opt;
111 }
112 }