1 package fr.in2p3.jsaga.command;
2
3 import org.apache.commons.cli.*;
4 import org.ogf.saga.error.BadParameterException;
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.task.State;
9 import org.ogf.saga.url.URL;
10 import org.ogf.saga.url.URLFactory;
11
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class JobCancel extends AbstractCommand {
28 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
29
30 protected JobCancel() {
31 super("jsaga-job-cancel", new String[]{"jobId"}, new String[]{OPT_HELP, LONGOPT_HELP});
32 }
33
34 public static void main(String[] args) throws Exception {
35 JobCancel command = new JobCancel();
36 CommandLine line = command.parse(args);
37 if (line.hasOption(OPT_HELP))
38 {
39 command.printHelpAndExit(null);
40 }
41 else
42 {
43
44 URL serviceURL;
45 String nativeJobId;
46 Pattern pattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]");
47 Matcher matcher = pattern.matcher(command.m_nonOptionValues[0]);
48 if (matcher.find()) {
49 serviceURL = URLFactory.createURL(matcher.group(1));
50 nativeJobId = matcher.group(2);
51 } else {
52 throw new BadParameterException("Job ID does not match regular expression: "+pattern.pattern());
53 }
54
55
56 Session session = SessionFactory.createSession(true);
57 JobService service = JobFactory.createJobService(session, serviceURL);
58 Job job = service.getJob(nativeJobId);
59 job.cancel();
60 job.waitFor();
61
62
63 State state = job.getState();
64 if (State.RUNNING.compareTo(state) == 0) {
65 System.out.println("Job is running.");
66 } else if (State.SUSPENDED.compareTo(state) == 0) {
67 System.out.println("Job is suspended.");
68 } else if (State.DONE.compareTo(state) == 0) {
69 System.out.println("Job completed successfully.");
70 } else if (State.CANCELED.compareTo(state) == 0) {
71 System.out.println("Job canceled.");
72 } else if (State.FAILED.compareTo(state) == 0) {
73 System.out.println("Job failed with "+job.getAttribute("ExitCode"));
74 } else {
75 throw new Exception("Unexpected state: "+ state);
76 }
77 System.exit(0);
78 }
79 }
80
81 protected Options createOptions() {
82 Options opt = new Options();
83
84
85 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
86 .withLongOpt(LONGOPT_HELP)
87 .create(OPT_HELP));
88
89
90 return opt;
91 }
92 }