Skip to content

Commit

Permalink
Merge pull request #27 from shibayan/feature/event-webhook
Browse files Browse the repository at this point in the history
Adding Event webhook
  • Loading branch information
shibayan authored May 25, 2019
2 parents c3ac316 + 83ff540 commit 30851c0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Net.Http;
using System.Threading.Tasks;

using Microsoft.Azure.WebJobs.Extensions.DurableTask;

namespace AzureKeyVault.LetsEncrypt.Internal
{
internal class InProcLifeCycleNotificationHelper : ILifeCycleNotificationHelper
{
public Task OrchestratorStartingAsync(string hubName, string functionName, string instanceId, bool isReplay)
{
return Task.CompletedTask;
}

public Task OrchestratorCompletedAsync(string hubName, string functionName, string instanceId, bool continuedAsNew, bool isReplay)
{
return Task.CompletedTask;
}

public async Task OrchestratorFailedAsync(string hubName, string functionName, string instanceId, string reason, bool isReplay)
{
await PostEventAsync(functionName, instanceId, reason);
}

public Task OrchestratorTerminatedAsync(string hubName, string functionName, string instanceId, string reason)
{
return Task.CompletedTask;
}

private static readonly HttpClient _httpClient = new HttpClient();

private static async Task PostEventAsync(string functionName, string instanceId, string reason)
{
if (string.IsNullOrEmpty(Settings.Default.Webhook))
{
return;
}

object model;

if (Settings.Default.Webhook.Contains("hooks.slack.com"))
{
model = new
{
attachments = new[]
{
new
{
title = functionName,
text = reason,
color = "danger"
}
}
};
}
else
{
model = new
{
functionName,
instanceId,
reason
};
}

await _httpClient.PostAsJsonAsync(Settings.Default.Webhook, model);
}
}
}
2 changes: 2 additions & 0 deletions AzureKeyVault.LetsEncrypt/Internal/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public Settings()

public string VaultBaseUrl => _section[nameof(VaultBaseUrl)];

public string Webhook => _section[nameof(Webhook)];

public static Settings Default { get; } = new Settings();
}
}
3 changes: 2 additions & 1 deletion AzureKeyVault.LetsEncrypt/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"extensions": {
"durableTask": {
"extendedSessionsEnabled": true,
"extendedSessionIdleTimeoutInSeconds": 30
"extendedSessionIdleTimeoutInSeconds": 30,
"CustomLifeCycleNotificationHelperType": "AzureKeyVault.LetsEncrypt.Internal.InProcLifeCycleNotificationHelper, AzureKeyVault.LetsEncrypt"
}
}
}

0 comments on commit 30851c0

Please sign in to comment.