generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from pagopa/PRDP-267-add-time-trigger-function…
…-for-recover-failed [PRDP-267] feat: Add timer trigger function for recovering failed receipt
- Loading branch information
Showing
13 changed files
with
400 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package it.gov.pagopa.receipt.pdf.helpdesk; | ||
|
||
import com.microsoft.azure.functions.ExecutionContext; | ||
import com.microsoft.azure.functions.OutputBinding; | ||
import com.microsoft.azure.functions.annotation.CosmosDBOutput; | ||
import com.microsoft.azure.functions.annotation.FunctionName; | ||
import com.microsoft.azure.functions.annotation.TimerTrigger; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.client.BizEventCosmosClient; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.BizEventCosmosClientImpl; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.model.MassiveRecoverResult; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.service.BizEventToReceiptService; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.service.impl.BizEventToReceiptServiceImpl; | ||
import it.gov.pagopa.receipt.pdf.helpdesk.service.impl.ReceiptCosmosServiceImpl; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import static it.gov.pagopa.receipt.pdf.helpdesk.utils.BizEventToReceiptUtils.massiveRecoverByStatus; | ||
|
||
/** | ||
* Azure Functions with Timer trigger. | ||
*/ | ||
public class RecoverFailedReceiptScheduled { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(RecoverFailedReceiptScheduled.class); | ||
|
||
private final boolean isEnabled = Boolean.parseBoolean(System.getenv().getOrDefault("FAILED_AUTORECOVER_ENABLED", "true")); | ||
|
||
private final BizEventToReceiptService bizEventToReceiptService; | ||
private final BizEventCosmosClient bizEventCosmosClient; | ||
private final ReceiptCosmosService receiptCosmosService; | ||
|
||
public RecoverFailedReceiptScheduled() { | ||
this.bizEventToReceiptService = new BizEventToReceiptServiceImpl(); | ||
this.receiptCosmosService = new ReceiptCosmosServiceImpl(); | ||
this.bizEventCosmosClient = BizEventCosmosClientImpl.getInstance(); | ||
} | ||
|
||
RecoverFailedReceiptScheduled(BizEventToReceiptService bizEventToReceiptService, | ||
BizEventCosmosClient bizEventCosmosClient, | ||
ReceiptCosmosService receiptCosmosService) { | ||
this.bizEventToReceiptService = bizEventToReceiptService; | ||
this.bizEventCosmosClient = bizEventCosmosClient; | ||
this.receiptCosmosService = receiptCosmosService; | ||
} | ||
|
||
/** | ||
* This function will be invoked periodically according to the specified schedule. | ||
* <p> | ||
* It recovers all the receipts with the following status: | ||
* - ({@link ReceiptStatusType#INSERTED}) | ||
* - ({@link ReceiptStatusType#FAILED}) | ||
* - ({@link ReceiptStatusType#NOT_QUEUE_SENT}) | ||
* <p> | ||
* It creates the receipts if not exist and send on queue the event in order to proceed with the receipt generation. | ||
*/ | ||
@FunctionName("RecoverFailedReceiptScheduled") | ||
public void run( | ||
@TimerTrigger(name = "timerInfo", schedule = "%RECOVER_FAILED_CRON%") String timerInfo, | ||
@CosmosDBOutput( | ||
name = "ReceiptDatastore", | ||
databaseName = "db", | ||
collectionName = "receipts", | ||
connectionStringSetting = "COSMOS_RECEIPTS_CONN_STRING") | ||
OutputBinding<List<Receipt>> documentdb, | ||
final ExecutionContext context | ||
) { | ||
if (isEnabled) { | ||
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now()); | ||
List<Receipt> receiptList = new ArrayList<>(); | ||
|
||
receiptList.addAll(recover(context, ReceiptStatusType.INSERTED)); | ||
receiptList.addAll(recover(context, ReceiptStatusType.FAILED)); | ||
receiptList.addAll(recover(context, ReceiptStatusType.NOT_QUEUE_SENT)); | ||
|
||
documentdb.setValue(receiptList); | ||
} | ||
} | ||
|
||
private List<Receipt> recover(ExecutionContext context, ReceiptStatusType statusType) { | ||
try { | ||
MassiveRecoverResult recoverResult = massiveRecoverByStatus( | ||
context, bizEventToReceiptService, bizEventCosmosClient, receiptCosmosService, logger, statusType); | ||
if (recoverResult.getErrorCounter() > 0) { | ||
logger.error("[{}] Error recovering {} failed receipts for status {}", | ||
context.getFunctionName(), recoverResult.getErrorCounter(), statusType); | ||
} | ||
return recoverResult.getReceiptList(); | ||
} catch (NoSuchElementException e) { | ||
logger.error("[{}] Unexpected error during recover of failed receipt for status {}", | ||
context.getFunctionName(), statusType, e); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/model/MassiveRecoverResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package it.gov.pagopa.receipt.pdf.helpdesk.model; | ||
|
||
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class MassiveRecoverResult { | ||
|
||
private List<Receipt> receiptList; | ||
private int errorCounter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.