c# - 在 Azure Webjobs SDK v3 中的 ServiceBusTrigger 函数中处理 Webjob 关闭
问题描述
我正在尝试在我的函数中优雅地处理 webjob 关闭。我正在使用 Azure Webjobs SDK 的 v3 和服务总线扩展。
这是我编写的一个测试函数,基于这里的一些示例代码:https ://github.com/Azure/azure-webjobs-sdk-samples/blob/master/BasicSamples/MiscOperations/Functions.cs
public async Task ProcessQueueMessageAsync([ServiceBusTrigger("testqueue")] Message message, CancellationToken cancellationToken, ILogger logWriter)
{
logWriter.LogError("GOT MESSAGE");
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(2000);
logWriter.LogError("Not Cancelled");
}
logWriter.LogError("CANCELLED!!!");
}
但是,当我关闭网络作业时,不会记录取消。
我也尝试过捕获 TaskCanceledException,如下例所示:https ://github.com/mathewc/samples/blob/master/WebJobSamples/ContinuousJobGracefulShutdown/Functions.cs
那对我也不起作用。任何想法如何在我的功能中实现这一点?
更新(18 年 12 月 18 日):
虽然我还没有弄清楚这一点,但我有一个适合我目的的解决方法。在我的 Program 类中,我声明了一个public static CancellationToken shutdownToken
变量,并在我的 Main 方法中将其设置为
shutdownToken = new WebJobsShutdownWatcher().Token;
然后我在我的函数中注册一个回调,如下所示:
Program.shutdownToken.Register(() => logWriter.LogWarning("Webjob is shutting down!"));
解决方案
我参考您的链接代码并编写了一个WebJob
with QueueTrigger
,然后上传并在运行一段时间后停止它。我的输出日志显示它工作正常。也许你可以参考一下。
public static string ShutDownFilePath
{
get
{
return shutDownFile;
}
}
public static void ShutdownMonitorJob(
[QueueTrigger("myqueue")] string message,
TextWriter log,
CancellationToken cancellationToken)
{
new Thread(new ThreadStart(() =>
{
log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
Thread.Sleep(10000);
// Modify the shutdown file
File.WriteAllText(shutDownFile, string.Empty);
})).Start();
log.WriteLine("From function: Received a message: " + message);
while (!cancellationToken.IsCancellationRequested)
{
log.WriteLine("From function: Cancelled: No");
Thread.Sleep(2000);
}
// Perform the graceful shutdown logic here
log.WriteLine("From function: Cancelled: Yes");
}
这是我的输出日志。取消状态更改为是。
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO]
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO]
[12/18/2018 02:14:09 > dd4ec8: SYS INFO] Detected WebJob file/s were updated,
refreshing
WebJob
[12/18/2018 02:14:10 > dd4ec8: SYS INFO] Status changed to Stopping
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO]
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob'
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Host.Results[0]
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob'
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] Job host stopped
我用 ServiceBus Trigger 做了更多的测试。
这是我的 Program.cs 内容。
static void Main(string[] args)
{
JobHostConfiguration config = new JobHostConfiguration();
config.UseServiceBus();
JobHost host = new JobHost(config);
host.RunAndBlock();
}
这是我的功能内容。
private static string shutDownFile = Path.GetTempFileName();
public static string ShutDownFilePath
{
get
{
return shutDownFile;
}
}
public static void ShutdownMonitorJob(
[ServiceBusTrigger("myqueue")]
string myQueueItem,
TextWriter log,
CancellationToken cancellationToken)
{
new Thread(new ThreadStart(() =>
{
log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
Thread.Sleep(10000);
// Modify the shutdown file
File.WriteAllText(shutDownFile, string.Empty);
})).Start();
log.WriteLine("From function: Received a message: " + myQueueItem);
while (!cancellationToken.IsCancellationRequested)
{
log.WriteLine("From function: Cancelled: No");
Thread.Sleep(2000);
}
// Perform the graceful shutdown logic here
log.WriteLine("From function: Cancelled: Yes");
}
}
我停止了webjob
并且状态正常更改。
推荐阅读
- php - 使用 PHP 在图像标签中返回 SQL 数据
- windows - 如果在类库中安装 nuget,我是否还必须将其添加到客户端应用程序中?
- java - 是否可以使用 IText java 读取由 itextsharp 生成的 pdf 表单?
- c++ - VSOMEIP - Communication between 2 devices (TCP/UDP) Not working
- c# - What EventType should I set for each of the 3 Handles.CircleHandleCap?
- r - R - Multiple Columns on one single Scatterplot
- bash - Bash input "read" stays open after Ctrl+C
- html - How to make this specific shadow for a box : before?
- c++ - Error handling. Mapping system error codes to generic
- python - Creating a list of random numbers without duplicates in python