Search in sources :

Example 1 with JobExecution

use of org.folio.rest.jaxrs.model.JobExecution in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method saveJobExecutions.

/**
 * Performs save for received JobExecution entities using {@link JobExecutionDao}
 *
 * @param jobExecutions list on JobExecution entities
 * @return future
 */
private Future<List<String>> saveJobExecutions(List<JobExecution> jobExecutions, String tenantId) {
    List<Future<String>> savedJobExecutionFutures = new ArrayList<>();
    for (JobExecution jobExecution : jobExecutions) {
        Future<String> savedJobExecutionFuture = jobExecutionDao.save(jobExecution, tenantId);
        savedJobExecutionFutures.add(savedJobExecutionFuture);
    }
    return GenericCompositeFuture.all(savedJobExecutionFutures).map(genericCompositeFuture -> genericCompositeFuture.result().list());
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) ArrayList(java.util.ArrayList) GenericCompositeFuture(org.folio.okapi.common.GenericCompositeFuture) Future(io.vertx.core.Future)

Example 2 with JobExecution

use of org.folio.rest.jaxrs.model.JobExecution in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method updateSnapshotStatus.

private Future<JobExecution> updateSnapshotStatus(JobExecution jobExecution, OkapiConnectionParams params) {
    Promise<JobExecution> promise = Promise.promise();
    Snapshot snapshot = new Snapshot().withJobExecutionId(jobExecution.getId()).withStatus(Snapshot.Status.fromValue(jobExecution.getStatus().name()));
    SourceStorageSnapshotsClient client = new SourceStorageSnapshotsClient(params.getOkapiUrl(), params.getTenantId(), params.getToken());
    try {
        client.putSourceStorageSnapshotsByJobExecutionId(jobExecution.getId(), null, snapshot, response -> {
            if (response.result().statusCode() == HttpStatus.HTTP_OK.toInt()) {
                promise.complete(jobExecution);
            } else {
                jobExecutionDao.updateBlocking(jobExecution.getId(), jobExec -> {
                    Promise<JobExecution> jobExecutionPromise = Promise.promise();
                    jobExec.setErrorStatus(JobExecution.ErrorStatus.SNAPSHOT_UPDATE_ERROR);
                    jobExec.setStatus(JobExecution.Status.ERROR);
                    jobExec.setUiStatus(JobExecution.UiStatus.ERROR);
                    jobExec.setCompletedDate(new Date());
                    jobExecutionPromise.complete(jobExec);
                    return jobExecutionPromise.future();
                }, params.getTenantId()).onComplete(jobExecutionUpdate -> {
                    String message = "Couldn't update snapshot status for jobExecution with id " + jobExecution.getId();
                    LOGGER.error(message);
                    promise.fail(message);
                });
            }
        });
    } catch (Exception e) {
        LOGGER.error("Error during update for Snapshot with id {}", jobExecution.getId(), e);
        promise.fail(e);
    }
    return promise.future();
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) DataImportProfilesClient(org.folio.rest.client.DataImportProfilesClient) Arrays(java.util.Arrays) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) UserInfo(org.folio.rest.jaxrs.model.UserInfo) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) JobExecutionSourceChunkDao(org.folio.dao.JobExecutionSourceChunkDao) JobExecution(org.folio.rest.jaxrs.model.JobExecution) JobProfileInfo(org.folio.rest.jaxrs.model.JobProfileInfo) InitJobExecutionsRsDto(org.folio.rest.jaxrs.model.InitJobExecutionsRsDto) ERROR(org.folio.rest.jaxrs.model.StatusDto.Status.ERROR) JsonObject(io.vertx.core.json.JsonObject) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) GenericCompositeFuture(org.folio.okapi.common.GenericCompositeFuture) StatusDto(org.folio.rest.jaxrs.model.StatusDto) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Logger(org.apache.logging.log4j.Logger) JobExecutionFilter(org.folio.dao.JobExecutionFilter) JobExecutionDtoCollection(org.folio.rest.jaxrs.model.JobExecutionDtoCollection) HTTP_CREATED(org.folio.HttpStatus.HTTP_CREATED) Optional(java.util.Optional) RestUtil(org.folio.dataimport.util.RestUtil) JobProfile(org.folio.rest.jaxrs.model.JobProfile) FilenameUtils(org.apache.commons.io.FilenameUtils) Progress(org.folio.rest.jaxrs.model.Progress) JobExecutionDao(org.folio.dao.JobExecutionDao) CANCELLED(org.folio.rest.jaxrs.model.StatusDto.Status.CANCELLED) DeleteJobExecutionsResp(org.folio.rest.jaxrs.model.DeleteJobExecutionsResp) ArrayList(java.util.ArrayList) SourceStorageSnapshotsClient(org.folio.rest.client.SourceStorageSnapshotsClient) Service(org.springframework.stereotype.Service) InitJobExecutionsRqDto(org.folio.rest.jaxrs.model.InitJobExecutionsRqDto) Promise(io.vertx.core.Promise) COMMITTED(org.folio.rest.jaxrs.model.JobExecution.Status.COMMITTED) RunBy(org.folio.rest.jaxrs.model.RunBy) File(org.folio.rest.jaxrs.model.File) SortField(org.folio.dao.util.SortField) HttpMethod(io.vertx.core.http.HttpMethod) JobExecutionUserInfoCollection(org.folio.rest.jaxrs.model.JobExecutionUserInfoCollection) JobProfileInfoCollection(org.folio.rest.jaxrs.model.JobProfileInfoCollection) HttpStatus(org.folio.HttpStatus) PROFILE_SNAPSHOT_CREATING_ERROR(org.folio.rest.jaxrs.model.StatusDto.ErrorStatus.PROFILE_SNAPSHOT_CREATING_ERROR) LogManager(org.apache.logging.log4j.LogManager) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Collections(java.util.Collections) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Promise(io.vertx.core.Promise) SourceStorageSnapshotsClient(org.folio.rest.client.SourceStorageSnapshotsClient) Date(java.util.Date) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) NotFoundException(javax.ws.rs.NotFoundException)

