Search in sources :

Example 1 with UseCaseSchedulerStartsJob

use of com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob in project sechub by mercedes-benz.

the class JobInformationCreateService method createByMessage.

@Validated
@UseCaseSchedulerStartsJob(@Step(number = 4, name = "Store admin job info", description = "Fetches event about started job and store info in admin domain."))
public void createByMessage(JobMessage message, JobStatus status) {
    String projectId = message.getProjectId();
    UUID jobUUID = message.getJobUUID();
    assertion.assertIsValidProjectId(projectId);
    assertion.assertIsValidJobUUID(jobUUID);
    LOG.debug("creating a new job information entry for project={}, job={}", projectId, jobUUID);
    JobInformation entity = new JobInformation();
    entity.setProjectId(projectId);
    entity.setJobUUID(jobUUID);
    entity.setConfiguration(message.getConfiguration());
    entity.setOwner(message.getOwner());
    entity.setSince(message.getSince());
    entity.setStatus(status);
    entity = repository.save(entity);
    LOG.debug("saved new job information entry uuid={} - for project={}, job={}, ", projectId, jobUUID, entity.getUUID());
}
Also used : UUID(java.util.UUID) UseCaseSchedulerStartsJob(com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob) Validated(org.springframework.validation.annotation.Validated)

Example 2 with UseCaseSchedulerStartsJob

use of com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob in project sechub by mercedes-benz.

the class ScanExecutionTasklet method execute.

@Override
@UseCaseSchedulerStartsJob(@Step(number = 3, next = 5, name = "Batch Job", description = "usecases/job/scheduler_starts_job_tasklet.adoc"))
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    Long batchJobId = contribution.getStepExecution().getJobExecution().getJobId();
    JobParameters jobParameters = this.scope.getJobExecution().getJobParameters();
    LOG.debug("executing with parameters:{}", jobParameters);
    String secHubJobUUIDAsString = jobParameters.getString(SchedulingConstants.BATCHPARAM_SECHUB_UUID);
    UUID secHubJobUUID = UUID.fromString(secHubJobUUIDAsString);
    ScheduleSecHubJob sechubJob = null;
    try {
        sechubJob = scope.getSecHubJobRepository().getById(secHubJobUUID);
        /* execute sechub job synchron */
        SynchronSecHubJobExecutor trigger = new SynchronSecHubJobExecutor(scope.getEventBusService(), scope.getSecHubJobUpdater());
        trigger.execute(sechubJob, batchJobId);
    } catch (Exception e) {
        LOG.error("Error happend at spring batch task execution:" + e.getMessage(), e);
        markSechHubJobFailed(secHubJobUUID);
        sendJobFailed(secHubJobUUID);
    }
    return RepeatStatus.FINISHED;
}
Also used : JobParameters(org.springframework.batch.core.JobParameters) UUID(java.util.UUID) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob) UseCaseSchedulerStartsJob(com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob)

Example 3 with UseCaseSchedulerStartsJob

use of com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob in project sechub by mercedes-benz.

the class SchedulerJobBatchTriggerService method triggerExecutionOfNextJob.

