Skip to content

Commit

Permalink
Fix Redis sample, adding Queue example.
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
fbeltrao committed Dec 30, 2018
1 parent 8e8ea26 commit ba049f0
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"azureFunctions.projectLanguage": "C#",
"azureFunctions.projectRuntime": "~2"
}
4 changes: 4 additions & 0 deletions samples/EventGridSample/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"azureFunctions.projectLanguage": "C#Script",
"azureFunctions.projectRuntime": "~2"
}
2 changes: 1 addition & 1 deletion samples/EventGridSample/EventGridSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.23" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\AzureFunctionExtensions\AzureFunctionExtensions.csproj" />
Expand Down
5 changes: 3 additions & 2 deletions samples/EventGridSample/local.settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"AzureWebJobs_ExtensionsPath": ".",
"eventgrid_sas": "EFgFqSaABprKP0Ww7jgf8TCY9L6lE3cVrB4+nVeloxc=",
"eventgrid_endpoint": "https://frbtest.westeurope-1.eventgrid.azure.net/api/events"
"eventgrid_sas": "",
"eventgrid_endpoint": "https://xxxxx.westeurope-1.eventgrid.azure.net/api/events"
}
}
6 changes: 6 additions & 0 deletions samples/RedisSample/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"ms-azuretools.vscode-azurefunctions",
"ms-vscode.csharp"
]
}
11 changes: 11 additions & 0 deletions samples/RedisSample/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to C# Functions",
"type": "coreclr",
"request": "attach",
"processId": "${command:azureFunctions.pickProcess}"
}
]
}
8 changes: 8 additions & 0 deletions samples/RedisSample/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"azureFunctions.projectRuntime": "~2",
"azureFunctions.projectLanguage": "C#",
"azureFunctions.templateFilter": "Verified",
"azureFunctions.deploySubpath": "bin/Release/netstandard2.0/publish",
"azureFunctions.preDeployTask": "publish",
"debug.internalConsoleOptions": "neverOpen"
}
46 changes: 46 additions & 0 deletions samples/RedisSample/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"command": "dotnet clean",
"type": "shell",
"problemMatcher": "$msCompile"
},
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"dependsOn": "clean",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile"
},
{
"label": "clean release",
"command": "dotnet clean --configuration Release",
"type": "shell",
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet publish --configuration Release",
"type": "shell",
"dependsOn": "clean release",
"problemMatcher": "$msCompile"
},
{
"label": "runFunctionsHost",
"type": "shell",
"dependsOn": "build",
"options": {
"cwd": "${workspaceFolder}/bin/Debug/netstandard2.0"
},
"command": "func host start",
"isBackground": true,
"problemMatcher": "$func-watch"
}
]
}
37 changes: 37 additions & 0 deletions samples/RedisSample/QueueTriggerWithRedisFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Fbeltrao.AzureFunctionExtensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;