Example 3 with JobExecution

use of org.folio.rest.jaxrs.model.JobExecution in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method prepareJobExecutionList.

/**
 * Creates and returns list of JobExecution entities depending on received files.
 * In a case if only one file passed, method returns list with one JobExecution entity
 * signed by SINGLE_PARENT status.
 * In a case if N files passed (N > 1), method returns list with JobExecution entities
 * with one JobExecution entity signed by PARENT_MULTIPLE and N JobExecution entities signed by CHILD status.
 *
 * @param parentJobExecutionId id of the parent JobExecution entity
 * @param files                Representations of the Files user uploads
 * @param userInfo             The user creating JobExecution
 * @param dto                  {@link InitJobExecutionsRqDto}
 * @return list of JobExecution entities
 */
private List<JobExecution> prepareJobExecutionList(String parentJobExecutionId, List<File> files, UserInfo userInfo, InitJobExecutionsRqDto dto) {
    String userId = dto.getUserId();
    if (dto.getSourceType().equals(InitJobExecutionsRqDto.SourceType.ONLINE)) {
        JobProfileInfo jobProfileInfo = dto.getJobProfileInfo();
        if (jobProfileInfo != null && jobProfileInfo.getId().equals(DEFAULT_JOB_PROFILE_ID)) {
            jobProfileInfo.withName(DEFAULT_JOB_PROFILE);
        }
        return Collections.singletonList(buildNewJobExecution(true, true, parentJobExecutionId, null, userId).withJobProfileInfo(jobProfileInfo).withRunBy(buildRunByFromUserInfo(userInfo)));
    }
    List<JobExecution> result = new ArrayList<>();
    if (files.size() > 1) {
        for (File file : files) {
            result.add(buildNewJobExecution(false, false, parentJobExecutionId, file.getName(), userId));
        }
        result.add(buildNewJobExecution(true, false, parentJobExecutionId, null, userId));
    } else {
        File file = files.get(0);
        result.add(buildNewJobExecution(true, true, parentJobExecutionId, file.getName(), userId));
    }
    result.forEach(job -> job.setRunBy(buildRunByFromUserInfo(userInfo)));
    return result;
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) JobProfileInfo(org.folio.rest.jaxrs.model.JobProfileInfo) ArrayList(java.util.ArrayList) File(org.folio.rest.jaxrs.model.File)

Example 4 with JobExecution

