如何在运行时从 appsettings.json 读取 CRON 表达式?

2024-02-24

我开发了具有多个计时器触发功能的网络作业项目。每个功能在一天中的不同时间执行。有些每分钟执行一次,有些每五分钟执行一次,有些一天执行一次。

如果我在函数参数属性本身中编写 CRON 表达式,例如
ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo),它按预期工作,但是当我尝试从 appsettings.json 读取它时,它失败了。

任何人都可以建议阅读 Functions.cs 中的 appsettings.json 的正确方法吗?

以下是我正在尝试的代码。

程序.cs

internal class Program
   {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        private static void Main()
        {
            ServiceCollection services = new ServiceCollection();
            ConfigureServices(services);

            var config = new JobHostConfiguration();
            config.JobActivator = new JobActivator(services.BuildServiceProvider());
            config.UseTimers();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }

        private static IConfiguration Configuration { get; set; }

        private static void ConfigureServices(IServiceCollection services)
        {
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            services.AddSingleton(Configuration);
            services.AddTransient<Functions, Functions>();
            services.AddLogging(builder => builder.AddConsole());
        }
    }

函数.cs

public class Functions
    {
        private readonly ILogger<Functions> logger;

        private readonly IConfiguration configuration;

        public Functions(ILogger<Functions> logger, IConfiguration configuration)
        {
            this.configuration = configuration;
            this.logger = logger;
        }


        [FunctionName("TimerTriggerEveryMinute")]
        public void ProcessTrigger1([TimerTrigger("%TimerTriggerEveryMinute:Schedule%")]TimerInfo timerInfo)
        {
            var ab = this.configuration;
            Console.WriteLine(string.Format("{0} Proc 1",DateTime.Now));
        }

        [FunctionName("TimerTriggerEveryThirdMinute")]
        public void ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo)
        {
            Console.WriteLine(string.Format("{0} Proc 2",DateTime.Now));
        }

        [FunctionName("TimerTriggerEveryFiveMinute")]
        public void ProcessTrigger3([TimerTrigger("%EveryFiveMinuteSchedule%",RunOnStartup =false)]TimerInfo timer)
        {
            Console.WriteLine(string.Format("{0} Proc 5", DateTime.Now));
        }
}

JobActivator.cs

 public class JobActivator : IJobActivator
    {
        private readonly IServiceProvider services;

        public JobActivator(IServiceProvider services)
        {
            this.services = services;
        }

        public T CreateInstance<T>()
        {
            return services.GetService<T>();
        }
    }

应用程序设置.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "My Azure Dashboard key here...",
    "AzureWebJobsStorage": "My Azure Job Storage key here...",
  },
  "TimerTriggerEveryMinute": {
    "Schedule": "0 * * * * *"
  },
  "TimerTriggerEveryFiveMinute": {
    "Schedule": "0 */5 * * * *"
  }
}

在上面的代码中,在 Functions.cs 中,如果我将计时器触发器写入
ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo)对于所有方法来说,这种方式都可以按预期工作,但如果我以其他两种方式编写,它就会给我例外。 请建议从 appsettings.json 读取时间表的编写方式。


是的。不要对 CRON 表达式进行硬编码,而是将表达式放入应用程序设置中并使用%迹象。例如,如果您有一个名为CRON_EXPRESSION:

公共静态无效运行([TimerTrigger(“%CRON_EXPRESSION%”)] TimerInfo myTimer,TraceWriter日志)

Refer - https://github.com/Azure/azure-functions-host/issues/1934 https://github.com/Azure/azure-functions-host/issues/1934

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在运行时从 appsettings.json 读取 CRON 表达式? 的相关文章

随机推荐