/// <summary> /// thanks to https://github.com/kobake/AspNetCore.RouteAnalyzer/issues/28 /// for the idea /// </summary> public static void CreateCronJobs( this IEndpointRouteBuilder endpoints, ISetCronJobClient cronJobClient, string baseUrl) { var cronJobEndPoints = endpoints .DataSources .SelectMany(dataSource => dataSource .Endpoints.OfType <RouteEndpoint>() .Select(ep => new { Endpoint = ep, CronJobs = ep.Metadata.OfType <SetCronJobAttribute>() }).Where(jobs => jobs.CronJobs.Any())); foreach (var ep in cronJobEndPoints) { // just spitballing here cronJobClient.CreateJobAsync(new CronJob() { Url = baseUrl + ep.Endpoint.RoutePattern.RawText, Name = ep.Endpoint.DisplayName }).Wait(); } }
private static async Task UpdateCronJobAsync( Account account, Notification notification, ISetCronJobClient cronJobClient, string baseUrl, string callbackFunctionCode, ILogger logger) { var cronJob = new CronJob() { Id = notification.CronJobId ?? 0, Expression = notification.Schedule, Name = $"{account.Name}.{notification.Name}", Status = (notification.IsActive) ? JobStatus.Active : JobStatus.Disabled, TimeZone = account.TimeZone, Method = "GET", Url = baseUrl + $"api/CronJobExecute?code={callbackFunctionCode}&accountId={account.Id}&name={notification.Name}" }; string errorContext = null; try { if (notification.IsCronJobEnabled && !notification.CronJobId.HasValue) { // create new errorContext = "creating"; var result = await cronJobClient.CreateJobAsync(cronJob); notification.CronJobId = result.Id; } else if (notification.CronJobId.HasValue) { // update or disable errorContext = "updating"; await cronJobClient.UpdateJobAsync(cronJob); } } catch (Exception exc) { string message = exc.Message + $" while {errorContext}"; notification.CronJobMessage = message; logger?.LogError(exc, message); } }