use of org.folio.rest.jaxrs.model.JobExecution in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method initializeJobExecutions.

@Override
public Future<InitJobExecutionsRsDto> initializeJobExecutions(InitJobExecutionsRqDto jobExecutionsRqDto, OkapiConnectionParams params) {
    if (jobExecutionsRqDto.getSourceType().equals(InitJobExecutionsRqDto.SourceType.FILES) && jobExecutionsRqDto.getFiles().isEmpty()) {
        String errorMessage = "Received files must not be empty";
        LOGGER.error(errorMessage);
        return Future.failedFuture(new BadRequestException(errorMessage));
    } else {
        String parentJobExecutionId = UUID.randomUUID().toString();
        return lookupUser(jobExecutionsRqDto.getUserId(), params).compose(userInfo -> {
            List<JobExecution> jobExecutions = prepareJobExecutionList(parentJobExecutionId, jobExecutionsRqDto.getFiles(), userInfo, jobExecutionsRqDto);
            List<Snapshot> snapshots = prepareSnapshotList(jobExecutions);
            Future<List<String>> savedJsonExecutionsFuture = saveJobExecutions(jobExecutions, params.getTenantId());
            Future<List<String>> savedSnapshotsFuture = saveSnapshots(snapshots, params);
            return GenericCompositeFuture.all(Arrays.asList(savedJsonExecutionsFuture, savedSnapshotsFuture)).map(new InitJobExecutionsRsDto().withParentJobExecutionId(parentJobExecutionId).withJobExecutions(jobExecutions));
        });
    }
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) Snapshot(org.folio.rest.jaxrs.model.Snapshot) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) ArrayList(java.util.ArrayList) InitJobExecutionsRsDto(org.folio.rest.jaxrs.model.InitJobExecutionsRsDto)

Example 5 with JobExecution

use of org.folio.rest.jaxrs.model.JobExecution in project mod-source-record-manager by folio-org.

the class JobExecutionDaoImplTest method shouldNotDeleteIfJobHasNotSoftDeleted.

@Test
public void shouldNotDeleteIfJobHasNotSoftDeleted(TestContext context) {
    /*
      In this test we setup job execution that finishes 3 day ago, but this job has not been soft deleted.
      Periodic job should not hard delete it.
    */
    Async async = context.async();
    Instant minusThreeDays = LocalDateTime.now().minus(3, ChronoUnit.DAYS).atOffset(ZoneOffset.UTC).toInstant();
    Future<JobExecution> preparationFuture = prepareDataForDeletion(minusThreeDays);
    preparationFuture.onComplete(ar -> {
        JobExecution jobExecution = ar.result();
        List<String> jobExecutionIds = List.of(jobExecution.getId());
        jobExecutionDao.hardDeleteJobExecutions(2, TENANT_ID).onSuccess(resp -> checkDataExistenceAfterHardDeleting(1, jobExecutionIds, async));
    });
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) Async(io.vertx.ext.unit.Async) Instant(java.time.Instant) AbstractRestTest(org.folio.rest.impl.AbstractRestTest) Test(org.junit.Test)

Aggregations

JobExecution (org.folio.rest.jaxrs.model.JobExecution)128 Test (org.junit.Test)111 AbstractRestTest (org.folio.rest.impl.AbstractRestTest)99 InitJobExecutionsRsDto (org.folio.rest.jaxrs.model.InitJobExecutionsRsDto)73 Async (io.vertx.ext.unit.Async)68 Future (io.vertx.core.Future)55 JobProfileInfo (org.folio.rest.jaxrs.model.JobProfileInfo)55 List (java.util.List)54 Date (java.util.Date)53 UUID (java.util.UUID)52 Before (org.junit.Before)48 PostgresClientFactory (org.folio.dao.util.PostgresClientFactory)47 RunWith (org.junit.runner.RunWith)47 InjectMocks (org.mockito.InjectMocks)47 TestContext (io.vertx.ext.unit.TestContext)46 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)46 MockitoAnnotations (org.mockito.MockitoAnnotations)46 Spy (org.mockito.Spy)46 JournalRecordDaoImpl (org.folio.dao.JournalRecordDaoImpl)41 JournalRecord (org.folio.rest.jaxrs.model.JournalRecord)38