动态启用/禁用Azure WebJob中的触发功能 [英] Dynamically enable/ disable a triggered function in Azure WebJob

查看:64
本文介绍了动态启用/禁用Azure WebJob中的触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Azure Web作业,在Functions.cs文件中有两种方法.这两个作业都是通过Azure Service Bus中的不同主题触发的.

We have an azure web job that has two methods in the Functions.cs file. Both jobs are triggered off different Topics in 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) {}

我需要根据运行时设置的变量让该Web作业要么运行这两种方法,要么仅运行其中一种(它不会更改正在运行的一种方法,但是在运行时会读取该方法).工作开始).我不能简单地基于变量将方法的内部包装在if()中,因为那样会读取并销毁消息.

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). 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.

是否可以使用JobHostConfiguration(IServiceProvider)来实现此目标,因为它是在运行时构建的.是JobHostConfiguration.IJobActivator可以使用的吗?

Is it possible to use the JobHostConfiguration (an IServiceProvider) to achieve this, as that is built at run time. Is that was the JobHostConfiguration.IJobActivator can be used for?

推荐答案

Webjob启动时可以禁用触发的功能.

Triggered Functions can be disabled when the Webjob starts.

您可以查看此问题:动态启用/禁用功能.

因此,Webjob SDK提供了DisableAttribute`:

So the Webjob SDK provided a DisableAttribute`:

  • 可以在参数/方法/类级别应用
  • 仅影响触发的功能
  • [Disable("setting")]-如果指定的设置名称存在配置/环境值,并且其值为"1"或"True"(不区分大小写),则该功能将被禁用.
  • [Disable(typeof(DisableProvider))]-声明签名bool IsDisabled(MethodInfo方法)功能的自定义类型.我们将调用此方法来确定是否应禁用该功能.
  • 这是仅在启动时检查.对于禁用的触发功能,我们仅跳过功能侦听器的启动.但是,当您更新绑定到这些属性的应用程序设置时,您的WebJob将自动重新启动,并且您的设置将生效.
  • 设置名称可以包含绑定参数(例如{MethodName},{MethodShortName},%test%等)
  • 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(typeof(DisableProvider))] - custom Type declaring a function of signature bool IsDisabled(MethodInfo method). 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.
  • Setting names can include binding parameters (e.g. {MethodName}, {MethodShortName}, %test%, etc.)

在您的情况下,您需要将DisableAttribute与DisableProvider一起使用.

In your case you need to use the DisableAttribute with a 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;
    }
}

您的函数应该用disable属性修饰

And your functions should be decorated with the disable attribute

[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) {}

否则,JobHostConfiguration.IJobActivator旨在将依赖项注入到您的函数中.您可以查看与以下内容相关的这些帖子:

Otherwise the JobHostConfiguration.IJobActivator is designed to inject dependencies into your functions. you can have a look at these posts related to:

  • Dependency injection using Azure WebJobs SDK?
  • Azure Triggered Webjobs Scope for Dependency Injection

这篇关于动态启用/禁用Azure WebJob中的触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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