All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.ws.commons.computation.service.NotificationService Maven / Gradle / Ivy

/**
 * Copyright (c) 2022, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.ws.commons.computation.service;

import com.powsybl.ws.commons.computation.utils.annotations.PostCompletion;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

import static com.powsybl.ws.commons.computation.utils.MessageUtils.shortenMessage;

/**
 * @author Etienne Homer  message) {
        RUN_MESSAGE_LOGGER.debug(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "Run-out-0", message);
    }

    public void sendCancelMessage(Message message) {
        CANCEL_MESSAGE_LOGGER.debug(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "Cancel-out-0", message);
    }

    @PostCompletion
    public void sendResultMessage(UUID resultUuid, String receiver, String userId, @Nullable Map additionalHeaders) {
        MessageBuilder builder = MessageBuilder
                .withPayload("")
                .setHeader(HEADER_RESULT_UUID, resultUuid.toString())
                .setHeader(HEADER_RECEIVER, receiver)
                .setHeader(HEADER_USER_ID, userId)
                .copyHeaders(additionalHeaders);
        Message message = builder.build();
        RESULT_MESSAGE_LOGGER.debug(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "Result-out-0", message);
    }

    @PostCompletion
    public void publishStop(UUID resultUuid, String receiver, String computationLabel) {
        Message message = MessageBuilder
                .withPayload("")
                .setHeader(HEADER_RESULT_UUID, resultUuid.toString())
                .setHeader(HEADER_RECEIVER, receiver)
                .setHeader(HEADER_MESSAGE, getCancelMessage(computationLabel))
                .build();
        STOP_MESSAGE_LOGGER.debug(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "Stopped-out-0", message);
    }

    @PostCompletion
    public void publishFail(UUID resultUuid, String receiver, String causeMessage, String userId, String computationLabel, Map additionalHeaders) {
        MessageBuilder builder = MessageBuilder
                .withPayload("")
                .setHeader(HEADER_RESULT_UUID, resultUuid.toString())
                .setHeader(HEADER_RECEIVER, receiver)
                .setHeader(HEADER_MESSAGE, shortenMessage(
                        getFailedMessage(computationLabel) + " : " + causeMessage))
                .setHeader(HEADER_USER_ID, userId)
                .copyHeaders(additionalHeaders);
        Message message = builder.build();
        FAILED_MESSAGE_LOGGER.debug(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "Failed-out-0", message);
    }

    @PostCompletion
    public void publishCancelFailed(UUID resultUuid, String receiver, String computationLabel, String userId) {
        Message message = MessageBuilder
                .withPayload("")
                .setHeader(HEADER_RESULT_UUID, resultUuid.toString())
                .setHeader(HEADER_RECEIVER, receiver)
                .setHeader(HEADER_USER_ID, userId)
                .setHeader(HEADER_MESSAGE, getCancelFailedMessage(computationLabel))
                .build();
        CANCEL_FAILED_MESSAGE_LOGGER.info(SENDING_MESSAGE, message);
        publisher.send(publishPrefix + "CancelFailed-out-0", message);
    }

    public static String getCancelMessage(String computationLabel) {
        return computationLabel + " was canceled";
    }

    public static String getFailedMessage(String computationLabel) {
        return computationLabel + " has failed";
    }

    public static String getCancelFailedMessage(String computationLabel) {
        return computationLabel + " could not be cancelled";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy