Azure函数触发器在启动时只运行一次
创始人
2024-09-29 19:00:34
0

可以使用Timer Trigger与Singleton Pattern结合来解决这个问题。首先,在timer trigger的配置中设置一个间隔时间,确保函数会定期运行。接着,在自定义StartUp拓展类的Configure方法中,添加一个单例标记,每次运行函数前首先检查该标记是否存在。若存在,则跳过运行;若不存在,则运行函数,并设置单例标记。以下是C#代码示例:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddSingleton();

        builder.Services.AddHttpClient();
    }
}

public class Function1
{
    private readonly SingletonService _singletonService;
    private readonly HttpClient _httpClient;

    public Function1(SingletonService singletonService, HttpClient httpClient)
    {
        _singletonService = singletonService;
        _httpClient = httpClient;
    }

    [FunctionName("Function1")]
    public async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        if (_singletonService.InstanceCount == 1)
        {
            log.LogInformation($"Function has already run once on startup.");
            return new OkResult();
        }

        // Function logic
        // ...

        _singletonService.SetInstanceCount(1);
        return new OkResult();
    }
}

public class SingletonService
{
    private int _instanceCount;

    public int InstanceCount => _instanceCount;

    public void SetInstanceCount(int count)
    {
        _instanceCount = count;
    }
}

相关内容

热门资讯

透视挂透视!德普之星透视辅助软... 透视挂透视!德普之星透视辅助软件激活码(透视)都是是有挂(详细辅助揭秘教程);1、德普之星透视辅助软...
来一盘!hardrock透视工... 来一盘!hardrock透视工具,竞技联盟辅助,技巧教程(有挂技巧)1、玩家可以在hardrock透...
透视模拟器!wepoker亲友... 透视模拟器!wepoker亲友圈有用吗(透视)确实真的是有挂(详细辅助黑科技教程)一、软件透明挂的定...
分享实测!hardrock作弊... 分享实测!hardrock作弊,wpk透视辅助方法,可靠技巧(有挂透明)关于wpk透视辅助方法机制的...
透视app!wepoker透视... 透视app!wepoker透视脚本(透视)原来是真的有挂(详细辅助2025新版);亲,关键说明,we...
教学盘点!约局吧怎么看有没有挂... 1、教学盘点!约局吧怎么看有没有挂,wepoker能不能透视,必赢方法(有挂软件)(UU poker...
透视智能ai!来玩app 德州... 透视智能ai!来玩app 德州 辅助(透视)一贯真的有挂(详细辅助透明挂教程);1、机器人多个强度级...
一分钟揭秘!hhpoker到底... 1、一分钟揭秘!hhpoker到底可以作弊吗,impoker辅助,规律教程(有挂技巧)。2、hhpo...
透视规律!aapoker真的假... 透视规律!aapoker真的假的(透视)竟然真的是有挂(详细辅助我来教教你)暗藏猫腻,小编详细说明原...
玩家必看教程!xpoker透视... 玩家必看教程!xpoker透视辅助,wepoker软件安装包,曝光教程(有挂技巧);最新版2024是...