1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.context;
17
18
19 import java.util.Arrays;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class BatchStepTrigger {
33 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchStepTrigger.class);
34
35 private static BatchStepTriggerParameters batchStepTriggerParameters;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public static void main(String[] args) {
55 try {
56 Log4jConfigurer.configureLogging(false);
57 BatchLogger.addConsoleAppender(LOG);
58
59 batchStepTriggerParameters = new BatchStepTriggerParameters(args);
60
61 String[] stepNames = getStepNames();
62 String jobName = getJobName();
63 int stepIndex = getStepIndex();
64 long sleepInterval = getSleepInterval();
65 BatchContainerDirectory batchContainerDirectory = getBatchContainerDirectory();
66
67 LOG.info("Executing Job: " + jobName + ", STEP"+ stepIndex +", Step(s): " + Arrays.toString(stepNames));
68
69 if (!batchContainerDirectory.isBatchContainerRunning()) {
70
71 LOG.error("The BatchContainer is not running - exiting without executing the steps: "+ Arrays.toString(stepNames));
72 LOG.info("Exit status: 8");
73 System.exit(8);
74 }
75
76
77 for (int i = (stepIndex-1); i < stepNames.length; i++) {
78 String stepName = stepNames[i];
79 BatchStepFileDescriptor batchStepFile = new BatchStepFileDescriptor(jobName, stepName, BatchStepFileDescriptor.getFileExtensionRun());
80
81
82 batchContainerDirectory.writeBatchStepRunFile(batchStepFile, i);
83
84
85 BatchStepFileDescriptor resultFile = listenForResultFile(batchContainerDirectory, batchStepFile, sleepInterval);
86
87 if (resultFile == null) {
88
89 batchContainerDirectory.removeBatchStepFileFromSystem(batchStepFile);
90
91 LOG.error("No result files were returned- exiting without knowing whether the Step was executed");
92 LOG.info("Exit status: 8");
93 System.exit(8);
94 }
95
96 if (resultFile.isStepFileAnErrorResultFile()) {
97
98 if (batchContainerDirectory.isFileEmpty(resultFile)) {
99
100 LOG.error(batchStepFile +" failed");
101 batchContainerDirectory.removeBatchStepFileFromSystem(resultFile);
102
103 LOG.info("Exit status: 4");
104 System.exit(4);
105 }
106 else {
107
108
109 LOG.error(batchStepFile +" failed with the following error message: ");
110 batchContainerDirectory.logFileContents(resultFile, LOG);
111 batchContainerDirectory.removeBatchStepFileFromSystem(resultFile);
112
113 LOG.info("Exit status: 8");
114 System.exit(8);
115 }
116 }
117
118 batchContainerDirectory.removeBatchStepFileFromSystem(resultFile);
119 LOG.info("Exiting "+ batchStepFile);
120
121 }
122
123
124 LOG.info("Exit status: 0");
125
126 System.exit(0);
127 }
128 catch (Throwable t) {
129 System.err.println("ERROR: Exception caught: ");
130 t.printStackTrace(System.err);
131 LOG.error(t);
132
133 System.exit(8);
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146 private static BatchStepFileDescriptor listenForResultFile(BatchContainerDirectory batchContainerDirectory, BatchStepFileDescriptor batchStepFile, long sleepInterval) {
147 if (LOG.isDebugEnabled()) {
148 LOG.debug("Waiting for result file for "+ batchStepFile);
149 }
150
151 while (true) {
152
153 BatchStepFileDescriptor resultFile = batchContainerDirectory.getResultFile(batchStepFile);
154 if (resultFile != null) {
155 LOG.info("Found result file: "+ resultFile.getName());
156
157 return resultFile;
158 }
159
160 if (batchContainerDirectory.isBatchContainerRunning()) {
161 sleep(sleepInterval);
162 }
163 else {
164
165 batchContainerDirectory.writeBatchStepErrorResultFile(batchStepFile, new RuntimeException("The BatchContainer is not running - exiting without knowing whether the Step executed"));
166
167 resultFile = batchContainerDirectory.getResultFile(batchStepFile);
168 return resultFile;
169 }
170 }
171 }
172
173
174
175
176
177
178 private static void sleep(long sleepInterval) {
179 if (LOG.isDebugEnabled()) {
180 LOG.debug("Sleeping...");
181 }
182 try {
183 Thread.sleep(sleepInterval);
184 }
185 catch (InterruptedException e) {
186 throw new RuntimeException("BatchStepTrigger encountered interrupt exception while trying to wait for the specified batch step semaphore processing interval", e);
187 }
188 }
189
190
191
192
193 private static String[] getStepNames() {
194 return batchStepTriggerParameters.getStepNames();
195 }
196
197
198
199
200 private static String getJobName() {
201 return batchStepTriggerParameters.getJobName();
202 }
203
204
205
206
207 private static int getStepIndex() {
208 return batchStepTriggerParameters.getStepIndex();
209 }
210
211
212
213
214 private static long getSleepInterval() {
215 return batchStepTriggerParameters.getSleepInterval();
216 }
217
218
219
220
221 private static BatchContainerDirectory getBatchContainerDirectory() {
222 return batchStepTriggerParameters.getBatchContainerDirectory();
223 }
224 }