11

I have Azure function with timer trigger.

public static void Run([TimerTrigger("0 */15 * * * *"), Disable("True")]TimerInfo myTimer, TraceWriter log)

Here the Disable("true") is not working. it generates the function.json as "disabled": "True", which is not correct. It should be "disabled": True, Disable only accepts string value. Is there any way to change this? or any other way to disable function?

Avinash patil
  • 1,689
  • 4
  • 17
  • 39

6 Answers6

9

Disable properties default values is true.

Use Disable() instead of Disable("true").

So the code will look like

public static void Run([TimerTrigger("0 */15 * * * *"), Disable()]TimerInfo myTimer, TraceWriter log) .

If you want to enable the function use Disable("False").

Avinash patil
  • 1,689
  • 4
  • 17
  • 39
  • 2
    Raised this issue to Azure functions team. Same issue with portal also. More information : https://github.com/Azure/azure-functions-ux/issues/1712 – Avinash patil Aug 31 '17 at 07:13
  • 1
    guys, the parameter in the Disable() attribute is not intended to be a boolean, but it refers to an appsetting that can contain a boolean value. (see my answer for more information & code snippet) – Sam Vanhoutte Feb 14 '20 at 12:40
8

Functions 2.x can be disabled individually via local.settings.json in the following manner

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobs.MyFunctionNameOne.Disabled": "true",
    "AzureWebJobs.MyFunctionNameTwo.Disabled": "true",
    ...
    }
}

Ref: https://learn.microsoft.com/en-us/azure/azure-functions/disable-function#functions-2x---all-languages

golfalot
  • 956
  • 12
  • 22
6

I wanted to add this answer, as I have been searching for it as well and I believe I found a good way to disable functions for debugging/development purposes (and to avoid these local changes get into the deployment pipelines/source control).

I combine the #if DEBUG statements with the Disable(string SettingsName) attribute:

The following piece of code shows different things at work:

  • The Disable attribute is not using the parameter to indicate True or False as a value, but it refers to an appsetting (that you can put in the local.settings.json file). See the second snippet in this post. So, just by changing that settings in the appsettings file, I can enable and disable triggers easily, without impacting my git repo or deployment pipelines.
  • The other interesting thing is the RunOnStartup = true parameter when using a TimerTrigger. This one (which I only enable with the DEBUG compiler directives), will allow me to trigger the timer function immediately, without waiting for the next CRON cycle to happen. (an alternative would be to do a post to your local functions endpoint, as described in this stackoverflow post). So again, when assuming you run your production workloads in the RELEASE configuration, this is only impacting your local development environment and not your development team or releases.

1st snippet (attributes)

#if DEBUG
        [Disable("disable-transactioncrawler")]
#endif
        [FunctionName("TransactionCrawler")]
        public async Task Run([TimerTrigger("50 */10 * * * *"
#if DEBUG
                , RunOnStartup = true
#endif
            )]TimerInfo myTimer, ILogger log)
        {
                // Logic comes here
        }

2nd snippet (local.appsettings.json)

{
  "Values": 
  {
    "disable-transactioncrawler": false
  }
}

The following 'solutions' typically impact your production code, which can lead to issues:

  • Just using Disable() is not allowing you to configure/change it afterwards
  • Updating the host.json file to only include the triggers you want to run is also risking to have this change arriving in production.
Sam Vanhoutte
  • 3,247
  • 27
  • 48
5

Have you tried modifying the host.json inside your solution? It has the following properties for you to specify which functions to load on runtime.

// Array of functions to load. Only functions in this list will be enabled.
// If not specified, all functions are enabled.
"functions": ["QueueProcessor", "GitHubWebHook"]

Note that if you have multiple Function App projects in your solution, you will also need to make to change to their corresponding host.json (i.e. each project has their own host.json)

Documentation: https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json

Harris K
  • 111
  • 1
  • 10
2

The string typed value - "disabled": "true" also could disable the function. Please see the test result as following.

Here is my function definition.

public static void Run([TimerTrigger("0 */5 * * * *"),Disable("true")]TimerInfo myTimer, TraceWriter log)

Here is the published function on Azure portal.

enter image description here

Amor
  • 8,325
  • 2
  • 19
  • 21
  • This is working on portal. but not working on local debug environment. (Visual Studio 2017) – Avinash patil Aug 18 '17 at 06:16
  • Yes, you are right. You can quit the debug if you don't want to run the function. Why do you want to disable the function on your debug environment? – Amor Aug 18 '17 at 06:23
  • I also test "disabled": true on my local environment. It also can't work. It think our local environment will skip this property because we can't stop the function at anytime we want on our local environment. – Amor Aug 18 '17 at 06:27
  • 1
    i have 40 functions. i want to test only one without running other functions. – Avinash patil Aug 18 '17 at 06:29
  • 3
    For your scenarios, you could comment out the FunctionName attribute which you don't want to test. For example, //[FunctionName("Function1")] – Amor Aug 18 '17 at 06:38
  • I suggest you post the issue and your requirement on Azure feedback site. https://feedback.azure.com – Amor Aug 18 '17 at 06:57
-1

I tried disabling using local.settings.json, and when debugging locally the emulator window actually says that the named function is disabled, but calls it anyway! This is the same in VS2017 and 2019.

The workaround I'm currently using is to test for this app setting as the first line in my function and return immediately:

    if(ConfigurationManager.AppSettings["AzureWebJobs.TimerTriggeredFunction.Disabled"] == "true") return;
Tony Gray
  • 19
  • 4