View Javadoc

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  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   JobCancel
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   25 janv. 2008
22  * ***************************************************
23  * Description:                                      */
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              // get arguments
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              // cancel job
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              // display status
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          // command
85          opt.addOption(OptionBuilder.withDescription("Display this help and exit")
86                  .withLongOpt(LONGOPT_HELP)
87                  .create(OPT_HELP));
88  
89          // returns
90          return opt;
91      }
92  }