1 package fr.in2p3.jsaga.command;
2
3 import org.apache.commons.cli.*;
4 import org.ogf.saga.context.Context;
5 import org.ogf.saga.error.*;
6 import org.ogf.saga.job.*;
7 import org.ogf.saga.monitoring.Callback;
8 import org.ogf.saga.monitoring.Metric;
9 import org.ogf.saga.monitoring.Monitorable;
10 import org.ogf.saga.session.Session;
11 import org.ogf.saga.session.SessionFactory;
12 import org.ogf.saga.task.State;
13 import org.ogf.saga.url.URL;
14 import org.ogf.saga.url.URLFactory;
15
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class JobStatus extends AbstractCommand {
32 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
33 private static final String OPT_MONITOR = "m", LONGOPT_MONITOR = "monitor";
34
35 protected JobStatus() {
36 super("jsaga-job-status", new String[]{"jobId"}, new String[]{OPT_HELP, LONGOPT_HELP});
37 }
38
39 public static void main(String[] args) throws Exception {
40 JobStatus command = new JobStatus();
41 CommandLine line = command.parse(args);
42 if (line.hasOption(OPT_HELP))
43 {
44 command.printHelpAndExit(null);
45 }
46 else
47 {
48
49 URL serviceURL;
50 String nativeJobId;
51 Pattern pattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]");
52 Matcher matcher = pattern.matcher(command.m_nonOptionValues[0]);
53 if (matcher.find()) {
54 serviceURL = URLFactory.createURL(matcher.group(1));
55 nativeJobId = matcher.group(2);
56 } else {
57 throw new BadParameterException("Job ID does not match regular expression: "+pattern.pattern());
58 }
59
60
61 Session session = SessionFactory.createSession(true);
62 JobService service = JobFactory.createJobService(session, serviceURL);
63 Job job = service.getJob(nativeJobId);
64
65 if (line.hasOption(OPT_MONITOR)) {
66 Metric metric = job.getMetric(Job.JOB_STATE);
67 metric.addCallback(new Callback(){
68 public boolean cb(Monitorable mt, Metric metric, Context ctx) throws NotImplementedException, AuthorizationFailedException {
69 try {
70 String value = metric.getAttribute(Metric.VALUE);
71 System.out.println("Current state: "+value);
72 }
73 catch (NotImplementedException e) {throw e;}
74 catch (AuthorizationFailedException e) {throw e;}
75 catch (Exception e) {
76 e.printStackTrace();
77 }
78
79 return true;
80 }
81 });
82 job.waitFor();
83 }
84
85 State state = job.getState();
86
87
88 if (State.RUNNING.compareTo(state) == 0) {
89 System.out.println("Job is running.");
90 } else if (State.SUSPENDED.compareTo(state) == 0) {
91 System.out.println("Job is suspended.");
92 } else if (State.DONE.compareTo(state) == 0) {
93 System.out.println("Job done.");
94 } else if (State.CANCELED.compareTo(state) == 0) {
95 System.out.println("Job canceled.");
96 } else if (State.FAILED.compareTo(state) == 0) {
97 try {
98 String exitCode = job.getAttribute(Job.EXITCODE);
99 System.out.println("Job failed with exit code: "+exitCode);
100 } catch(NotImplementedException e) {
101 System.out.println("Job failed.");
102 job.rethrow();
103 }
104 } else {
105 throw new Exception("Unexpected state: "+ state);
106 }
107 System.exit(0);
108 }
109 }
110
111 protected Options createOptions() {
112 Options opt = new Options();
113
114
115 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
116 .withLongOpt(LONGOPT_HELP)
117 .create(OPT_HELP));
118 opt.addOption(OptionBuilder.withDescription("Monitor job")
119 .isRequired(false)
120 .withLongOpt(LONGOPT_MONITOR)
121 .create(OPT_MONITOR));
122
123
124 return opt;
125 }
126 }