为不同用户自定义 azure webjob schedule 计时
Customize azure webjob schedule timing for different users
目前我有 azure webjob,它由 azure scheduler 每 2 小时触发一次 statically.But 我需要根据我的每个用户配置动态安排时间 website.since 功能对所有用户都是相同的除了计划执行时间,我不能去寻找新的 webjobs.I 使用参数 cron 值查看 azure 队列,但我不知道如何实现上述情况。
请说明如何为不同的用户自定义 azure webjob schedule 时间。
Azure 调度程序将无法根据用户触发。
可能您必须需要一个连续的 运行ning 网络作业,这需要 运行 根据用户特定的时间表。您必须需要将用户特定的时间表存储在 table/configuration/file 中。仅当满足调度条件时才读取那些调度和 运行 作业。或者您可以使用服务总线来触发网络作业。您的服务总线必须根据用户计划触发。
只要 1 分钟的粒度合适(并且您的站点设置为始终在线),我就会使用 WebJobs SDK Extension TimerTrigger 而不是计划的 webjob。
那么您可以进行以下操作:
- 在某个地方有一个 table 或数据存储来跟踪代码最后一次为用户 运行 的时间,并在需要再次 运行 时设置
- 将检查#1 中的数据存储的 TimerTrigger 设置为每分钟 运行 并查看是否有任何需要 运行,如果需要 - 然后将消息放入队列并 return
- 有另一个函数可以侦听该队列,计时器触发器刚刚使用 QueueTrigger 抛出一条消息,然后执行您当前正在执行的任何处理。
这应该允许您的用户拥有自己的日程安排并让代码由这些日程安排触发。
更新:
下面是一个使用定时器和队列触发器的 producer/consumer 模型的简单示例:
using Microsoft.Azure.WebJobs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProducerConsumer
{
class Program
{
static void Main(string[] args)
{
var config = new JobHostConfiguration();
config.UseDevelopmentSettings();
config.UseTimers();
var host = new JobHost(config);
host.RunAndBlock();
}
}
public static class Functions
{
public static int counter = 0;
// Create a new message on the "orders" queue every 10 seconds
public static void Producer(
[TimerTrigger("00:00:10", RunOnStartup=true)] TimerInfo timer,
[Queue("orders")] out string messageCount,
TextWriter log)
{
messageCount = "message" + counter++;
log.WriteLine("messsage: " + messageCount);
}
// Watch the "orders" queue for messages, and when one is found save it to blob storage
public static void Consumer(
[QueueTrigger("orders")] string message,
[Blob("orders/order.txt")] out string order,
TextWriter log)
{
order = message;
log.WriteLine("Received " + message);
}
}
}
目前我有 azure webjob,它由 azure scheduler 每 2 小时触发一次 statically.But 我需要根据我的每个用户配置动态安排时间 website.since 功能对所有用户都是相同的除了计划执行时间,我不能去寻找新的 webjobs.I 使用参数 cron 值查看 azure 队列,但我不知道如何实现上述情况。
请说明如何为不同的用户自定义 azure webjob schedule 时间。
Azure 调度程序将无法根据用户触发。
可能您必须需要一个连续的 运行ning 网络作业,这需要 运行 根据用户特定的时间表。您必须需要将用户特定的时间表存储在 table/configuration/file 中。仅当满足调度条件时才读取那些调度和 运行 作业。或者您可以使用服务总线来触发网络作业。您的服务总线必须根据用户计划触发。
只要 1 分钟的粒度合适(并且您的站点设置为始终在线),我就会使用 WebJobs SDK Extension TimerTrigger 而不是计划的 webjob。
那么您可以进行以下操作:
- 在某个地方有一个 table 或数据存储来跟踪代码最后一次为用户 运行 的时间,并在需要再次 运行 时设置
- 将检查#1 中的数据存储的 TimerTrigger 设置为每分钟 运行 并查看是否有任何需要 运行,如果需要 - 然后将消息放入队列并 return
- 有另一个函数可以侦听该队列,计时器触发器刚刚使用 QueueTrigger 抛出一条消息,然后执行您当前正在执行的任何处理。
这应该允许您的用户拥有自己的日程安排并让代码由这些日程安排触发。
更新: 下面是一个使用定时器和队列触发器的 producer/consumer 模型的简单示例:
using Microsoft.Azure.WebJobs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProducerConsumer
{
class Program
{
static void Main(string[] args)
{
var config = new JobHostConfiguration();
config.UseDevelopmentSettings();
config.UseTimers();
var host = new JobHost(config);
host.RunAndBlock();
}
}
public static class Functions
{
public static int counter = 0;
// Create a new message on the "orders" queue every 10 seconds
public static void Producer(
[TimerTrigger("00:00:10", RunOnStartup=true)] TimerInfo timer,
[Queue("orders")] out string messageCount,
TextWriter log)
{
messageCount = "message" + counter++;
log.WriteLine("messsage: " + messageCount);
}
// Watch the "orders" queue for messages, and when one is found save it to blob storage
public static void Consumer(
[QueueTrigger("orders")] string message,
[Blob("orders/order.txt")] out string order,
TextWriter log)
{
order = message;
log.WriteLine("Received " + message);
}
}
}