What is the simplest way to run a timer-triggered Azure Function locally once?

后端 未结 7 856
醉话见心
醉话见心 2020-12-13 12:03

I have a few C# Azure Functions that run on a schedule using timer triggers. I\'ve set them up like so, where %TimerSchedule% refers to a cron expression in the

相关标签:
7条回答
  • 2020-12-13 12:21

    Using postman should do the trick. Follow the below steps to Run or debug you Timer Trigger Locally.

    1 . RUN your Project.

    1. Open Postman and past this url http://localhost:{port}/admin/functions/{function_name}

    2. Make sure to use a POST Method with Json body of { "input": "" }

    3. Press SEND.

    You Should receive a response of 202.

    0 讨论(0)
  • 2020-12-13 12:21

    Just add another function with HTTP trigger type within the same class, add your code, or call your Run method from that function and invoke it from your browser.

    Be sure to comment/remove that function when deployed to prod, or you will have the ability to trigger the function via HTTP calls in prod.

    0 讨论(0)
  • 2020-12-13 12:28

    I had the same question, and used the DEBUG-flag to have the RunOnStartup only while debugging:

            public static void Run(
                [TimerTrigger("* 0 7 * * 1-5"
    #if DEBUG
                , RunOnStartup=true
    #endif
                )]TimerInfo myTimer, TraceWriter log)
            {
    
    0 讨论(0)
  • 2020-12-13 12:28

    I had the same question. I fixed it with a Unittest. Indeed you need to stub out the TraceWriter and the TimerInfo.

    Here some code how I did this.

    TimerInfo:

    public class ScheduleStub : TimerInfo
    {
        public ScheduleStub(TimerSchedule schedule, ScheduleStatus status, bool isPastDue = false) : base(schedule, status, isPastDue)
        {
        }
    }
    

    And the TraceWriter:

     public class TraceWriterStub : TraceWriter
    {
        protected TraceLevel _level;
        protected List<TraceEvent> _traces;
    
        public TraceWriterStub(TraceLevel level) : base(level)
        {
            _level = level;
            _traces = new List<TraceEvent>();
        }
    
        public override void Trace(TraceEvent traceEvent)
        {
            _traces.Add(traceEvent);
        }
    
        public List<TraceEvent> Traces => _traces;
    }
    
    0 讨论(0)
  • 2020-12-13 12:33

    From https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#non-http-triggered-functions

    Non-HTTP triggered functions

    For all kinds of functions other than HTTP triggers and webhooks, you can test your functions locally by calling an administration endpoint. Calling this endpoint with an HTTP POST request on the local server triggers the function. You can optionally pass test data to the execution in the body of the POST request. This functionality is similar to the Test tab in the Azure portal.

    You call the following administrator endpoint to trigger non-HTTP functions:

    http://localhost:{port}/admin/functions/{function_name}

    To pass test data to the administrator endpoint of a function, you must supply the data in the body of a POST request message. The message body is required to have the following JSON format:

    {
        "input": "<trigger_input>"
    }
    
    0 讨论(0)
  • 2020-12-13 12:35

    You could perhaps use the RunOnStartup flag as documented here. It doesn't quite meet your brief regarding it only running once, but it should at least execute it locally once the app has started.

    /// Gets or sets a value indicating whether the function should be invoked
    /// immediately on startup. After the initial startup run, the function will
    /// be run on schedule thereafter.
    

    Example using attribute binding:

    [TimerTrigger("%TimerSchedule%", RunOnStartup = true)]TimerInfo myTimer

    0 讨论(0)
提交回复
热议问题