简体   繁体   English

在Azure WebJob中动态启用/禁用触发的功能

[英]Dynamically enable/ disable a triggered function in Azure WebJob

We have an azure web job that has two methods in the Functions.cs file. 我们有一个azure web作业,在functions.cs文件中有两个方法。 Both jobs are triggered off different Topics in Azure Service Bus. 这两个作业都是从Azure Service Bus中的不同主题触发的。

As this uses reflection at run time to determine the functions that are to be run/triggered by messages hitting the topics, there are no references to these methods in code. 由于它在运行时使用反射来确定由命中主题的消息运行/触发的函数,因此代码中没有对这些方法的引用。

public static async Task DoWork([ServiceBusTrigger("topic-one", "%environmentVar%")] BrokeredMessage brokeredMessage, TextWriter log) {}

public static async Task DoOtherWork([ServiceBusTrigger("topic-two", "%environmentVar2%")] BrokeredMessage brokeredMessage, TextWriter log) {}

I have a need to have this web job run either both methods, or just one of them, based on a variable set at run time (it won't change one the job is running, but it is read in when the job starts). 基于运行时设置的变量,我需要让这个Web作业同时运行这两种方法,或者只运行其中一种方法(它不会更改作业正在运行的一个,但是在作业启动时读入它) 。 I can't simply wrap the internals of the methods in an if() based on the variable, as that would read and destroy the message. 我不能简单地将方法的内部包装在基于变量的if() ,因为它会读取并销毁消息。

Is it possible to use the JobHostConfiguration (an IServiceProvider ) to achieve this, as that is built at run time. 是否可以使用JobHostConfiguration (一个IServiceProvider )来实现这一点,因为它是在运行时构建的。 Is that was the JobHostConfiguration.IJobActivator can be used for? 这是JobHostConfiguration.IJobActivator可以用于吗?

Triggered Functions can be disabled when the Webjob starts. Webjob启动时可以禁用触发功能。

You can have a look at this issue: Dynamic Enable/ Disable a function . 您可以查看此问题: 动态启用/禁用功能

So the Webjob SDK provided a DisableAttribute`: 所以Webjob SDK提供了一个DisableAttribute`:

  • Can be applied at the Parameter/Method/Class level 可以在参数/方法/类级别应用
  • Only affects triggered functions 仅影响触发的功能
  • [Disable("setting")] - If a config/environment value exists for the specified setting name, and its value is "1" or "True" (case insensitive), the function will be disabled. [Disable("setting")] - 如果指定的设置名称存在配置/环境值,并且其值为“1”或“True”(不区分大小写),则该功能将被禁用。
  • [Disable(typeof(DisableProvider))] - custom Type declaring a function of signature bool IsDisabled(MethodInfo method). [Disable(typeof(DisableProvider))] - 自定义类型,声明签名bool IsDisabled(MethodInfo方法)的功能。 We'll call this method to determine if the function should be disabled. 我们将调用此方法来确定是否应禁用该函数。
  • This is a startup time only check. 这是仅启动时间检查。 For disabled triggered functions, we simply skip starting of the function listener. 对于禁用的触发函数,我们只需跳过函数监听器的启动。 However, when you update app settings bound to these attributes, your WebJob will automaticallly restart and your settings will take effect. 但是,当您更新绑定到这些属性的应用程序设置时,WebJob将自动重新启动,您的设置将生效。
  • Setting names can include binding parameters (eg {MethodName}, {MethodShortName}, %test%, etc.) 设置名称可以包括绑定参数(例如{MethodName},{MethodShortName},%test%等)

In your case you need to use the DisableAttribute with a DisableProvider. 在您的情况下,您需要使用DisableAttribute与DisableProvider。

public class DoWorkDisableProvider
{
    public bool IsDisabled(MethodInfo method)
    {
        // check if the function should be disable
        // return true or false

        return true;
    }
}

public class DoOtherWorkkDisableProvider
{
    public bool IsDisabled(MethodInfo method)
    {
        // check if the function should be disable
        // return true or false

        return true;
    }
}

And your functions should be decorated with the disable attribute 您的函数应该使用disable属性进行修饰

[Disable(typeof(DoWorkDisableProvider))]
public static async Task DoWork([ServiceBusTrigger("topic-one", "%environmentVar%")] BrokeredMessage brokeredMessage, TextWriter log) {}

[Disable(typeof(DoOtherWorkkDisableProvider))]
public static async Task DoOtherWork([ServiceBusTrigger("topic-two", "%environmentVar2%")] BrokeredMessage brokeredMessage, TextWriter log) {}

Otherwise the JobHostConfiguration.IJobActivator is designed to inject dependencies into your functions. 否则, JobHostConfiguration.IJobActivator旨在将依赖项注入到您的函数中。 you can have a look at these posts related to: 你可以看一下这些与以下相关的帖子:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM