我正在尝试在我的函数中正常处理Webjob关闭。我正在使用Azure Webjobs SDK和Service Bus Extensions的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!!!");
}
但是,当我关闭webjob时,取消记录不会被记录。
我还尝试捕获TaskCanceledException,如以下示例所示:https://github.com/mathewc/samples/blob/master/WebJobSamples/ContinuousJobGracefulShutdown/Functions.cs
那对我也不起作用。有什么想法可以在我的函数中实现吗?
更新(12/18/18):
虽然我仍然没有弄清楚,但我有一个适合我目的的解决方法。在我的Program类中,我声明了一个public static CancellationToken shutdownToken
变量,并将其在Main方法中设置为
shutdownToken = new WebJobsShutdownWatcher().Token;
然后我在函数中注册回调,如下所示:
Program.shutdownToken.Register(() => logWriter.LogWarning("Webjob is shutting down!"));
答案 0 :(得分:0)
我参考了您的链接代码,并用WebJob
编写了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触发器进行了更多测试。
这是我的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
,状态正常更改。