Skip to content

Commit 3e7c0bb

Browse files
committed
Updating Timer sample to use Queue output binding
1 parent 494918a commit 3e7c0bb

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

sample/TimerTrigger/function.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"input": [
44
{
55
"type": "timerTrigger",
6-
"schedule": "*/15 * * * * *",
6+
"schedule": "0 * * * * *",
77
"runOnStartup": true
88
}
9+
],
10+
"output": [
11+
{
12+
"type": "queue",
13+
"name": "message",
14+
"queueName": "samples-fsharp"
15+
}
916
]
1017
}
1118
}

sample/TimerTrigger/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ module.exports = function (context) {
66
var timeStamp = new Date().toISOString();
77
context.log('Node.js timer trigger function ran at ' + timeStamp);
88

9+
var message = {
10+
id: Math.floor(Math.random() * 10000) + 1,
11+
notes: "Generated by Timer function"
12+
};
13+
context.output({
14+
message: JSON.stringify(message)
15+
})
16+
917
logger.log(timeStamp, context.done);
1018
}

src/WebJobs.Script/Binding/Binding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
67
using System.IO;
@@ -58,6 +59,10 @@ internal static Collection<Binding> GetBindings(JobHostConfiguration config, JAr
5859
}
5960
else if (type == "queue")
6061
{
62+
if (fileAccess != FileAccess.Write)
63+
{
64+
throw new InvalidOperationException("Queue binding can only be used for output.");
65+
}
6166
string queueName = (string)binding["queueName"];
6267
bindings.Add(new QueueBinding(config, name, queueName, fileAccess, isTrigger: false));
6368
}

src/WebJobs.Script/Binding/QueueBinding.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,14 @@ public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDic
3939

4040
boundQueueName = Resolve(boundQueueName);
4141

42-
if (FileAccess == FileAccess.Write)
42+
IAsyncCollector<byte[]> collector = binder.Bind<IAsyncCollector<byte[]>>(new QueueAttribute(boundQueueName));
43+
byte[] bytes;
44+
using (MemoryStream ms = new MemoryStream())
4345
{
44-
Stream queueStream = binder.Bind<Stream>(new QueueAttribute(boundQueueName));
45-
await queueStream.CopyToAsync(stream);
46-
}
47-
else
48-
{
49-
IAsyncCollector<byte[]> collector = binder.Bind<IAsyncCollector<byte[]>>(new QueueAttribute(boundQueueName));
50-
byte[] bytes;
51-
using (MemoryStream ms = new MemoryStream())
52-
{
53-
stream.CopyTo(ms);
54-
bytes = ms.ToArray();
55-
}
56-
await collector.AddAsync(bytes);
46+
stream.CopyTo(ms);
47+
bytes = ms.ToArray();
5748
}
49+
await collector.AddAsync(bytes);
5850
}
5951
}
6052
}

test/WebJobs.Script.Tests/NodeFunctionGenerationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ private static MethodInfo GenerateMethod(JObject trigger)
156156
List<FunctionFolderInfo> functionFolderInfos = new List<FunctionFolderInfo>();
157157
functionFolderInfos.Add(functionFolderInfo);
158158

159+
JobHostConfiguration config = new JobHostConfiguration();
159160
FunctionDescriptorProvider[] descriptorProviders = new FunctionDescriptorProvider[]
160161
{
161-
new NodeFunctionDescriptorProvider(Environment.CurrentDirectory)
162+
new NodeFunctionDescriptorProvider(config, Environment.CurrentDirectory)
162163
};
163164
var functionDescriptors = ScriptHost.ReadFunctions(functionFolderInfos, descriptorProviders);
164165
Type t = FunctionGenerator.Generate("TestScriptHost", "Host.Functions", functionDescriptors);

0 commit comments

Comments
 (0)