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