namespace Fbeltrao.RedisSample
{
public static class QueueTriggerWithRedisFunctions
{
[FunctionName(nameof(MoveQueueItemToRedis))]
public static async Task MoveQueueItemToRedis(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
[RedisDatabase(Connection = "%redis_connectionstring%")] IDatabase db,
ILogger log)
{
await db.ListRightPushAsync("my-queue", myQueueItem);
log.LogInformation($"Queue trigger pushed to redis list: {myQueueItem}");
}


[FunctionName(nameof(GetRedisList))]
public static async Task<IActionResult> GetRedisList(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
[RedisDatabase(Connection = "%redis_connectionstring%")] IDatabase db,
ILogger log)
{
var values = await db.ListRangeAsync("my-queue");
return new OkObjectResult(new { values });
}
}
}
3 changes: 2 additions & 1 deletion samples/RedisSample/RedisSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.23" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\AzureFunctionExtensions\AzureFunctionExtensions.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion samples/RedisSample/local.settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"AzureWebJobs_ExtensionsPath": ".",
"redis_connectionstring": "10.211.55.2:6379",
"redis_connectionstring": "192.168.1.124:6379",
"redis_setvalueinredis_key": "mySample"
}
}
2 changes: 1 addition & 1 deletion samples/SignalRSample/SignalRSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.23" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\AzureFunctionExtensions\AzureFunctionExtensions.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/AzureFunctionExtensions/AzureFunctionExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.1" />
<PackageReference Include="StackExchange.Redis.StrongName.Core" Version="1.1.605" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
</ItemGroup>
Expand Down
12 changes: 2 additions & 10 deletions src/AzureFunctionExtensions/Redis/RedisConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ namespace Fbeltrao.AzureFunctionExtensions
public class RedisConfiguration : IExtensionConfigProvider
{
private readonly IRedisDatabaseManager redisDatabaseManager;

/// <summary>
/// Constructor
/// </summary>
public RedisConfiguration() : this(RedisDatabaseManager.GetInstance())
{

}


/// <summary>
/// Constructor
/// </summary>
Expand Down Expand Up @@ -69,7 +61,7 @@ public void Initialize(ExtensionConfigContext context)
// Redis output binding
context
.AddBindingRule<RedisOutputAttribute>()
.BindToCollector<RedisOutput>(attr => new RedisOutputAsyncCollector(this, attr));
.BindToCollector<RedisOutput>(attr => new RedisOutputAsyncCollector(this, attr, this.redisDatabaseManager));

// Redis database (input) binding
context
Expand Down
6 changes: 1 addition & 5 deletions src/AzureFunctionExtensions/Redis/RedisDatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ public class RedisDatabaseManager : IRedisDatabaseManager
/// <summary>
/// Constructor
/// </summary>
protected RedisDatabaseManager()
public RedisDatabaseManager()
{
connections = new System.Collections.Concurrent.ConcurrentDictionary<string, Lazy<ConnectionMultiplexer>>();
}

static RedisDatabaseManager instance = new RedisDatabaseManager();
public static IRedisDatabaseManager GetInstance() => instance;


/// <summary>
/// Gets the redis database
/// </summary>
Expand Down
10 changes: 1 addition & 9 deletions src/AzureFunctionExtensions/Redis/RedisOutputAsyncCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ public class RedisOutputAsyncCollector : IAsyncCollector<RedisOutput>
/// </summary>
/// <param name="config"></param>
/// <param name="attr"></param>
public RedisOutputAsyncCollector(RedisConfiguration config, RedisOutputAttribute attr) : this(config, attr, RedisDatabaseManager.GetInstance())
{
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="config"></param>
/// <param name="attr"></param>
/// <param name="redisDatabaseManager"></param>
public RedisOutputAsyncCollector(RedisConfiguration config, RedisOutputAttribute attr, IRedisDatabaseManager redisDatabaseManager)
{
this.config = config;
Expand Down
2 changes: 2 additions & 0 deletions src/AzureFunctionExtensions/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -19,6 +20,7 @@ public void Configure(IWebJobsBuilder builder)
builder.AddExtension<HttpCallConfiguration>();
builder.AddExtension<RedisConfiguration>();
builder.AddExtension<SignalRConfiguration>();
builder.Services.AddSingleton(typeof(IRedisDatabaseManager), typeof(RedisDatabaseManager));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.10.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
23 changes: 13 additions & 10 deletions test/AzureFunctionExtensions.Test/RedisOutputAsyncCollectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class RedisOutputAsyncCollectorTest
[Fact]
public async Task SetKeyValue_ConnectionInConfig_KeyInAttribute_OperationInAttribute_SetsValue()
{
var config = new RedisConfiguration()
var connectionManager = new Mock<IRedisDatabaseManager>();
var config = new RedisConfiguration(connectionManager.Object)
{
Connection = "localhost:3679",
};
Expand All @@ -28,7 +29,7 @@ public async Task SetKeyValue_ConnectionInConfig_KeyInAttribute_OperationInAttri

var redisDatabase = new RedisDatabaseMock();

var connectionManager = new Mock<IRedisDatabaseManager>();

connectionManager.Setup(x => x.GetDatabase("localhost:3679", -1))
.Returns(redisDatabase);

Expand All @@ -49,7 +50,8 @@ await target.AddAsync(new RedisOutput()
[Fact]
public async Task IncrementValue_ConnectionInConfig_KeyInAttribute_OperationInAttribute_WithoutIncrementValue_IncrementsOne()
{
var config = new RedisConfiguration()
var connectionManager = new Mock<IRedisDatabaseManager>();
var config = new RedisConfiguration(connectionManager.Object)
{
Connection = "localhost:3679",
};
Expand All @@ -62,7 +64,7 @@ public async Task IncrementValue_ConnectionInConfig_KeyInAttribute_OperationInAt

var redisDatabase = new RedisDatabaseMock();

var connectionManager = new Mock<IRedisDatabaseManager>();

connectionManager.Setup(x => x.GetDatabase("localhost:3679", -1))
.Returns(redisDatabase);

Expand All @@ -82,7 +84,8 @@ await target.AddAsync(new RedisOutput()
[Fact]
public async Task IncrementValue_ConnectionInConfig_KeyInAttribute_OperationInAttribute_By10_Increments10()
{
var config = new RedisConfiguration()
var connectionManager = new Mock<IRedisDatabaseManager>();
var config = new RedisConfiguration(connectionManager.Object)
{
Connection = "localhost:3679",
};
Expand All @@ -95,7 +98,7 @@ public async Task IncrementValue_ConnectionInConfig_KeyInAttribute_OperationInAt

var redisDatabase = new RedisDatabaseMock();

var connectionManager = new Mock<IRedisDatabaseManager>();

connectionManager.Setup(x => x.GetDatabase("localhost:3679", -1))
.Returns(redisDatabase);

Expand All @@ -117,7 +120,8 @@ await target.AddAsync(new RedisOutput()
[Fact]
public async Task ListRightPush_ConnectionInConfig_KeyInAttribute_OperationInAttribute_AddsItemToEndOfList()
{
var config = new RedisConfiguration()
var connectionManager = new Mock<IRedisDatabaseManager>();
var config = new RedisConfiguration(connectionManager.Object)
{
Connection = "localhost:3679",
};
Expand All @@ -130,7 +134,6 @@ public async Task ListRightPush_ConnectionInConfig_KeyInAttribute_OperationInAtt

var redisDatabase = new RedisDatabaseMock();

var connectionManager = new Mock<IRedisDatabaseManager>();
connectionManager.Setup(x => x.GetDatabase("localhost:3679", -1))
.Returns(redisDatabase);

Expand Down Expand Up @@ -159,7 +162,8 @@ await target.AddAsync(new RedisOutput()
[Fact]
public async Task ListLeftPush_ConnectionInConfig_KeyInAttribute_OperationInAttribute_AddsItemToStartOfList()
{
var config = new RedisConfiguration()
var connectionManager = new Mock<IRedisDatabaseManager>();
var config = new RedisConfiguration(connectionManager.Object)
{
Connection = "localhost:3679",
};
Expand All @@ -172,7 +176,6 @@ public async Task ListLeftPush_ConnectionInConfig_KeyInAttribute_OperationInAttr

var redisDatabase = new RedisDatabaseMock();

var connectionManager = new Mock<IRedisDatabaseManager>();
connectionManager.Setup(x => x.GetDatabase("localhost:3679", -1))
.Returns(redisDatabase);

Expand Down

0 comments on commit ba049f0

Please sign in to comment.