Skip to content

Commit

Permalink
Adding ServiceBus output binding support + sample
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Dec 18, 2015
1 parent d5985ad commit e0a783e
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 23 deletions.
7 changes: 7 additions & 0 deletions WebJobs.Script.sln
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{446135
.nuget\packages.config = .nuget\packages.config
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ServiceBusQueueTrigger", "ServiceBusQueueTrigger", "{5C89A41E-D179-4D2F-B66E-0F77BF1DE53E}"
ProjectSection(SolutionItems) = preProject
sample\ServiceBusQueueTrigger\function.json = sample\ServiceBusQueueTrigger\function.json
sample\ServiceBusQueueTrigger\index.js = sample\ServiceBusQueueTrigger\index.js
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -142,5 +148,6 @@ Global
{644AFFC7-46C5-483A-812E-F5C9B93CBA86} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{1B6E0EA1-DB38-431C-864A-2A553DACB8DA} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{5C458CC0-F3EC-463E-9091-2B08955C4375} = {1B6E0EA1-DB38-431C-864A-2A553DACB8DA}
{5C89A41E-D179-4D2F-B66E-0F77BF1DE53E} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion sample/QueueTrigger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function (context) {

context.output({
receipt: JSON.stringify(context.workItem)
})
});

context.done();
}
18 changes: 18 additions & 0 deletions sample/ServiceBusQueueTrigger/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"bindings": {
"input": [
{
"type": "serviceBusTrigger",
"name": "message",
"queueName": "samples-input"
}
],
"output": [
{
"type": "serviceBus",
"name": "message",
"queueName": "samples-input"
}
]
}
}
16 changes: 16 additions & 0 deletions sample/ServiceBusQueueTrigger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function (context) {
var message = context.message;
context.log("Node.js ServiceBus queue trigger function processed message '" + JSON.stringify(message) + "'");

if (message.count < 1)
{
// write a message back to the queue that this function is triggered on
// ensuring that we only loop on this once
message.count += 1;
context.output({
message: JSON.stringify(message)
});
}

context.done();
}
2 changes: 1 addition & 1 deletion sample/TimerTrigger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function (context) {
};
context.output({
message: JSON.stringify(message)
})
});

logger.log(timeStamp, context.done);
}
6 changes: 3 additions & 3 deletions src/WebJobs.Script.Host/WebJobs.Script.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
Expand All @@ -83,11 +83,11 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Host, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.ServiceBus, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Edm, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
6 changes: 3 additions & 3 deletions src/WebJobs.Script.Host/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.WebHooks" version="1.0.0-beta3-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net45" />
Expand Down
9 changes: 9 additions & 0 deletions src/WebJobs.Script/Binding/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ internal static Collection<Binding> GetBindings(JobHostConfiguration config, JAr
string queueName = (string)binding["queueName"];
bindings.Add(new QueueBinding(config, name, queueName, fileAccess, isTrigger: false));
}
else if (type == "serviceBus")
{
if (fileAccess != FileAccess.Write)
{
throw new InvalidOperationException("ServiceBus binding can only be used for output.");
}
string queueOrTopicName = (string)(binding["queueName"] ?? binding["topicName"]);
bindings.Add(new ServiceBusBinding(config, name, queueOrTopicName, fileAccess, isTrigger: false));
}
else if (type == "queueTrigger")
{
string queueName = (string)binding["queueName"];
Expand Down
52 changes: 52 additions & 0 deletions src/WebJobs.Script/Binding/ServiceBusBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Bindings.Path;

namespace Microsoft.Azure.WebJobs.Script
{
internal class ServiceBusBinding : Binding
{
private readonly BindingTemplate _queueOrTopicNameBindingTemplate;

public ServiceBusBinding(JobHostConfiguration config, string name, string queueOrTopicName, FileAccess fileAccess, bool isTrigger) : base(config, name, "serviceBus", fileAccess, isTrigger)
{
QueueOrTopicName = queueOrTopicName;
_queueOrTopicNameBindingTemplate = BindingTemplate.FromString(QueueOrTopicName);
}

public string QueueOrTopicName { get; private set; }

public override bool HasBindingParameters
{
get
{
return _queueOrTopicNameBindingTemplate.ParameterNames.Any();
}
}

public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDictionary<string, string> bindingData)
{
string boundQueueName = QueueOrTopicName;
if (bindingData != null)
{
boundQueueName = _queueOrTopicNameBindingTemplate.Bind(bindingData);
}

boundQueueName = Resolve(boundQueueName);

// only an output binding is supported
using (StreamReader reader = new StreamReader(stream))
{
// TODO: only string supported currently - need to support other types
IAsyncCollector<string> collector = binder.Bind<IAsyncCollector<string>>(new ServiceBusAttribute(boundQueueName));
string data = reader.ReadToEnd();
await collector.AddAsync(data);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected ParameterDescriptor ParseServiceBusTrigger(JObject trigger, Type trigg
}
else
{
throw new InvalidOperationException("Invalid servicebus trigger configuration.");
throw new InvalidOperationException("Invalid ServiceBus trigger configuration.");
}

string parameterName = (string)trigger["name"];
Expand Down
9 changes: 8 additions & 1 deletion src/WebJobs.Script/Description/NodeFunctionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task Invoke(object[] parameters)
private Dictionary<string, object> CreateContext(object input, TraceWriter traceWriter, IBinder binder)
{
Type triggerParameterType = input.GetType();
if (triggerParameterType == typeof(string))
if (triggerParameterType == typeof(string) && IsJson((string)input))
{
// convert string into Dictionary (recursively) which Edge will convert into an object
// before invoking the function
Expand All @@ -155,5 +155,12 @@ private Dictionary<string, object> CreateContext(object input, TraceWriter trace

return context;
}

public static bool IsJson(string input)
{
input = input.Trim();
return (input.StartsWith("{") && input.EndsWith("}"))
|| (input.StartsWith("[") && input.EndsWith("]"));
}
}
}
7 changes: 4 additions & 3 deletions src/WebJobs.Script/WebJobs.Script.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
Expand All @@ -80,11 +80,11 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Host, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.ServiceBus, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Storage, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down Expand Up @@ -166,6 +166,7 @@
<Compile Include="Binding\BlobBinding.cs" />
<Compile Include="Binding\Binding.cs" />
<Compile Include="Binding\QueueBinding.cs" />
<Compile Include="Binding\ServiceBusBinding.cs" />
<Compile Include="Binding\TableBinding.cs" />
<Compile Include="Config\NameResolver.cs" />
<Compile Include="Config\ScriptHost.cs" />
Expand Down
6 changes: 3 additions & 3 deletions src/WebJobs.Script/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.WebHooks" version="1.0.0-beta3-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function (context) {

context.output({
output: json
})
});

context.done();
}
6 changes: 3 additions & 3 deletions test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
Expand All @@ -76,11 +76,11 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Host, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.Host.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.ServiceBus, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10195\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.ServiceBus.1.1.1-alpha-10200\lib\net45\Microsoft.Azure.WebJobs.ServiceBus.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Edm, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
6 changes: 3 additions & 3 deletions test/WebJobs.Script.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta4" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.1-alpha-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.Extensions.WebHooks" version="1.0.0-beta3-10210" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10195" targetFramework="net45" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.1-alpha-10200" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net45" />
Expand Down

0 comments on commit e0a783e

Please sign in to comment.