001package ca.uhn.fhir.jpa.bulk.export.job;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.batch.config.BatchConstants;
024import ca.uhn.fhir.jpa.bulk.export.model.BulkExportJobStatusEnum;
025import ca.uhn.fhir.jpa.bulk.export.svc.BulkExportDaoSvc;
026import org.springframework.batch.core.ExitStatus;
027import org.springframework.batch.core.StepExecution;
028import org.springframework.batch.core.StepExecutionListener;
029import org.springframework.beans.factory.annotation.Autowired;
030
031import javax.annotation.Nonnull;
032
033import static org.apache.commons.lang3.StringUtils.isNotBlank;
034
035/**
036 * This class sets the job status to ERROR if any failures occur while actually
037 * generating the export files.
038 */
039public class BulkExportGenerateResourceFilesStepListener implements StepExecutionListener {
040
041        @Autowired
042        private BulkExportDaoSvc myBulkExportDaoSvc;
043
044        @Override
045        public void beforeStep(@Nonnull StepExecution stepExecution) {
046                // nothing
047        }
048
049        @Override
050        public ExitStatus afterStep(StepExecution theStepExecution) {
051                if (theStepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())) {
052                        //Try to fetch it from the parameters first, and if it doesn't exist, fetch it from the context.
053                        String jobUuid = theStepExecution.getJobExecution().getJobParameters().getString(BatchConstants.JOB_UUID_PARAMETER);
054                        if (jobUuid == null) {
055                                jobUuid = theStepExecution.getJobExecution().getExecutionContext().getString(BatchConstants.JOB_UUID_PARAMETER);
056                        }
057                        assert isNotBlank(jobUuid);
058                        String exitDescription = theStepExecution.getExitStatus().getExitDescription();
059                        myBulkExportDaoSvc.setJobToStatus(jobUuid, BulkExportJobStatusEnum.ERROR, exitDescription);
060                }
061                return theStepExecution.getExitStatus();
062        }
063}