// default 10 seconds delay and 5 seconds initial
@MustBeDocumented(value = "Job scheduling is triggered by a cron job operation - default is 10 seconds to delay after last execution. " + "For initial delay " + DEFAULT_INITIAL_DELAY_MILLIS + " milliseconds are defined. It can be configured differently. This is useful when you need to startup a cluster. Simply change the initial delay values in to allow the cluster to startup.", scope = "schedule")
@Scheduled(initialDelayString = "${sechub.config.trigger.nextjob.initialdelay:" + DEFAULT_INITIAL_DELAY_MILLIS + "}", fixedDelayString = "${sechub.config.trigger.nextjob.delay:" + DEFAULT_FIXED_DELAY_MILLIS + "}")
@UseCaseSchedulerStartsJob(@Step(number = 1, name = "Scheduling", description = "Fetches next schedule job from queue and trigger execution."))
public void triggerExecutionOfNextJob() {
    if (LOG.isTraceEnabled()) {
        /* NOSONAR */
        LOG.trace("Trigger execution of next job started. Environment: {}", environmentService.getEnvironment());
    }
    if (!configService.isJobProcessingEnabled()) {
        LOG.warn("Job processing is disabled, so cancel scheduling. Environment: {}", environmentService.getEnvironment());
        return;
    }
    if (healthCheckEnabled) {
        if (monitorService.isCPULoadAverageMaxReached()) {
            alertLogService.log(SCHEDULER_PROBLEM, CPU_OVERLOAD, "Job processing is skipped. {}, {}", monitorService.createCPUDescription(), environmentService.getEnvironment());
            return;
        }
        if (monitorService.isMemoryUsageMaxReached()) {
            alertLogService.log(SCHEDULER_PROBLEM, MEMORY_OVERLOAD, "Job processing is skipped. {}, {}", monitorService.createMemoryDescription(), environmentService.getEnvironment());
            return;
        }
    }
    RetryContext retryContext = new RetryContext(markNextJobRetries);
    do {
        try {
            ScheduleSecHubJob next = markerService.markNextJobToExecuteByThisInstance();
            retryContext.executionDone();
            if (next == null) {
                return;
            }
            try {
                launcherService.executeJob(next);
            } catch (Exception e) {
                /* fatal failure happened, job launch was not executable */
                LOG.trace("was not able to execute next job, because fatal error occurred. Environment: {}", environmentService.getEnvironment());
                markerService.markJobExecutionFailed(next);
                retryContext.markAsFatalFailure();
            }
        } catch (OptimisticLockingFailureException e) {
            LOG.trace("was not able to trigger next, because already done. Environment: {}", environmentService.getEnvironment());
            retryContext.setRetryTimeToWait(createRandomTimeMillisToWait()).executionFailed();
        } catch (Exception e) {
            LOG.trace("was not able to trigger next job, because fatal error occurred. Environment: {}", environmentService.getEnvironment());
            retryContext.markAsFatalFailure();
        }
    } while (retryContext.isRetryPossible());
    if (!retryContext.isExecutionDone()) {
        LOG.warn("Was not able to handle trigger execution of next job, failed {} times. Environment:{}", retryContext.getExecutionFailedCount(), environmentService.getEnvironment());
    }
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) Scheduled(org.springframework.scheduling.annotation.Scheduled) UseCaseSchedulerStartsJob(com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob) MustBeDocumented(com.mercedesbenz.sechub.sharedkernel.MustBeDocumented)

Example 4 with UseCaseSchedulerStartsJob

use of com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob in project sechub by mercedes-benz.

the class ScheduleJobLauncherService method executeJob.

@UseCaseSchedulerStartsJob(@Step(number = 2, next = { 3, 4 }, name = "Execution", description = "Starts a spring boot batch job which does execute the scan asynchronous. If spring boot batch job cannot be started the next steps will not be executed."))
public void executeJob(ScheduleSecHubJob secHubJob) {
    UUID secHubJobUUID = secHubJob.getUUID();
    LOG.debug("Execute job:{}", secHubJobUUID);
    try {
        /* prepare batch job */
        JobParameters jobParameters = parameterBuilder.buildParams(secHubJobUUID);
        /* launch batch job */
        LOG.debug("Trigger batch job launch: {}", secHubJobUUID);
        JobExecution execution = jobLauncher.run(job, jobParameters);
        /* job is launched - inspect batch job internal id */
        Long batchJobId = execution.getJobId();
        LOG.debug("Execution triggered: {} has batch-ID: {}", secHubJobUUID, batchJobId);
        /* send domain event */
        sendJobStarted(secHubJob.getProjectId(), secHubJobUUID, secHubJob.getJsonConfiguration(), secHubJob.getOwner());
    } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
        /*
             * we do not need to send a "jobEnded" event, because in this case job was never
             * started
             */
        LOG.error("Not able to run batch job for sechhub: {}", secHubJobUUID);
        throw new ScheduleFailedException(e);
    }
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) JobInstanceAlreadyCompleteException(org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException) JobExecutionAlreadyRunningException(org.springframework.batch.core.repository.JobExecutionAlreadyRunningException) JobParameters(org.springframework.batch.core.JobParameters) JobParametersInvalidException(org.springframework.batch.core.JobParametersInvalidException) UUID(java.util.UUID) JobRestartException(org.springframework.batch.core.repository.JobRestartException) UseCaseSchedulerStartsJob(com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob)

Aggregations

UseCaseSchedulerStartsJob (com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob)4 UUID (java.util.UUID)3 ScheduleSecHubJob (com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)2 JobParameters (org.springframework.batch.core.JobParameters)2 MustBeDocumented (com.mercedesbenz.sechub.sharedkernel.MustBeDocumented)1 JobExecution (org.springframework.batch.core.JobExecution)1 JobParametersInvalidException (org.springframework.batch.core.JobParametersInvalidException)1 JobExecutionAlreadyRunningException (org.springframework.batch.core.repository.JobExecutionAlreadyRunningException)1 JobInstanceAlreadyCompleteException (org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException)1 JobRestartException (org.springframework.batch.core.repository.JobRestartException)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Validated (org.springframework.validation.annotation.Validated)1