Skip to content

Commit d601a45

Browse files
committed
rename file and package added
1 parent 45f03a2 commit d601a45

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

src/AzureFunctionsLabs.TimerTrigger/AzureFunctionsLabs.TimerTrigger.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.27" />
8+
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
89
</ItemGroup>
910
<ItemGroup>
1011
<None Update="host.json">
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1+
#region Imports
12
using System;
3+
using System.Threading.Tasks;
24
using Microsoft.Azure.WebJobs;
35
using Microsoft.Azure.WebJobs.Host;
4-
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Logging;
7+
#endregion
58

69
namespace AzureFunctionsLabs.TimerTrigger
710
{
8-
public static class Function1
11+
public static partial class TimerFunctions
912
{
10-
[FunctionName("Function1")]
13+
[FunctionName("TimerTrigger")]
1114
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
1215
{
1316
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
1417
}
1518
}
1619
}
20+
21+
22+
23+
#region @@Reference
24+
/*
25+
26+
27+
*/
28+
#endregion
29+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#region Imports
2+
using Microsoft.Azure.WebJobs;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
#endregion
8+
9+
namespace AzureFunctionsLabs.TimerTrigger
10+
{
11+
public static partial class TimerFunctions
12+
{
13+
#region Fields
14+
15+
// {second=0} {minute=15} {hour=10} {day} {month} {day-of-week=(2=Tuesday)}
16+
private const string TimerSchedule = "0 15 10 * * 2";
17+
private static HttpClient _client = new HttpClient();
18+
19+
#endregion
20+
21+
[FunctionName("CallWebhook")]
22+
public static async Task CallWebhook([TimerTrigger(TimerSchedule)]TimerInfo myTimer, ILogger log)
23+
{
24+
log.LogInformation($"Tiggering CallWebhook at: {DateTime.Now}");
25+
26+
var webHookUrl = Environment.GetEnvironmentVariable("WebHookUrl");
27+
28+
await _client.PostAsJsonAsync(webHookUrl, "{}");
29+
30+
log.LogInformation($"CallWebhook triggered successfully at: {DateTime.Now}");
31+
}
32+
}
33+
}
34+
35+
36+
37+
#region @@Reference
38+
/*
39+
https://andrewlock.net/creating-my-first-azure-functions-v2-app-a-webhook-and-a-timer/
40+
41+
*/
42+
#endregion
43+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#region Imports
2+
using System;
3+
using System.Data.SqlClient;
4+
using System.Threading.Tasks;
5+
using Microsoft.Azure.WebJobs;
6+
using Microsoft.Azure.WebJobs.Host;
7+
using Microsoft.Extensions.Logging;
8+
#endregion
9+
10+
namespace AzureFunctionsLabs.TimerTrigger
11+
{
12+
public static partial class TimerFunctions
13+
{
14+
#region Fields
15+
16+
// {second=0} {minute=15} {hour=10} {day} {month} {day-of-week=(2=Tuesday)}
17+
private const string RemoveLogsSchedule = "0 15 10 * * 2";
18+
19+
#endregion
20+
21+
[FunctionName("RemoveLogs")]
22+
public static async Task RemoveLogs([TimerTrigger(RemoveLogsSchedule)]TimerInfo myTimer, ILogger log)
23+
{
24+
try
25+
{
26+
log.LogInformation($"Function executed at: {DateTime.Now}");
27+
28+
var connectionString = Environment.GetEnvironmentVariable("SQLDbConnection");
29+
using (var connection = new SqlConnection(connectionString))
30+
{
31+
connection.Open();
32+
var query = "DELETE FROM [dbo].[Logs] WHERE [CreatedOn] > DATEADD(DAY, -2, GETUTCDATE())";
33+
34+
using (SqlCommand cmd = new SqlCommand(query, connection))
35+
{
36+
var result = await cmd.ExecuteNonQueryAsync();
37+
log.LogInformation($"Deleted GeoLocations: {result}");
38+
}
39+
}
40+
41+
log.LogInformation($"Function execution finished at: {DateTime.Now}");
42+
}
43+
catch (Exception ex)
44+
{
45+
log.LogError("Exception", ex);
46+
throw;
47+
}
48+
}
49+
}
50+
}
51+
52+
53+
54+
#region @@Reference
55+
/*
56+
57+
58+
*/
59+
#endregion
60+

0 commit comments

Comments
 (0)