找到 Azure WebJob,但未执行计时器函数
Azure WebJob found, but timer functions are not getting executed
我已经完成了 30 多次测试 运行,其中我能够成功完成一项工作(它只是同时完成了 Console.WriteLine("")
和 (TextWriter)log.WriteLine("")
),但是自从尝试添加我的真实代码后,我无法让它再次工作。
我删除了我添加的所有自定义代码,并将其恢复为仅执行日志记录语句,但 Azure 从未完成初始作业 运行。
这是完整的输出,无论我让它放置多长时间:
[08/16/2018 00:43:38 > 0708fc: SYS INFO] Status changed to Initializing
[08/16/2018 00:43:40 > 0708fc: SYS INFO] Run script 'BatchJobEngineWebJob.exe' with script host - 'WindowsScriptHost'
[08/16/2018 00:43:40 > 0708fc: SYS INFO] Status changed to Running
[08/16/2018 00:43:43 > 0708fc: INFO] Found the following functions:
[08/16/2018 00:43:43 > 0708fc: INFO] BatchJobEngineWebJob.Functions.DivisionI
[08/16/2018 00:43:45 > 0708fc: INFO] Job host started
阅读日志,您可以看到它正在识别我的定时函数(称为*.Functions.DivisionI),仅此而已。它说作业主机已启动,仅此而已。
这是我配置所有内容的 program.cs 代码:
static void Main()
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var config = new JobHostConfiguration();
//config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(5);
//config.Queues.VisibilityTimeout = TimeSpan.FromMinutes(1);
//config.Queues.BatchSize = 1;
config.JobActivator = new BatchJobActivator(serviceCollection.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 void ConfigureServices(IServiceCollection serviceCollection)
{
// Optional: Setup your configuration:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
// add any interfaces that will be needed here
serviceCollection.AddTransient<IBatchJobService, BatchJobService>();
// ...stripped the rest of the interfaces
// executes the job
serviceCollection.AddTransient<ExecuteBatchJobs, ExecuteBatchJobs>();
// One more thing - tell azure where your azure connection strings are
var connStringDashboard = configuration["ConnectionStrings:AzureWebJobsDashboard"];
var connStringStorage = configuration["ConnectionStrings:AzureWebJobsStorage"];
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", connStringDashboard);
Environment.SetEnvironmentVariable("AzureWebJobsStorage", connStringStorage);
}
这是我的 Functions.cs class,其中包含需要执行的函数:
public class Functions
{
private readonly IBatchJobService _batchJobService;
//public Functions(IBatchJobService batchJobService)
//{
// _batchJobService = batchJobService;
//}
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
//public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
//{
// log.WriteLine(message);
//}
//[FunctionName("HourlyBatchJobs_DivisionI")]
public async Task DivisionI([TimerTrigger("1 * * * * *", RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
{
await log.WriteLineAsync("bout time!");
Console.WriteLine("starting");
//log.WriteLine("executing HourleyBatchJobs_DivisionI");
//await _batchJobService.Execute();
}
}
我的代码有什么问题可能会阻止它执行?
需要注意的一件事:我已经在我的 WebJobs 列表中添加和删除了 40 多个作业,试图让它发挥作用。我注意到 Azure WebJobs 有一个非常严重的错误。如果我添加 运行 并删除一个名为“Test5”的作业。然后稍后回来添加另一个同名的作业,点击“运行”按钮会失败,说作业已经在执行。我最终不得不将作业添加为 Test5b 以允许它达到 运行。我想知道这是否有可能搞砸某些事情,不允许所有这些连续测试失败。
实际上,用于 .Net Core webjobs 的 VS 2017 工具尚不存在,尽管 planed.It 仍然可以编写基于 Core 的 WebJob 并将其手动部署到您的 Web 应用程序。但请注意,WebJob SDK(这是一个完全不同的主题)尚不支持 .NET Core。你可以参考这个 issue.
另外,您可以使用.net core 2.0 console application来实现。您可以参考以下步骤。
1.InstallMicrosoft.Azure.WebJobs.Extensions包。
2.Add Program.cs
文件中的以下代码。
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
config.UseTimers();
config.DashboardConnectionString ="storage connectionstring";
config.StorageConnectionString = "storage connectionstring";
var host = new JobHost(config);
host.RunAndBlock();
3.Add 将 Functions.cs
文件添加到项目中。
public class Functions
{
public static void CronJob([TimerTrigger("0 */5 * * * *")] TimerInfo timer)
{
Console.WriteLine("timer job fired!");
}
}
4.The 在 KUDU 上输出。
我已经完成了 30 多次测试 运行,其中我能够成功完成一项工作(它只是同时完成了 Console.WriteLine("")
和 (TextWriter)log.WriteLine("")
),但是自从尝试添加我的真实代码后,我无法让它再次工作。
我删除了我添加的所有自定义代码,并将其恢复为仅执行日志记录语句,但 Azure 从未完成初始作业 运行。
这是完整的输出,无论我让它放置多长时间:
[08/16/2018 00:43:38 > 0708fc: SYS INFO] Status changed to Initializing
[08/16/2018 00:43:40 > 0708fc: SYS INFO] Run script 'BatchJobEngineWebJob.exe' with script host - 'WindowsScriptHost'
[08/16/2018 00:43:40 > 0708fc: SYS INFO] Status changed to Running
[08/16/2018 00:43:43 > 0708fc: INFO] Found the following functions:
[08/16/2018 00:43:43 > 0708fc: INFO] BatchJobEngineWebJob.Functions.DivisionI
[08/16/2018 00:43:45 > 0708fc: INFO] Job host started
阅读日志,您可以看到它正在识别我的定时函数(称为*.Functions.DivisionI),仅此而已。它说作业主机已启动,仅此而已。
这是我配置所有内容的 program.cs 代码:
static void Main()
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var config = new JobHostConfiguration();
//config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(5);
//config.Queues.VisibilityTimeout = TimeSpan.FromMinutes(1);
//config.Queues.BatchSize = 1;
config.JobActivator = new BatchJobActivator(serviceCollection.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 void ConfigureServices(IServiceCollection serviceCollection)
{
// Optional: Setup your configuration:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
// add any interfaces that will be needed here
serviceCollection.AddTransient<IBatchJobService, BatchJobService>();
// ...stripped the rest of the interfaces
// executes the job
serviceCollection.AddTransient<ExecuteBatchJobs, ExecuteBatchJobs>();
// One more thing - tell azure where your azure connection strings are
var connStringDashboard = configuration["ConnectionStrings:AzureWebJobsDashboard"];
var connStringStorage = configuration["ConnectionStrings:AzureWebJobsStorage"];
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", connStringDashboard);
Environment.SetEnvironmentVariable("AzureWebJobsStorage", connStringStorage);
}
这是我的 Functions.cs class,其中包含需要执行的函数:
public class Functions
{
private readonly IBatchJobService _batchJobService;
//public Functions(IBatchJobService batchJobService)
//{
// _batchJobService = batchJobService;
//}
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
//public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
//{
// log.WriteLine(message);
//}
//[FunctionName("HourlyBatchJobs_DivisionI")]
public async Task DivisionI([TimerTrigger("1 * * * * *", RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
{
await log.WriteLineAsync("bout time!");
Console.WriteLine("starting");
//log.WriteLine("executing HourleyBatchJobs_DivisionI");
//await _batchJobService.Execute();
}
}
我的代码有什么问题可能会阻止它执行?
需要注意的一件事:我已经在我的 WebJobs 列表中添加和删除了 40 多个作业,试图让它发挥作用。我注意到 Azure WebJobs 有一个非常严重的错误。如果我添加 运行 并删除一个名为“Test5”的作业。然后稍后回来添加另一个同名的作业,点击“运行”按钮会失败,说作业已经在执行。我最终不得不将作业添加为 Test5b 以允许它达到 运行。我想知道这是否有可能搞砸某些事情,不允许所有这些连续测试失败。
实际上,用于 .Net Core webjobs 的 VS 2017 工具尚不存在,尽管 planed.It 仍然可以编写基于 Core 的 WebJob 并将其手动部署到您的 Web 应用程序。但请注意,WebJob SDK(这是一个完全不同的主题)尚不支持 .NET Core。你可以参考这个 issue.
另外,您可以使用.net core 2.0 console application来实现。您可以参考以下步骤。
1.InstallMicrosoft.Azure.WebJobs.Extensions包。
2.Add Program.cs
文件中的以下代码。
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
config.UseTimers();
config.DashboardConnectionString ="storage connectionstring";
config.StorageConnectionString = "storage connectionstring";
var host = new JobHost(config);
host.RunAndBlock();
3.Add 将 Functions.cs
文件添加到项目中。
public class Functions
{
public static void CronJob([TimerTrigger("0 */5 * * * *")] TimerInfo timer)
{
Console.WriteLine("timer job fired!");
}
}
4.The 在 KUDU 上输出。