Search in sources :

Example 46 with JobEntity

use of org.camunda.bpm.engine.impl.persistence.entity.JobEntity in project camunda-bpm-platform by camunda.

the class AcquireJobsCmd method execute.

public AcquiredJobs execute(CommandContext commandContext) {
    acquiredJobs = new AcquiredJobs(numJobsToAcquire);
    List<JobEntity> jobs = commandContext.getJobManager().findNextJobsToExecute(new Page(0, numJobsToAcquire));
    Map<String, List<String>> exclusiveJobsByProcessInstance = new HashMap<String, List<String>>();
    for (JobEntity job : jobs) {
        lockJob(job);
        if (job.isExclusive()) {
            List<String> list = exclusiveJobsByProcessInstance.get(job.getProcessInstanceId());
            if (list == null) {
                list = new ArrayList<String>();
                exclusiveJobsByProcessInstance.put(job.getProcessInstanceId(), list);
            }
            list.add(job.getId());
        } else {
            acquiredJobs.addJobIdBatch(job.getId());
        }
    }
    for (List<String> jobIds : exclusiveJobsByProcessInstance.values()) {
        acquiredJobs.addJobIdBatch(jobIds);
    }
    // register an OptimisticLockingListener which is notified about jobs which cannot be acquired.
    // the listener removes them from the list of acquired jobs.
    commandContext.getDbEntityManager().registerOptimisticLockingListener(this);
    return acquiredJobs;
}
Also used : JobEntity(org.camunda.bpm.engine.impl.persistence.entity.JobEntity) AcquiredJobs(org.camunda.bpm.engine.impl.jobexecutor.AcquiredJobs) Page(org.camunda.bpm.engine.impl.Page)

Example 47 with JobEntity

use of org.camunda.bpm.engine.impl.persistence.entity.JobEntity in project camunda-bpm-platform by camunda.

the class AbstractSetJobStateCmd method checkAuthorization.

@Override
protected void checkAuthorization(CommandContext commandContext) {
    for (CommandChecker checker : commandContext.getProcessEngineConfiguration().getCommandCheckers()) {
        if (jobId != null) {
            JobManager jobManager = commandContext.getJobManager();
            JobEntity job = jobManager.findJobById(jobId);
            if (job != null) {
                String processInstanceId = job.getProcessInstanceId();
                if (processInstanceId != null) {
                    checker.checkUpdateProcessInstanceById(processInstanceId);
                } else {
                    // start timer job is not assigned to a specific process
                    // instance, that's why we have to check whether there
                    // exists a UPDATE_INSTANCES permission on process definition or
                    // a UPDATE permission on any process instance
                    String processDefinitionKey = job.getProcessDefinitionKey();
                    if (processDefinitionKey != null) {
                        checker.checkUpdateProcessInstanceByProcessDefinitionKey(processDefinitionKey);
                    }
                }
            // if (processInstanceId == null && processDefinitionKey == null):
            // job is not assigned to any process instance nor process definition
            // then it is always possible to activate/suspend the corresponding job
            // -> no authorization check necessary
            }
        } else if (jobDefinitionId != null) {
            JobDefinitionManager jobDefinitionManager = commandContext.getJobDefinitionManager();
            JobDefinitionEntity jobDefinition = jobDefinitionManager.findById(jobDefinitionId);
            if (jobDefinition != null) {
                String processDefinitionKey = jobDefinition.getProcessDefinitionKey();
                checker.checkUpdateProcessInstanceByProcessDefinitionKey(processDefinitionKey);
            }
        } else if (processInstanceId != null) {
            checker.checkUpdateProcessInstanceById(processInstanceId);
        } else if (processDefinitionId != null) {
            checker.checkUpdateProcessInstanceByProcessDefinitionId(processDefinitionId);
        } else if (processDefinitionKey != null) {
            checker.checkUpdateProcessInstanceByProcessDefinitionKey(processDefinitionKey);
        }
    }
}
Also used : JobEntity(org.camunda.bpm.engine.impl.persistence.entity.JobEntity) JobDefinitionEntity(org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity) JobDefinitionManager(org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionManager) JobManager(org.camunda.bpm.engine.impl.persistence.entity.JobManager) CommandChecker(org.camunda.bpm.engine.impl.cfg.CommandChecker)

Example 48 with JobEntity

use of org.camunda.bpm.engine.impl.persistence.entity.JobEntity in project camunda-bpm-platform by camunda.

the class ExecuteJobsCmd method execute.

