我已经声明了一个ConcurrentQueue
并添加了一个 GUID 列表。添加到队列中很好,但是当我从TimerTrigger
函数内部访问队列时,它似乎是空的(updateQueue.count
为 0)。这种行为发生在云中,但是当我在本地执行相同的操作时,它可以正常工作。
public static class Function1
{
private static readonly ConcurrentQueue<IEnumerable<Guid>> updateQueue = new ConcurrentQueue<IEnumerable<Guid>>();
public static async Task<IActionResult> UpdateRun(
[HttpTrigger(AuthorizationLevel.Admin, "post", Route = null)] HttpRequest req, ExecutionContext execContext)
{
logger.LogInformation($"FunctionUpdate Start");
using var reader = new StreamReader(req.Body);
var request = JsonConvert.DeserializeObject<IEnumerable<Request>>(reader.ReadToEnd());
var correlationIds = request?.Select(s => s.CorrelationId);
updateQueue.Enqueue(correlationIds);
return new OkObjectResult(new Response { HttpStatusCode = HttpStatusCode.Accepted });
}
[FunctionName("FunctionHandleQueue"), Timeout("00:05:00")]
public static async Task HandleQueue([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ExecutionContext execContext) // once every 1 minutes
{
logger.LogInformation($"before updateQueue condition : {updateQueue.Count}");
if (updateQueue.Count > 0)
{
logger.LogInformation($"after updateQueue condition {updateQueue.Count}");
var guids = new List<Guid>();
var count = 0;
while (count <= 1000 && updateQueue.Count > 0)
{
updateQueue.TryDequeue(out var updateRequest);
var enumerable = updateRequest.ToList();
count += enumerable.Count;
guids.AddRange(enumerable);
}
await new ProcessUpdateSales(CreateMapper(), execContext)
.Orchestrate(guids)
}
}
}
TimerTrigger
每 1 分钟执行一次时会创建日志:
updateQueue 条件之前:0
为什么updateQueue.Count
总是0?我究竟做错了什么?