Search in sources :

Example 26 with ScheduleSecHubJob

use of com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob 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 27 with ScheduleSecHubJob

use of com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob in project sechub by mercedes-benz.

the class SchedulerRestartJobService method restartJob.

private void restartJob(UUID jobUUID, String ownerEmailAddress, boolean hard) {
    assertion.assertIsValidJobUUID(jobUUID);
    auditLogService.log("triggered restart of job:{}, variant:[hard={}]", jobUUID, hard);
    Optional<ScheduleSecHubJob> optJob = jobRepository.findById(jobUUID);
    if (!optJob.isPresent()) {
        LOG.warn("SecHub job {} not found, so not able to restart!", jobUUID);
        JobDataContext context = new JobDataContext();
        context.sechubJobUUID = jobUUID;
        context.ownerEmailAddress = ownerEmailAddress;
        context.info = "Restart canceled, because job not found!";
        sendJobRestartCanceled(context);
        throw new NotFoundException("Job not found or you have no access");
    }
    /* job exists, so can be restarted - hard or soft */
    ScheduleSecHubJob job = optJob.get();
    if (job.getExecutionResult().hasFinished()) {
        /* already done so just ignore */
        LOG.warn("SecHub job {} has already finished, so not able to restart!", jobUUID);
        sendJobRestartCanceled(job, ownerEmailAddress, "Restart canceled, because job already finished");
        throw new AlreadyExistsException("Job has already finished - restart not necessary");
    }
    /*
         * when we have still running batch jobs we must terminate them as well +
         * abandon
         */
    schedulerCancelJobService.stopAndAbandonAllRunningBatchJobsForSechubJobUUID(jobUUID);
    if (hard) {
        sendPurgeJobResultsSynchronousRequest(job);
    }
    ScheduleSecHubJob secHubJob = optJob.get();
    markJobAsNewExecutedNow(secHubJob);
    sendJobRestartTriggered(secHubJob, ownerEmailAddress);
    launcherService.executeJob(secHubJob);
    String type = (hard ? "hard" : "normal");
    LOG.info("job {} has been {} restarted", jobUUID, type);
}
Also used : AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)

Example 28 with ScheduleSecHubJob

use of com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob in project sechub by mercedes-benz.

the class SchedulerStartHandler method buildZombieInformation.

String buildZombieInformation(List<ScheduleSecHubJob> jobsRunningButStartedBefore) {
    if (jobsRunningButStartedBefore == null || jobsRunningButStartedBefore.isEmpty()) {
        return "OK: No zombie jobs found";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("---------------------------------------\n");
    sb.append("ATTENTION: Potential zombie jobs found:\n");
    sb.append("---------------------------------------\n");
    for (ScheduleSecHubJob potentialZombieJob : jobsRunningButStartedBefore) {
        sb.append("- job:").append(potentialZombieJob.getUUID());
        sb.append(", started:").append(potentialZombieJob.getStarted());
        sb.append(", execution-state:").append(potentialZombieJob.getExecutionState());
        sb.append("\n");
    }
    sb.append("\nPlease check if they have been already started by another before started (and still running) scheduler instance. If identified as zombie job you have to restart the job!");
    return sb.toString();
}
Also used : ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)

Example 29 with ScheduleSecHubJob

use of com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob in project sechub by mercedes-benz.

the class ScheduleJobMarkerService method markNextJobToExecuteByThisInstance.

/**
 * @return either schedule job to execute, or <code>null</code> if no one has to
 *         be executed
 */
@Transactional
public ScheduleSecHubJob markNextJobToExecuteByThisInstance() {
    schedulerStrategy = schedulerStrategyFactory.build();
    if (LOG.isTraceEnabled()) {
        /* NOSONAR */
        LOG.trace("Trigger execution of next job started");
    }
    UUID nextJobId = schedulerStrategy.nextJobId();
    if (nextJobId == null) {
        return null;
    }
    Optional<ScheduleSecHubJob> secHubJobOptional = jobRepository.getJob(nextJobId);
    if (!secHubJobOptional.isPresent()) {
        if (LOG.isTraceEnabled()) {
            /* NOSONAR */
            LOG.trace("No job found.");
        }
        return null;
    }
    ScheduleSecHubJob secHubJob = secHubJobOptional.get();
    secHubJob.setExecutionState(ExecutionState.STARTED);
    secHubJob.setStarted(LocalDateTime.now());
    return jobRepository.save(secHubJob);
}
Also used : UUID(java.util.UUID) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with ScheduleSecHubJob

use of com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob in project sechub by mercedes-benz.

the class IntegrationTestSchedulerService method revertJobAsStillNotApproved.

public void revertJobAsStillNotApproved(UUID sechubJobUUID) {
    Optional<ScheduleSecHubJob> found = repository.findById(sechubJobUUID);
    if (!found.isPresent()) {
        throw new NotFoundException("Job not found!");
    }
    ScheduleSecHubJob job = found.get();
    job.setExecutionResult(ExecutionResult.NONE);
    job.setExecutionState(ExecutionState.INITIALIZING);
    job.setEnded(null);
    job.setTrafficLight(null);
    repository.save(job);
}
Also used : NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)

Aggregations

ScheduleSecHubJob (com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)30 Test (org.junit.Test)12 UUID (java.util.UUID)6 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)5 ScheduleJobStatus (com.mercedesbenz.sechub.domain.schedule.ScheduleJobStatus)4 UseCaseRestDoc (com.mercedesbenz.sechub.sharedkernel.usecases.UseCaseRestDoc)4 NotAcceptableException (com.mercedesbenz.sechub.sharedkernel.error.NotAcceptableException)3 NotFoundException (com.mercedesbenz.sechub.sharedkernel.error.NotFoundException)3 AuditLogService (com.mercedesbenz.sechub.sharedkernel.logging.AuditLogService)3 LogSanitizer (com.mercedesbenz.sechub.sharedkernel.logging.LogSanitizer)3 ChecksumSHA256Service (com.mercedesbenz.sechub.sharedkernel.util.ChecksumSHA256Service)3 UserInputAssertion (com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion)3 JobStorage (com.mercedesbenz.sechub.storage.core.JobStorage)3 StorageService (com.mercedesbenz.sechub.storage.core.StorageService)3 InputStream (java.io.InputStream)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)3 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)2 UseCaseSchedulerStartsJob (com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseSchedulerStartsJob)2 UseCaseUserApprovesJob (com.mercedesbenz.sechub.sharedkernel.usecases.user.execute.UseCaseUserApprovesJob)2