From e3c6c84c3e77191f107bef8eb8d77322d30b6a2d Mon Sep 17 00:00:00 2001 From: Pramod Nuthakki Date: Mon, 22 Jul 2013 16:54:50 +0530 Subject: [PATCH] added update constrain while job execution in propcess added column for update constrain in jobs table --- ...ed_update_constrain_for_scheduler_jobs.sql | 2 ++ ...CommandSourceWritePlatformServiceImpl.java | 13 +++++++++- .../OperationNotAllowedException.java | 14 +++++++++++ .../jobs/service/JobRegisterServiceImpl.java | 3 ++- .../SchedulerJobRunnerReadService.java | 2 ++ .../SchedulerJobRunnerReadServiceImpl.java | 25 ++++++++++++++++++- 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100755 mifosng-db/migrations/core_db/V88__added_update_constrain_for_scheduler_jobs.sql create mode 100755 mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/exception/OperationNotAllowedException.java diff --git a/mifosng-db/migrations/core_db/V88__added_update_constrain_for_scheduler_jobs.sql b/mifosng-db/migrations/core_db/V88__added_update_constrain_for_scheduler_jobs.sql new file mode 100755 index 000000000..c73bc936f --- /dev/null +++ b/mifosng-db/migrations/core_db/V88__added_update_constrain_for_scheduler_jobs.sql @@ -0,0 +1,2 @@ +ALTER TABLE `job` + ADD COLUMN `updates_allowed` TINYINT(1) NOT NULL DEFAULT '1' AFTER `currently_running`; \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java index 57a6073e1..29fdbf742 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java @@ -14,6 +14,7 @@ import org.mifosplatform.commands.exception.RollbackTransactionAsCommandIsNotApp import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.infrastructure.jobs.service.SchedulerJobRunnerReadService; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -28,15 +29,17 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio private final CommandSourceRepository commandSourceRepository; private final FromJsonHelper fromApiJsonHelper; private final CommandProcessingService processAndLogCommandService; + private final SchedulerJobRunnerReadService schedulerJobRunnerReadService; @Autowired public PortfolioCommandSourceWritePlatformServiceImpl(final PlatformSecurityContext context, final CommandSourceRepository commandSourceRepository, final FromJsonHelper fromApiJsonHelper, - final CommandProcessingService processAndLogCommandService) { + final CommandProcessingService processAndLogCommandService, final SchedulerJobRunnerReadService schedulerJobRunnerReadService) { this.context = context; this.commandSourceRepository = commandSourceRepository; this.fromApiJsonHelper = fromApiJsonHelper; this.processAndLogCommandService = processAndLogCommandService; + this.schedulerJobRunnerReadService = schedulerJobRunnerReadService; } @Override @@ -55,6 +58,7 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio // permission to perform specific task. context.authenticatedUser().validateHasPermissionTo(wrapper.getTaskPermissionName()); } + validateIsUpdateAllowed(); final String json = wrapper.getJson(); CommandProcessingResult result = null; @@ -78,6 +82,7 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio public CommandProcessingResult approveEntry(final Long makerCheckerId) { CommandSource commandSourceInput = validateMakerCheckerTransaction(makerCheckerId); + validateIsUpdateAllowed(); final CommandWrapper wrapper = CommandWrapper.fromExistingCommand(makerCheckerId, commandSourceInput.getActionName(), commandSourceInput.getEntityName(), commandSourceInput.resourceId(), commandSourceInput.subresourceId(), @@ -97,6 +102,7 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio public Long deleteEntry(final Long makerCheckerId) { validateMakerCheckerTransaction(makerCheckerId); + validateIsUpdateAllowed(); this.commandSourceRepository.delete(makerCheckerId); @@ -113,4 +119,9 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio return commandSourceInput; } + + private boolean validateIsUpdateAllowed() { + return schedulerJobRunnerReadService.isUpdatesAllowed(); + + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/exception/OperationNotAllowedException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/exception/OperationNotAllowedException.java new file mode 100755 index 000000000..e6d36d6aa --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/exception/OperationNotAllowedException.java @@ -0,0 +1,14 @@ +package org.mifosplatform.infrastructure.jobs.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException; + +/** + * A {@link RuntimeException} thrown when Job execution is in progress. + */ +public class OperationNotAllowedException extends AbstractPlatformResourceNotFoundException { + + public OperationNotAllowedException(final String jobNames) { + super("error.msg.sheduler.job.currently.running", "Exicution is in-process for jobs " + jobNames + + "...., so update operations are not allowed at this moment", jobNames); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java index 6ec7c7eec..06190c20f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java @@ -125,6 +125,7 @@ public class JobRegisterServiceImpl implements JobRegisterService { private void scheduleJob(ScheduledJobDetail scheduledJobDetails) { if (!scheduledJobDetails.isActiveSchedular()) { scheduledJobDetails.updateNextRunTime(null); + scheduledJobDetails.updateCurrentlyRunningStatus(false); return; } try { @@ -133,13 +134,13 @@ public class JobRegisterServiceImpl implements JobRegisterService { schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, trigger); scheduledJobDetails.updateJobKey(getJobKeyAsString(jobDetail.getKey())); scheduledJobDetails.updateNextRunTime(trigger.getNextFireTime()); - scheduledJobDetails.updateCurrentlyRunningStatus(false); scheduledJobDetails.updateErrorLog(null); } catch (Throwable throwable) { scheduledJobDetails.updateNextRunTime(null); String stackTrace = getStackTraceAsString(throwable); scheduledJobDetails.updateErrorLog(stackTrace); } + scheduledJobDetails.updateCurrentlyRunningStatus(false); } private JobDetail createJobDetail(ScheduledJobDetail scheduledJobDetails) throws Exception { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadService.java index 2609f426a..236cba8e7 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadService.java @@ -15,4 +15,6 @@ public interface SchedulerJobRunnerReadService { public Page retrieveJobHistory(Long jobId, SearchParameters searchParameters); + public boolean isUpdatesAllowed(); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadServiceImpl.java index 74091975c..f43ba6a2c 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/SchedulerJobRunnerReadServiceImpl.java @@ -11,6 +11,7 @@ import org.mifosplatform.infrastructure.core.service.RoutingDataSource; import org.mifosplatform.infrastructure.jobs.data.JobDetailData; import org.mifosplatform.infrastructure.jobs.data.JobDetailHistoryData; import org.mifosplatform.infrastructure.jobs.exception.JobNotFoundException; +import org.mifosplatform.infrastructure.jobs.exception.OperationNotAllowedException; import org.mifosplatform.portfolio.group.service.SearchParameters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; @@ -52,7 +53,7 @@ public class SchedulerJobRunnerReadServiceImpl implements SchedulerJobRunnerRead @Override public Page retrieveJobHistory(Long jobId, SearchParameters searchParameters) { - retrieveOne(jobId); + if (!isJobExist(jobId)) { throw new JobNotFoundException(String.valueOf(jobId)); } JobHistoryMapper jobHistoryMapper = new JobHistoryMapper(); StringBuilder sqlBuilder = new StringBuilder(jobHistoryMapper.schema()); sqlBuilder.append(" where job.id=?"); @@ -75,6 +76,28 @@ public class SchedulerJobRunnerReadServiceImpl implements SchedulerJobRunnerRead return paginationHelper.fetchPage(jdbcTemplate, sqlCountRows, sqlBuilder.toString(), new Object[] { jobId }, jobHistoryMapper); } + @Override + public boolean isUpdatesAllowed() { + String sql = "select job.display_name from job job where job.currently_running=true and job.updates_allowed=false"; + List names = jdbcTemplate.queryForList(sql, String.class); + if (names != null && names.size() > 0) { + String listVals = names.toString(); + String jobNames = listVals.substring(listVals.indexOf("[") + 1, listVals.indexOf("]")); + throw new OperationNotAllowedException(jobNames); + } + return true; + } + + private boolean isJobExist(Long jobId) { + boolean isJobPresent = false; + String sql = "select count(*) from job job where job.id=" + jobId; + int count = jdbcTemplate.queryForInt(sql); + if (count == 1) { + isJobPresent = true; + } + return isJobPresent; + } + private static final class JobDetailMapper implements RowMapper { private StringBuilder sqlBuilder = new StringBuilder("select")