6

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

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.

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?

Thomas
  • 24,234
  • 6
  • 81
  • 125
Mark McGookin
  • 932
  • 1
  • 16
  • 39

1 Answers1

17

Triggered Functions can be disabled when the Webjob starts.

You can have a look at this issue: Dynamic Enable/ Disable a function.

So the Webjob SDK provided a 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(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.)

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;
    }
}

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

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

Thomas
  • 24,234
  • 6
  • 81
  • 125
  • Perfect @Thomas thanks! Looks like I was barking up the wrong tree with IJobActivator – Mark McGookin Aug 11 '17 at 07:37
  • 5
    Is this still supported? I get an error "the constructor 'DisableAttribute(Type)' is not supported" in Microsoft.NET.Sdk.Functions.Build.targets. I use Functions v2. – Thomas Schreiter Feb 01 '19 at 18:53
  • 1
    Yes it is but is not part of the `Microsoft.NET.Sdk.Functions.Build.targets` namespace... it is part of the azure webjob sdk – Thomas Feb 02 '19 at 01:27
  • 1
    Can't you comment run.cmd for the job? By adding REM before the command. like REM dotnet WebJobAssemblyName.dll. – Sujit Singh Aug 08 '19 at 07:59
  • I am getting same error the constructor 'DisableAttribute(Type)' is not supported. Error generating functions metadata. I using like [Microsoft.Azure.WebJobs.Disable(typeof(x))], but my error message showing Microsoft.NET.Sdk.Functions.Build.targets. Any idea? – Sap_vr Nov 21 '22 at 18:48
  • There is an open issue in git https://github.com/Azure/azure-functions-vs-build-sdk/issues/480 – Sap_vr Nov 21 '22 at 20:14