Skip to content

Package: JobEngineServiceJbatch

JobEngineServiceJbatch

nameinstructionbranchcomplexitylinemethod
JobEngineServiceJbatch()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
cleanJobData(KapuaId, KapuaId)
M: 50 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
internalIsRunning(KapuaId, KapuaId)
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
isRunning(KapuaId, KapuaId)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
isRunning(KapuaId, Set)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
lambda$isRunning$0(Map, KapuaId, KapuaId)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
resumeJobExecution(KapuaId, KapuaId, KapuaId)
M: 71 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
startJob(KapuaId, KapuaId)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
startJob(KapuaId, KapuaId, JobStartOptions)
M: 131 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 43 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
stopJob(KapuaId, KapuaId)
M: 51 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
stopJobExecution(KapuaId, KapuaId, KapuaId)
M: 71 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.job.engine.jbatch;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17: import java.util.Set;
18:
19: import org.eclipse.kapua.KapuaEntityNotFoundException;
20: import org.eclipse.kapua.KapuaException;
21: import org.eclipse.kapua.KapuaIllegalArgumentException;
22: import org.eclipse.kapua.commons.util.ArgumentValidator;
23: import org.eclipse.kapua.job.engine.JobEngineService;
24: import org.eclipse.kapua.job.engine.JobStartOptions;
25: import org.eclipse.kapua.job.engine.jbatch.driver.JbatchDriver;
26: import org.eclipse.kapua.job.engine.exception.CleanJobDataException;
27: import org.eclipse.kapua.job.engine.exception.JobCheckRunningException;
28: import org.eclipse.kapua.job.engine.exception.JobInvalidTargetException;
29: import org.eclipse.kapua.job.engine.exception.JobMissingStepException;
30: import org.eclipse.kapua.job.engine.exception.JobMissingTargetException;
31: import org.eclipse.kapua.job.engine.exception.JobNotRunningException;
32: import org.eclipse.kapua.job.engine.exception.JobResumingException;
33: import org.eclipse.kapua.job.engine.exception.JobRunningException;
34: import org.eclipse.kapua.job.engine.exception.JobStartingException;
35: import org.eclipse.kapua.job.engine.exception.JobStoppingException;
36: import org.eclipse.kapua.locator.KapuaLocator;
37: import org.eclipse.kapua.locator.KapuaProvider;
38: import org.eclipse.kapua.model.KapuaEntityAttributes;
39: import org.eclipse.kapua.model.domain.Actions;
40: import org.eclipse.kapua.model.id.KapuaId;
41: import org.eclipse.kapua.service.authorization.AuthorizationService;
42: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
43: import org.eclipse.kapua.service.job.Job;
44: import org.eclipse.kapua.service.job.JobDomains;
45: import org.eclipse.kapua.service.job.JobService;
46: import org.eclipse.kapua.service.job.execution.JobExecution;
47: import org.eclipse.kapua.service.job.execution.JobExecutionService;
48: import org.eclipse.kapua.service.job.step.JobStepAttributes;
49: import org.eclipse.kapua.service.job.step.JobStepFactory;
50: import org.eclipse.kapua.service.job.step.JobStepQuery;
51: import org.eclipse.kapua.service.job.step.JobStepService;
52: import org.eclipse.kapua.service.job.targets.JobTargetAttributes;
53: import org.eclipse.kapua.service.job.targets.JobTargetFactory;
54: import org.eclipse.kapua.service.job.targets.JobTargetQuery;
55: import org.eclipse.kapua.service.job.targets.JobTargetService;
56:
57: @KapuaProvider
58: public class JobEngineServiceJbatch implements JobEngineService {
59:
60: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
61:
62: private static final AuthorizationService AUTHORIZATION_SERVICE = LOCATOR.getService(AuthorizationService.class);
63: private static final PermissionFactory PERMISSION_FACTORY = LOCATOR.getFactory(PermissionFactory.class);
64:
65: private static final JobService JOB_SERVICE = LOCATOR.getService(JobService.class);
66:
67: private static final JobExecutionService JOB_EXECUTION_SERVICE = LOCATOR.getService(JobExecutionService.class);
68:
69: private static final JobStepService JOB_STEP_SERVICE = LOCATOR.getService(JobStepService.class);
70: private static final JobStepFactory JOB_STEP_FACTORY = LOCATOR.getFactory(JobStepFactory.class);
71:
72: private static final JobTargetService JOB_TARGET_SERVICE = LOCATOR.getService(JobTargetService.class);
73: private static final JobTargetFactory JOB_TARGET_FACTORY = LOCATOR.getFactory(JobTargetFactory.class);
74:
75: private static final String JOB_EXECUTION_ID = "jobExecutionId";
76:
77: @Override
78: public void startJob(KapuaId scopeId, KapuaId jobId) throws KapuaException {
79: startJob(scopeId, jobId, new JobStartOptionsImpl());
80: }
81:
82: @Override
83: public void startJob(KapuaId scopeId, KapuaId jobId, JobStartOptions jobStartOptions) throws KapuaException {
84: //
85: // Argument Validation
86: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
87: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
88: ArgumentValidator.notNull(jobStartOptions, "jobStartOptions");
89:
90: //
91: // Check Access
92: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.execute, scopeId));
93:
94: //
95: // Check Job Existence
96: Job job = JOB_SERVICE.find(scopeId, jobId);
97:• if (job == null) {
98: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
99: }
100:
101: //
102: // Check job targets
103: JobTargetQuery jobTargetQuery = JOB_TARGET_FACTORY.newQuery(scopeId);
104: jobTargetQuery.setPredicate(jobTargetQuery.attributePredicate(JobTargetAttributes.JOB_ID, jobId));
105:• if (JOB_TARGET_SERVICE.count(jobTargetQuery) <= 0) {
106: throw new JobMissingTargetException(scopeId, jobId);
107: }
108:
109: //
110: // Check job target sublist
111:• if (!jobStartOptions.getTargetIdSublist().isEmpty()) {
112: jobTargetQuery.setPredicate(
113: jobTargetQuery.andPredicate(
114: jobTargetQuery.getPredicate(),
115: jobTargetQuery.attributePredicate(KapuaEntityAttributes.ENTITY_ID, jobStartOptions.getTargetIdSublist().toArray())
116: )
117: );
118:
119:• if (jobStartOptions.getTargetIdSublist().size() != JOB_TARGET_SERVICE.count(jobTargetQuery)) {
120: throw new JobInvalidTargetException(scopeId, jobId, jobStartOptions.getTargetIdSublist());
121: }
122: }
123:
124: //
125: // Check job steps
126: JobStepQuery jobStepQuery = JOB_STEP_FACTORY.newQuery(scopeId);
127: jobStepQuery.setPredicate(jobStepQuery.attributePredicate(JobStepAttributes.JOB_ID, jobId));
128:• if (JOB_STEP_SERVICE.count(jobStepQuery) <= 0) {
129: throw new JobMissingStepException(scopeId, jobId);
130: }
131:
132: //
133: // Start the job
134: try {
135: JbatchDriver.startJob(scopeId, jobId, jobStartOptions);
136: } catch (Exception e) {
137: throw new JobStartingException(e, scopeId, jobId);
138: }
139: }
140:
141: @Override
142: public boolean isRunning(KapuaId scopeId, KapuaId jobId) throws KapuaException {
143: //
144: // Argument Validation
145: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
146: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
147:
148: //
149: // Check Access
150: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.read, scopeId));
151:
152: return internalIsRunning(scopeId, jobId);
153: }
154:
155: private boolean internalIsRunning(KapuaId scopeId, KapuaId jobId) throws KapuaException {
156: //
157: // Check existence
158: Job job = JOB_SERVICE.find(scopeId, jobId);
159:• if (job == null) {
160: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
161: }
162:
163: //
164: // Do check running
165: try {
166: return JbatchDriver.isRunningJob(scopeId, jobId);
167: } catch (Exception e) {
168: throw new JobCheckRunningException(e, scopeId, jobId);
169: }
170: }
171:
172: @Override
173: public Map<KapuaId, Boolean> isRunning(KapuaId scopeId, Set<KapuaId> jobIds) throws KapuaException {
174: //
175: // Argument Validation
176: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
177:
178: //
179: // Check Access
180: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.read, scopeId));
181:
182: Map<KapuaId, Boolean> isRunningMap = new HashMap<>();
183: jobIds.forEach(jobId -> {
184: try {
185: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
186: isRunningMap.put(jobId, internalIsRunning(scopeId, jobId));
187: } catch (KapuaException kapuaException) {
188: // No other way to report an error?
189: isRunningMap.put(jobId, null);
190: }
191: });
192: return isRunningMap;
193: }
194:
195: @Override
196: public void stopJob(KapuaId scopeId, KapuaId jobId) throws KapuaException {
197: //
198: // Argument Validation
199: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
200: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
201:
202: //
203: // Check Access
204: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.execute, scopeId));
205:
206: //
207: // Check existence
208: Job job = JOB_SERVICE.find(scopeId, jobId);
209:• if (job == null) {
210: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
211: }
212:
213: //
214: // Check job running
215:• if (!JbatchDriver.isRunningJob(scopeId, jobId)) {
216: throw new JobNotRunningException(scopeId, jobId);
217: }
218:
219: //
220: // Stop the job
221: try {
222: JbatchDriver.stopJob(scopeId, jobId, null);
223: } catch (Exception e) {
224: throw new JobStoppingException(e, scopeId, jobId);
225: }
226: }
227:
228: @Override
229: public void stopJobExecution(KapuaId scopeId, KapuaId jobId, KapuaId jobExecutionId) throws KapuaException {
230: //
231: // Argument Validation
232: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
233: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
234: ArgumentValidator.notNull(jobExecutionId, JOB_EXECUTION_ID);
235:
236: //
237: // Check Access
238: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.execute, scopeId));
239:
240: //
241: // Check existence
242: Job job = JOB_SERVICE.find(scopeId, jobId);
243:• if (job == null) {
244: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
245: }
246:
247: //
248: // Check execution existence
249: JobExecution jobExecution = JOB_EXECUTION_SERVICE.find(scopeId, jobExecutionId);
250:• if (jobExecution == null) {
251: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
252: }
253:
254: //
255: // Check that JobExecution belongs to the Job
256:• if (!jobExecution.getJobId().equals(jobId)) {
257: throw new KapuaIllegalArgumentException(JOB_EXECUTION_ID, jobExecutionId.toString());
258: }
259:
260: //
261: // Stop the JobExecution
262: try {
263: JbatchDriver.stopJob(scopeId, jobId, jobExecutionId);
264: } catch (Exception e) {
265: throw new JobStoppingException(e, scopeId, jobId, jobExecutionId);
266: }
267:
268: }
269:
270: @Override
271: public void resumeJobExecution(KapuaId scopeId, KapuaId jobId, KapuaId jobExecutionId) throws KapuaException {
272: //
273: // Argument Validation
274: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
275: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
276: ArgumentValidator.notNull(jobExecutionId, JOB_EXECUTION_ID);
277:
278: //
279: // Check Access
280: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.execute, scopeId));
281:
282: //
283: // Check existence
284: Job job = JOB_SERVICE.find(scopeId, jobId);
285:• if (job == null) {
286: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
287: }
288:
289: //
290: // Check execution existence
291: JobExecution jobExecution = JOB_EXECUTION_SERVICE.find(scopeId, jobExecutionId);
292:
293:• if (jobExecution == null) {
294: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
295: }
296:
297: //
298: // Check that JobExecution belongs to the Job
299:• if (!jobExecution.getJobId().equals(jobId)) {
300: throw new KapuaIllegalArgumentException(JOB_EXECUTION_ID, jobExecutionId.toString());
301: }
302:
303: //
304: // Resume the JobExecution
305: try {
306: JbatchDriver.resumeJob(scopeId, jobId, jobExecutionId);
307: } catch (Exception e) {
308: throw new JobResumingException(e, scopeId, jobId, jobExecutionId);
309: }
310: }
311:
312: @Override
313: public void cleanJobData(KapuaId scopeId, KapuaId jobId) throws KapuaException {
314: //
315: // Argument Validation
316: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
317: ArgumentValidator.notNull(jobId, KapuaEntityAttributes.ENTITY_ID);
318:
319: //
320: // Check Access
321: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(JobDomains.JOB_DOMAIN, Actions.delete, null));
322:
323: //
324: // Check existence
325: Job job = JOB_SERVICE.find(scopeId, jobId);
326:• if (job == null) {
327: throw new KapuaEntityNotFoundException(Job.TYPE, jobId);
328: }
329:
330: //
331: // Check job not running
332:• if (JbatchDriver.isRunningJob(scopeId, jobId)) {
333: throw new JobRunningException(scopeId, jobId);
334: }
335:
336: try {
337: JbatchDriver.cleanJobData(scopeId, jobId);
338: } catch (Exception ex) {
339: throw new CleanJobDataException(ex, scopeId, jobId);
340: }
341: }
342: }