public Void execute(CommandContext commandContext) {
    ensureNotNull("jobId", jobId);
    final JobEntity job = commandContext.getDbEntityManager().selectById(JobEntity.class, jobId);
    final ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    final IdentityService identityService = processEngineConfiguration.getIdentityService();
    final JobExecutorContext jobExecutorContext = Context.getJobExecutorContext();
    if (job == null) {
        if (jobExecutorContext != null) {
            // CAM-1842
            // Job was acquired but does not exist anymore. This is not a problem.
            // It usually means that the job has been deleted after it was acquired which can happen if the
            // the activity instance corresponding to the job is cancelled.
            LOG.debugAcquiredJobNotFound(jobId);
            return null;
        } else {
            throw LOG.jobNotFoundException(jobId);
        }
    }
    jobFailureCollector.setJob(job);
    if (jobExecutorContext == null) {
        // if null, then we are not called by the job executor
        for (CommandChecker checker : commandContext.getProcessEngineConfiguration().getCommandCheckers()) {
            checker.checkUpdateJob(job);
        }
    } else {
        jobExecutorContext.setCurrentJob(job);
        // if the job is called by the job executor then set the tenant id of the job
        // as authenticated tenant to enable tenant checks
        String tenantId = job.getTenantId();
        if (tenantId != null) {
            identityService.setAuthentication(null, null, Collections.singletonList(tenantId));
        }
    }
    try {
        // register as command context close lister to intercept exceptions on flush
        commandContext.registerCommandContextListener(jobFailureCollector);
        commandContext.setCurrentJob(job);
        job.execute(commandContext);
    } finally {
        if (jobExecutorContext != null) {
            jobExecutorContext.setCurrentJob(null);
            identityService.clearAuthentication();
        }
    }
    return null;
}
Also used : JobEntity(org.camunda.bpm.engine.impl.persistence.entity.JobEntity) IdentityService(org.camunda.bpm.engine.IdentityService) JobExecutorContext(org.camunda.bpm.engine.impl.jobexecutor.JobExecutorContext) ProcessEngineConfigurationImpl(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) CommandChecker(org.camunda.bpm.engine.impl.cfg.CommandChecker)

Example 49 with JobEntity

use of org.camunda.bpm.engine.impl.persistence.entity.JobEntity in project camunda-bpm-platform by camunda.

the class DefaultJobRetryCmd method executeStandardStrategy.

protected void executeStandardStrategy(CommandContext commandContext) {
    JobEntity job = getJob();
    if (job != null) {
        job.unlock();
        logException(job);
        decrementRetries(job);
        notifyAcquisition(commandContext);
    } else {
        LOG.debugFailedJobNotFound(jobId);
    }
}
Also used : JobEntity(org.camunda.bpm.engine.impl.persistence.entity.JobEntity)

Example 50 with JobEntity

use of org.camunda.bpm.engine.impl.persistence.entity.JobEntity in project camunda-bpm-platform by camunda.

the class BulkHistoryDeleteCmmnDisabledTest method clearDatabase.

@After
public void clearDatabase() {
    engineRule.getProcessEngineConfiguration().getCommandExecutorTxRequired().execute(new Command<Void>() {

        public Void execute(CommandContext commandContext) {
            List<Job> jobs = engineRule.getManagementService().createJobQuery().list();
            if (jobs.size() > 0) {
                assertEquals(1, jobs.size());
                String jobId = jobs.get(0).getId();
                commandContext.getJobManager().deleteJob((JobEntity) jobs.get(0));
                commandContext.getHistoricJobLogManager().deleteHistoricJobLogByJobId(jobId);
            }
            commandContext.getMeterLogManager().deleteAll();
            return null;
        }
    });
    List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().list();
    for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
        historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
    }
    List<HistoricDecisionInstance> historicDecisionInstances = historyService.createHistoricDecisionInstanceQuery().list();
    for (HistoricDecisionInstance historicDecisionInstance : historicDecisionInstances) {
        historyService.deleteHistoricDecisionInstanceByInstanceId(historicDecisionInstance.getId());
    }
}
Also used : JobEntity(org.camunda.bpm.engine.impl.persistence.entity.JobEntity) CommandContext(org.camunda.bpm.engine.impl.interceptor.CommandContext) HistoricProcessInstance(org.camunda.bpm.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) List(java.util.List) HistoricDecisionInstance(org.camunda.bpm.engine.history.HistoricDecisionInstance) After(org.junit.After)

Aggregations

JobEntity (org.camunda.bpm.engine.impl.persistence.entity.JobEntity)68 Test (org.junit.Test)26 Date (java.util.Date)18 Job (org.camunda.bpm.engine.runtime.Job)13 CommandContext (org.camunda.bpm.engine.impl.interceptor.CommandContext)11 HistoryCleanupJobHandlerConfiguration (org.camunda.bpm.engine.impl.jobexecutor.historycleanup.HistoryCleanupJobHandlerConfiguration)9 Deployment (org.camunda.bpm.engine.test.Deployment)9 ProcessInstance (org.camunda.bpm.engine.runtime.ProcessInstance)7 SimpleDateFormat (java.text.SimpleDateFormat)6 List (java.util.List)6 CommandChecker (org.camunda.bpm.engine.impl.cfg.CommandChecker)6 Page (org.camunda.bpm.engine.impl.Page)5 JobDefinitionEntity (org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity)5 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)4 HistoricDecisionInstance (org.camunda.bpm.engine.history.HistoricDecisionInstance)4 HistoricProcessInstance (org.camunda.bpm.engine.history.HistoricProcessInstance)4 ExecutionEntity (org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity)4 JobManager (org.camunda.bpm.engine.impl.persistence.entity.JobManager)4 ArrayList (java.util.ArrayList)3 HistoricCaseInstance (org.camunda.bpm.engine.history.HistoricCaseInstance)3