diff --git a/assembly/broker/configurations/locator.xml b/assembly/broker/configurations/locator.xml index 9084e40e700..862cfe16eb9 100644 --- a/assembly/broker/configurations/locator.xml +++ b/assembly/broker/configurations/locator.xml @@ -86,11 +86,12 @@ org.eclipse.kapua.service.device.registry.event.DeviceEventService org.eclipse.kapua.service.device.registry.lifecycle.DeviceLifeCycleService + org.eclipse.kapua.service.job.execution.JobExecutionService + org.eclipse.kapua.service.job.execution.JobExecutionFactory org.eclipse.kapua.service.job.step.JobStepService org.eclipse.kapua.service.job.step.JobStepFactory - org.eclipse.kapua.service.job.step.definition.JobStepDefinitionService - + org.eclipse.kapua.service.job.step.definition.JobStepDefinitionFactory org.eclipse.kapua.service.job.targets.JobTargetService org.eclipse.kapua.service.job.targets.JobTargetFactory diff --git a/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepDefinitionServiceImpl.java b/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepDefinitionServiceImpl.java index 10ce3785f47..9383687ca26 100644 --- a/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepDefinitionServiceImpl.java +++ b/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepDefinitionServiceImpl.java @@ -64,10 +64,10 @@ public ListLoadResult findAll() throws GwtKapuaException { gwtJobStepDefinitionList.add(gwtJobStepDefinition); } - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + return new BaseListLoadResult(gwtJobStepDefinitionList); + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } - return new BaseListLoadResult(gwtJobStepDefinitionList); } @Override @@ -82,11 +82,10 @@ public GwtJobStepDefinition find(String gwtJobStepDefinitionId) throws GwtKapuaE setEnumOnJobStepProperty(gwtJobStepDefinition.getStepProperties()); } - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + return gwtJobStepDefinition; + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } - - return gwtJobStepDefinition; } @Override diff --git a/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepServiceImpl.java b/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepServiceImpl.java index 862ec7a8623..35b0722b44f 100644 --- a/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepServiceImpl.java +++ b/console/module/job/src/main/java/org/eclipse/kapua/app/console/module/job/server/GwtJobStepServiceImpl.java @@ -75,11 +75,10 @@ public PagingLoadResult query(PagingLoadConfig loadConfig, GwtJobSte gwtJobStepList.add(gwtJobStep); } - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + return new BasePagingLoadResult(gwtJobStepList, loadConfig.getOffset(), totalLength); + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } - - return new BasePagingLoadResult(gwtJobStepList, loadConfig.getOffset(), totalLength); } @Override @@ -115,13 +114,13 @@ public GwtJobStep create(GwtXSRFToken xsrfToken, GwtJobStepCreator gwtJobStepCre gwtJobStep = KapuaGwtJobModelConverter.convertJobStep(jobStep); setEnumOnJobStepProperty(gwtJobStep.getStepProperties()); - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); - } - // - // Return result - return gwtJobStep; + // + // Return result + return gwtJobStep; + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); + } } @Override @@ -133,8 +132,8 @@ public void delete(GwtXSRFToken xsrfToken, String gwtScopeId, String gwtJobStepI try { JOB_STEP_SERVICE.delete(scopeId, jobTargetId); - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } } @@ -153,8 +152,8 @@ public GwtJobStep find(String gwtScopeId, String gwtJobStepId) throws GwtKapuaEx setEnumOnJobStepProperty(gwtJobStep.getStepProperties()); } - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } return gwtJobStep; @@ -193,10 +192,11 @@ public GwtJobStep update(GwtXSRFToken xsrfToken, GwtJobStep gwtJobStep) throws G setEnumOnJobStepProperty(gwtJobStep.getStepProperties()); } - } catch (Throwable t) { - KapuaExceptionHandler.handle(t); + + return gwtJobStepUpdated; + } catch (Exception e) { + throw KapuaExceptionHandler.buildExceptionFromError(e); } - return gwtJobStepUpdated; } /** diff --git a/console/web/src/main/resources/locator.xml b/console/web/src/main/resources/locator.xml index d6cad90e8c1..4eebb436c1b 100644 --- a/console/web/src/main/resources/locator.xml +++ b/console/web/src/main/resources/locator.xml @@ -103,22 +103,17 @@ org.eclipse.kapua.service.job.JobService org.eclipse.kapua.service.job.JobFactory - org.eclipse.kapua.service.job.execution.JobExecutionService org.eclipse.kapua.service.job.execution.JobExecutionFactory - org.eclipse.kapua.service.job.step.JobStepService org.eclipse.kapua.service.job.step.JobStepFactory - org.eclipse.kapua.service.job.step.definition.JobStepDefinitionService org.eclipse.kapua.service.job.step.definition.JobStepDefinitionFactory - org.eclipse.kapua.service.job.targets.JobTargetService org.eclipse.kapua.service.job.targets.JobTargetFactory org.eclipse.kapua.service.scheduler.trigger.TriggerService org.eclipse.kapua.service.scheduler.trigger.TriggerFactory - org.eclipse.kapua.service.scheduler.trigger.definition.TriggerDefinitionService org.eclipse.kapua.service.scheduler.trigger.definition.TriggerDefinitionFactory diff --git a/rest-api/web/src/main/resources/locator.xml b/rest-api/web/src/main/resources/locator.xml index 98db1bfcb81..b03e19512b6 100644 --- a/rest-api/web/src/main/resources/locator.xml +++ b/rest-api/web/src/main/resources/locator.xml @@ -107,16 +107,12 @@ org.eclipse.kapua.service.job.JobService org.eclipse.kapua.service.job.JobFactory - org.eclipse.kapua.service.job.execution.JobExecutionService org.eclipse.kapua.service.job.execution.JobExecutionFactory - org.eclipse.kapua.service.job.step.JobStepService org.eclipse.kapua.service.job.step.JobStepFactory - org.eclipse.kapua.service.job.step.definition.JobStepDefinitionService org.eclipse.kapua.service.job.step.definition.JobStepDefinitionFactory - org.eclipse.kapua.service.job.targets.JobTargetService org.eclipse.kapua.service.job.targets.JobTargetFactory @@ -125,7 +121,6 @@ org.eclipse.kapua.service.scheduler.trigger.TriggerService org.eclipse.kapua.service.scheduler.trigger.TriggerFactory - org.eclipse.kapua.service.scheduler.trigger.definition.TriggerDefinitionService org.eclipse.kapua.service.scheduler.trigger.definition.TriggerDefinitionFactory diff --git a/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/CannotModifyJobStepsException.java b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/CannotModifyJobStepsException.java new file mode 100644 index 00000000000..f818909f812 --- /dev/null +++ b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/CannotModifyJobStepsException.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2021 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.job.exception; + +import org.eclipse.kapua.model.id.KapuaId; +import org.eclipse.kapua.service.job.Job; +import org.eclipse.kapua.service.job.step.JobStep; + +/** + * {@link JobServiceException} to throw when trying to modify {@link JobStep}s of a {@link Job} that already started once. + * + * @since 1.5.0 + */ +public class CannotModifyJobStepsException extends JobServiceException { + + private final KapuaId jobId; + + /** + * Constructor. + * + * @param jobId The {@link Job#getId()}. + * @since 1.5.0 + */ + public CannotModifyJobStepsException(KapuaId jobId) { + super(JobServiceErrorCodes.CANNOT_MODIFY_JOB_STEPS, jobId); + + this.jobId = jobId; + } + + /** + * Gets the {@link Job#getId()}. + * + * @return The {@link Job#getId()}. + * @since 1.5.0 + */ + public KapuaId getJobId() { + return jobId; + } +} diff --git a/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceErrorCodes.java b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceErrorCodes.java new file mode 100644 index 00000000000..028bd400bac --- /dev/null +++ b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceErrorCodes.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2021 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.job.exception; + +import org.eclipse.kapua.KapuaErrorCode; + +/** + * {@link KapuaErrorCode}s for {@link JobServiceException} + * + * @since 1.5.0 + */ +public enum JobServiceErrorCodes implements KapuaErrorCode { + + /** + * See {@link CannotModifyJobStepsException}. + * + * @since 1.5.0 + */ + CANNOT_MODIFY_JOB_STEPS, +} diff --git a/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceException.java b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceException.java new file mode 100644 index 00000000000..1605112cd4f --- /dev/null +++ b/service/job/api/src/main/java/org/eclipse/kapua/service/job/exception/JobServiceException.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2021 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.job.exception; + +import org.eclipse.kapua.KapuaException; + +/** + * {@link KapuaException} base for Scheduler Service {@link Exception}s + * + * @since 1.5.0 + */ +public abstract class JobServiceException extends KapuaException { + + private static final String KAPUA_ERROR_MESSAGES = "job-service-error-messages"; + + /** + * Constructor. + * + * @param code The {@link JobServiceErrorCodes}. + * @since 1.5.0 + */ + public JobServiceException(JobServiceErrorCodes code) { + super(code); + } + + /** + * Constructor. + * + * @param code The {@link JobServiceErrorCodes}. + * @param arguments Additional argument associated with the {@link JobServiceException}. + * @since 1.5.0 + */ + public JobServiceException(JobServiceErrorCodes code, Object... arguments) { + super(code, arguments); + } + + /** + * Constructor. + * + * @param code The {@link JobServiceErrorCodes}. + * @param cause The root {@link Throwable} of this {@link JobServiceException}. + * @param arguments Additional argument associated with the {@link JobServiceException}. + * @since 1.5.0 + */ + public JobServiceException(JobServiceErrorCodes code, Throwable cause, Object... arguments) { + super(code, cause, arguments); + } + + @Override + protected String getKapuaErrorMessagesBundle() { + return KAPUA_ERROR_MESSAGES; + } +} diff --git a/service/job/api/src/main/resources/job-service-error-messages.properties b/service/job/api/src/main/resources/job-service-error-messages.properties new file mode 100644 index 00000000000..1e14e9bee46 --- /dev/null +++ b/service/job/api/src/main/resources/job-service-error-messages.properties @@ -0,0 +1,14 @@ +############################################################################### +# Copyright (c) 2021 Eurotech and/or its affiliates and others +# +# This program and the accompanying materials are made +# available under the terms of the Eclipse Public License 2.0 +# which is available at https://www.eclipse.org/legal/epl-2.0/ +# +# SPDX-License-Identifier: EPL-2.0 +# +# Contributors: +# Eurotech - initial API and implementation +# +############################################################################### +CANNOT_MODIFY_JOB_STEPS=Cannot modify JobSteps of Job {0} because it has already started once. \ No newline at end of file diff --git a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java index 6f39c64a1b1..e215e97b5fc 100644 --- a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java +++ b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java @@ -28,6 +28,11 @@ import org.eclipse.kapua.service.authorization.AuthorizationService; import org.eclipse.kapua.service.authorization.permission.PermissionFactory; import org.eclipse.kapua.service.job.JobDomains; +import org.eclipse.kapua.service.job.exception.CannotModifyJobStepsException; +import org.eclipse.kapua.service.job.execution.JobExecutionAttributes; +import org.eclipse.kapua.service.job.execution.JobExecutionFactory; +import org.eclipse.kapua.service.job.execution.JobExecutionQuery; +import org.eclipse.kapua.service.job.execution.JobExecutionService; import org.eclipse.kapua.service.job.internal.JobEntityManagerFactory; import org.eclipse.kapua.service.job.step.JobStep; import org.eclipse.kapua.service.job.step.JobStepAttributes; @@ -59,8 +64,12 @@ public class JobStepServiceImpl extends AbstractKapuaService private static final AuthorizationService AUTHORIZATION_SERVICE = LOCATOR.getService(AuthorizationService.class); private static final PermissionFactory PERMISSION_FACTORY = LOCATOR.getFactory(PermissionFactory.class); + private static final JobExecutionService JOB_EXECUTION_SERVICE = LOCATOR.getService(JobExecutionService.class); + private static final JobExecutionFactory JOB_EXECUTION_FACTORY = LOCATOR.getFactory(JobExecutionFactory.class); + private static final JobStepDefinitionService JOB_STEP_DEFINITION_SERVICE = LOCATOR.getService(JobStepDefinitionService.class); + public JobStepServiceImpl() { super(JobEntityManagerFactory.getInstance(), null); } @@ -110,6 +119,15 @@ public JobStep create(JobStepCreator jobStepCreator) throws KapuaException { throw new KapuaDuplicateNameException(jobStepCreator.getName()); } + // + // Check Job Executions + JobExecutionQuery jobExecutionQuery = JOB_EXECUTION_FACTORY.newQuery(jobStepCreator.getScopeId()); + jobExecutionQuery.attributePredicate(JobExecutionAttributes.JOB_ID, jobStepCreator.getJobId()); + + if (JOB_EXECUTION_SERVICE.count(jobExecutionQuery) > 0) { + throw new CannotModifyJobStepsException(jobStepCreator.getJobId()); + } + // // Check step index if (jobStepCreator.getStepIndex() == null) { @@ -193,6 +211,16 @@ public JobStep update(JobStep jobStep) throws KapuaException { throw new KapuaDuplicateNameException(jobStep.getName()); } + // + // Check Job Executions + JobExecutionQuery jobExecutionQuery = JOB_EXECUTION_FACTORY.newQuery(jobStep.getScopeId()); + jobExecutionQuery.attributePredicate(JobExecutionAttributes.JOB_ID, jobStep.getJobId()); + + if (JOB_EXECUTION_SERVICE.count(jobExecutionQuery) > 0) { + throw new CannotModifyJobStepsException(jobStep.getJobId()); + } + + // Do Update return entityManagerSession.doTransactedAction(em -> JobStepDAO.update(em, jobStep)); } @@ -255,10 +283,20 @@ public void delete(KapuaId scopeId, KapuaId jobStepId) throws KapuaException { // // Check existence - if (find(scopeId, jobStepId) == null) { + JobStep jobStep = find(scopeId, jobStepId); + if (jobStep == null) { throw new KapuaEntityNotFoundException(JobStep.TYPE, jobStepId); } + // + // Check Job Executions + JobExecutionQuery jobExecutionQuery = JOB_EXECUTION_FACTORY.newQuery(scopeId); + jobExecutionQuery.attributePredicate(JobExecutionAttributes.JOB_ID, jobStep.getJobId()); + + if (JOB_EXECUTION_SERVICE.count(jobExecutionQuery) > 0) { + throw new CannotModifyJobStepsException(jobStep.getJobId()); + } + // // Do delete entityManagerSession.doTransactedAction(em -> {