|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2020 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.feature.series.importing.event; |
| 19 | + |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import org.apache.commons.lang3.Validate; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import org.springframework.context.ApplicationEventPublisher; |
| 25 | +import org.springframework.context.ApplicationListener; |
| 26 | +import ru.mystamps.web.feature.series.DownloadResult; |
| 27 | +import ru.mystamps.web.feature.series.DownloaderService; |
| 28 | +import ru.mystamps.web.feature.series.importing.ImportRequestDto; |
| 29 | +import ru.mystamps.web.feature.series.importing.SeriesImportDb.SeriesImportRequestStatus; |
| 30 | +import ru.mystamps.web.feature.series.importing.SeriesImportService; |
| 31 | + |
| 32 | +/** |
| 33 | + * Listener of the {@link RetryDownloading} event. |
| 34 | + * |
| 35 | + * Downloads a file, saves it to database and publish the {@link DownloadingSucceeded} event. |
| 36 | + * |
| 37 | + * It is similar to {@link ImportRequestCreatedEventListener} with the following differences: |
| 38 | + * - it loads a request from database as we have only id |
| 39 | + * - it doesn't modify a request state when downloading fails |
| 40 | + * - it invokes {@code saveDownloadedContent()} with retry=true parameter |
| 41 | + * |
| 42 | + * @see ImportRequestCreatedEventListener |
| 43 | + * @see DownloadingSucceededEventListener |
| 44 | + */ |
| 45 | +@RequiredArgsConstructor |
| 46 | +public class RetryDownloadingEventListener implements ApplicationListener<RetryDownloading> { |
| 47 | + |
| 48 | + private static final Logger LOG = LoggerFactory.getLogger(RetryDownloadingEventListener.class); |
| 49 | + |
| 50 | + private final DownloaderService downloaderService; |
| 51 | + private final SeriesImportService seriesImportService; |
| 52 | + private final ApplicationEventPublisher eventPublisher; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onApplicationEvent(RetryDownloading event) { |
| 56 | + Integer requestId = event.getRequestId(); |
| 57 | + |
| 58 | + ImportRequestDto request = seriesImportService.findById(requestId); |
| 59 | + if (request == null) { |
| 60 | + // FIXME: how to handle error? maybe publish UnexpectedErrorEvent? |
| 61 | + LOG.error("Request #{}: couldn't retry is it doesn't exist", requestId); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + String status = request.getStatus(); |
| 66 | + if (SeriesImportRequestStatus.DOWNLOADING_FAILED.equals(status)) { |
| 67 | + LOG.warn("Request #{}: unexpected status '{}'. Abort a retry process", request, status); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + String url = request.getUrl(); |
| 72 | + LOG.info("Request #{}: retry downloading '{}'", requestId, url); |
| 73 | + |
| 74 | + DownloadResult result = downloaderService.download(url); |
| 75 | + if (result.hasFailed()) { |
| 76 | + LOG.info( |
| 77 | + "Request #{}: downloading of '{}' failed again: {}", |
| 78 | + requestId, |
| 79 | + url, |
| 80 | + result.getCode() |
| 81 | + ); |
| 82 | + |
| 83 | + // in case of failure we don't need to change request status as we assume that |
| 84 | + // it's already set to DownloadingFailed |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // FIXME: do we need updated_by field? |
| 89 | + seriesImportService.saveDownloadedContent(requestId, result.getDataAsString(), true); |
| 90 | + |
| 91 | + eventPublisher.publishEvent(new DownloadingSucceeded(this, requestId, url)); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments