当连续天青WebJob的通知是停止为NoAutomaticTrigger类型的工作 [英] Notification of when continuous Azure WebJob is stopping for NoAutomaticTrigger type jobs

查看:473
本文介绍了当连续天青WebJob的通知是停止为NoAutomaticTrigger类型的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我将现有工作者角色code到Azure的Web作业。我试图用WebJob SDK(1.0),使我有在Azure网站上的全面整合。

I am migrating existing Worker Role code to an Azure Web job. I am trying to use the WebJob SDK (1.0) so that I have the full integration with the Azure Web Site.

我的困难是,JobHost不与外界有它通常基于属性的invoke选择就业发挥很好(队列,斑点等)

My difficultly is that the JobHost doesn't play nicely with jobs that are outside it's usual Attribute based invoke options (Queues, Blobs etc.)

我已经有标准code,我不能改变听Azure的队列/主题等等,所以我不能用WebJob code做到这一点。

I already have standard code that I cannot change to listen to Azure Queues/Topics etc. so I cannot use the WebJob code to do this.

因此​​,我需要使用WebJob调用方法:

Therefore I need to use the WebJob Call method:

var cancelTokenSource = new CancellationTokenSource();
var onStartMethod = typeof(Program).GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public);

host.CallAsync(onStartMethod, cancelTokenSource.Token)
    .ConfigureAwait(false)
    .GetAwaiter()
    .GetResult();

注:我用我自己的CallAsync因为所有的建议是使用ConfigureAwait(假)使用库,但JobHost的内部结构并没有这样做的时候,所以我复制它的呼叫code自己但与ConfigureAwait。取消令牌不JobHost做任何事情,只要我可以告诉。

NB: I am using my own CallAsync as all the advice is to use ConfigureAwait(false) when using libraries but the innards of JobHost doesn't do this, so I'm replicating it's "call" code myself but with the ConfigureAwait. The cancellation token doesn't do anything in JobHost as far as I can tell.

我的问题是,然后我需要调用host.RunAndBlock();停止退出工作,这是好的,但后来我需要运行一些清理处理。我做不出来CallAsync一个新的呼叫与调用OnStop方法,因为主机已被取消,因此,所有我能做的就是让我的调用OnStop方法直接调用。不幸的是后来我失去了通过提供的TextWriter类写入到网站目录的能力。

My problem is that I then need to call host.RunAndBlock(); to stop the job exiting, which is fine, but then I need to run some clean up processing. I can't make a new call to "CallAsync" with an OnStop method, as the Host is already being cancelled, so all I can do is make a direct call to my OnStop method. Unfortunately then I lose the ability to write to the WebSite log through the provided TextWriter class.

我想我需要的是JobHost内RunAndBlock调用我的方法的一种方式,所以后来我可以拿起当主机关机打响了取消标记,然后进行清理我的code ....但似乎也没有什么办法做到这一点。

I think what I need is a way for JobHost to invoke my method within RunAndBlock, so I can then pick up the cancellation token fired when the host is shutting down, and then perform my cleanup code.... but there doesn't seem any way to do this.

有没有我缺少一个明显的方法是什么? JobHost看来真是可怜的处理方案,这一点的规范之外:(

Is there an obvious way I am missing? JobHost seems really poor at handling scenarios outside it's norm :(

推荐答案

维克多说,你可以使用<一个href=\"https://github.com/Azure/azure-webjobs-sdk/blob/master/src/Microsoft.Azure.WebJobs.Host/WebjobsShutdownWatcher.cs\"相对=nofollow> Microsoft.Azure.WebJobs.WebJobsShutdownWatcher

这是阿密特的解决方案的实现: WebJobs正常关闭

As victor said, you could use Microsoft.Azure.WebJobs.WebJobsShutdownWatcher
This is an implementation of Amit solution : WebJobs Graceful Shutdown

所以,我已经找到了解决办法这样做:

在Program.cs中
任何修改

So I've found a solution doing this :
No modification in the Program.cs

class Program
{
    static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Startup).GetMethod("Start"));
        host.RunAndBlock();
    }
}

正常关闭云在Startup.cs:

the graceful shutdown goes in the Startup.cs :

public class Startup
{
    [NoAutomaticTrigger]
    public static void Start(TextWriter log)
    {
        var token = new Microsoft.Azure.WebJobs.WebJobsShutdownWatcher().Token;
        //Shut down gracefully
        while (!token.IsCancellationRequested)
        {
            // Do somethings
        }
    }
}

在while循环,你也可以停止启动的任务。

After the while loop, you could also stop started tasks.

这篇关于当连续天青WebJob的通知是停止为NoAutomaticTrigger类型的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