1

我有一个传奇,它每 30 秒检查一次 API 调用的状态,如果从调用返回的状态成功则传奇结束,否则传奇等待 30 秒并再次尝试。如果 API 调用在 60 分钟内没有返回成功响应,则 saga 超时并结束。

我无法让我的 60 分钟超时触发。我的代码是

public class MonitorSubmissionFeedSagaData: IContainSagaData
{
    public Guid Id { get; set; }

    public string Originator { get; set; }

    public string OriginalMessageId { get; set; }

    public bool TimeoutSet { get; set; }

    [Unique]
    public string JobId { get; set; }
}

public class MonitorSubmissionFeedSaga : Saga<MonitorSubmissionFeedSagaData>,
    IAmStartedByMessages<MonitorFeedSubmissonCommand>,
    IHandleMessages<StartCheckSubmissionCommand>,
    IHandleTimeouts<MonitorSubmissionFeedSagaTimeout>
{
    public const int SagaTimeoutInMinutes = 60;

    public IEmpathyBrokerClientApi PostFileService { get; set; }

    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MonitorSubmissionFeedSagaData> mapper)
    {
        mapper.ConfigureMapping<MonitorFeedSubmissonCommand>(x => x.JobId).ToSaga(saga => saga.JobId);

    }

    public void Handle(MonitorFeedSubmissonCommand message)
    {
        Data.JobId = message.JobId;

        CheckTimeout();

        Bus.Send(new StartCheckSubmissionCommand
        {
            JobId = Data.JobId
        });
    }

    public void Handle(StartCheckSubmissionCommand message)
    {
        Log.Info("Saga with JobId {0} received", Data.JobId);

        bool isCompleted = GetJobStatus(message.JobId);

        while (isCompleted)
        {
            Thread.Sleep(30000);
            isCompleted = GetJobStatus(message.JobId);
        }

        MarkAsComplete();
    }

    public void CheckTimeout()
    {
        RequestTimeout<MonitorSubmissionFeedSagaTimeout>(TimeSpan.FromMinutes(SagaTimeoutInMinutes));
    }

    public void Timeout(MonitorSubmissionFeedSagaTimeout state)
    {
        MarkAsComplete();
    }

    bool GetJobStatus(string jobId)
    {
        return false;
        var status = PostFileService.GetJobIdStatus(jobId);
        if (status.state == "FAILURE" || status.state == "DISCARDED")
        {
            return false;
        }
        return true;
    }

}

谁能看到我哪里出错了?

谢谢

4

1 回答 1

5

你的 Saga 应该闲置了。你用一个while循环让它保持活力。超时消息到达某个时间点,然后您应该检查应该发生什么。另一个结帐或 MarkAsComplete。

我在记事本中写了这个,所以它可能无法编译。但这是为了得到一个想法。

public class MonitorSubmissionFeedSagaData: IContainSagaData
{
public Guid Id { get; set; }
public string Originator { get; set; }
public string OriginalMessageId { get; set; }

[Unique]
public string JobId { get; set; }
public DateTime SagaStartTimeUtc { get; set; }
}

public class MonitorSubmissionFeedSaga : Saga<MonitorSubmissionFeedSagaData>,
    IAmStartedByMessages<MonitorFeedSubmissonCommand>,
    IHandleTimeouts<VerifyApiTimeOut>
{
public IEmpathyBrokerClientApi PostFileService { get; set; }

public void Handle(MonitorFeedSubmissonCommand message)
{
    Data.JobId = message.JobId;
    Data.SagaStartTimeUtc = DateTime.NowUtc;

    CreateTimeoutRequest();
}

public void CreateTimeoutRequest()
{
    RequestTimeout<VerifyApiTimeOut>(TimeSpan.FromSeconds(30));
}

public void Timeout(VerifyApiTimeOut state)
{
    if (!GetJobStatus(Data.JobId) && DateTime.NowUtc < Data.SagaStartTimeUtc.AddMinutes(60))
    {
      CreateTimeoutRequest();
    }

    MarkAsComplete();
}

bool GetJobStatus(string jobId)
{
    return false;
    var status = PostFileService.GetJobIdStatus(jobId);
    if (status.state == "FAILURE" || status.state == "DISCARDED")
    {
        return false;
    }
    return true;
}

}

另一个评论可能是 Saga 本身不应该调用外部服务。最好不要对某些数据库。将此委托给另一个服务。每 30 秒,向另一个处理程序发送一条消息。此处理程序应调用 WebService/WebAPI。当它可以确认一切都正确时,回复原始 Saga。当它不正确时,就让它发生吧。Saga 将每 30 秒发送一次消息以重试。

60 分钟后,Saga 应该停止发送消息和 MarkAsComplete。

于 2015-11-20T09:20:10.007 回答