added update constrain while job execution in propcess

added column for update constrain in jobs table
This commit is contained in:
Pramod Nuthakki
2013-07-22 17:02:14 +05:30
parent 7ee6a22df5
commit e3c6c84c3e
6 changed files with 56 additions and 3 deletions
@@ -0,0 +1,2 @@
ALTER TABLE `job`
ADD COLUMN `updates_allowed` TINYINT(1) NOT NULL DEFAULT '1' AFTER `currently_running`;
@@ -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();
}
}
@@ -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);
}
}
@@ -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 {
@@ -15,4 +15,6 @@ public interface SchedulerJobRunnerReadService {
public Page<JobDetailHistoryData> retrieveJobHistory(Long jobId, SearchParameters searchParameters);
public boolean isUpdatesAllowed();
}
@@ -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<JobDetailHistoryData> 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<String> 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<JobDetailData> {
private StringBuilder sqlBuilder = new StringBuilder("select")