Skip to content

Commit 19df660

Browse files
committed
Added a lot of stuff
1 parent 304917d commit 19df660

31 files changed

+602
-116
lines changed

Frank.Scheduler.Api/Frank.Scheduler.Api.csproj

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentScheduler" Version="5.5.1" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5">
10+
<!--<PackageReference Include="FluentScheduler" Version="5.5.1" />-->
11+
<PackageReference Include="CronExpressionDescriptor" Version="2.16.0" />
12+
<PackageReference Include="CronQuery" Version="2.0.0" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.6">
1315
<PrivateAssets>all</PrivateAssets>
1416
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1517
</PackageReference>
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using CronQuery.Mvc.Jobs;
2+
using Frank.Scheduler.Services;
3+
using Microsoft.Extensions.Logging;
4+
using System.Threading.Tasks;
5+
6+
namespace Frank.Scheduler.Api.Jobs
7+
{
8+
public class SchedulerJob : IJob
9+
{
10+
private readonly ILogger<SchedulerJob> _logger;
11+
private readonly IScheduledTaskService _scheduledTaskService;
12+
13+
14+
public SchedulerJob(ILogger<SchedulerJob> logger, IScheduledTaskService scheduledTaskService)
15+
{
16+
_logger = logger;
17+
_scheduledTaskService = scheduledTaskService;
18+
}
19+
20+
public async Task RunAsync()
21+
{
22+
var tasks = await _scheduledTaskService.GetPendingTasksAsync();
23+
24+
25+
26+
}
27+
}
28+
}

Frank.Scheduler.Api/Jobs/SchedulerService.cs

-22
This file was deleted.

Frank.Scheduler.Api/Properties/launchSettings.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
},
1010
"profiles": {
1111
"Frank.Scheduler.Api (Development)": {
12+
"commandName": "Project",
13+
"environmentVariables": {
14+
"ASPNETCORE_ENVIRONMENT": "Development"
15+
},
16+
"applicationUrl": "http://localhost:5110"
17+
},
18+
"Frank.Scheduler.Api (Test)": {
19+
"commandName": "Project",
20+
"environmentVariables": {
21+
"ASPNETCORE_ENVIRONMENT": "Test"
22+
},
23+
"applicationUrl": "http://localhost:5110"
24+
},
25+
"Swagger (Development)": {
1226
"commandName": "Project",
1327
"launchBrowser": true,
1428
"launchUrl": "swagger",
@@ -17,7 +31,7 @@
1731
},
1832
"applicationUrl": "http://localhost:5110"
1933
},
20-
"Frank.Scheduler.Api (Test)": {
34+
"Swagger (Test)": {
2135
"commandName": "Project",
2236
"launchBrowser": true,
2337
"launchUrl": "swagger",
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Azure.ServiceBus;
24

35
namespace Frank.Scheduler.Api.ServiceBus
46
{
57
public interface IServiceBusService
68
{
7-
Task SendMessage(string message);
9+
Task<Message> SendMessage<T>(Guid messageId, string messageLabel, T body);
10+
Task<Message> SendMessage(Guid messageId, string messageLabel, string messageBody);
811
}
912
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
1+
using Microsoft.Azure.ServiceBus;
2+
using Microsoft.Extensions.Options;
3+
using System;
24
using System.Text;
5+
using System.Text.Json;
36
using System.Threading.Tasks;
4-
using Microsoft.Azure.ServiceBus;
5-
using Microsoft.Extensions.Options;
67

78
namespace Frank.Scheduler.Api.ServiceBus
89
{
@@ -15,20 +16,30 @@ public ServiceBusService(IOptions<ServiceBusConfiguration> options)
1516
_options = options.Value;
1617
}
1718

18-
public async Task SendMessage(string message) => await SendMessage(Encoding.UTF8.GetBytes(message));
19+
public async Task<Message> SendMessage<T>(Guid messageId, string messageLabel, T body) =>
20+
await SendMessage(new Message(JsonSerializer.SerializeToUtf8Bytes(body))
21+
{
22+
Label = messageLabel,
23+
ContentType = "application/json",
24+
MessageId = messageId.ToString(),
25+
TimeToLive = TimeSpan.FromHours(1)
26+
});
1927

20-
private async Task SendMessage(byte[] body)
21-
{
22-
var message = new Message
28+
public async Task<Message> SendMessage(Guid messageId, string messageLabel, string messageBody) =>
29+
await SendMessage(new Message(Encoding.UTF8.GetBytes(messageBody))
2330
{
24-
Body = body,
31+
Label = messageLabel,
2532
ContentType = "application/json",
26-
TimeToLive = TimeSpan.FromHours(24),
27-
Label = _options.Filter
28-
};
33+
MessageId = messageId.ToString(),
34+
TimeToLive = TimeSpan.FromHours(1)
35+
});
2936

37+
private async Task<Message> SendMessage(Message message)
38+
{
3039
var topicClient = new TopicClient(_options.Endpoint, _options.TopicName, RetryPolicy.Default);
3140
await topicClient.SendAsync(message);
41+
42+
return message;
3243
}
3344
}
3445
}

Frank.Scheduler.Api/Startup.cs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using CronQuery.Mvc.DependencyInjection;
2+
using Frank.Scheduler.Api.Jobs;
3+
using Frank.Scheduler.Api.ServiceBus;
4+
using Frank.Scheduler.Data;
5+
using Frank.Scheduler.Services;
6+
using Microsoft.AspNetCore.Builder;
27
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.EntityFrameworkCore;
39
using Microsoft.Extensions.Configuration;
410
using Microsoft.Extensions.DependencyInjection;
511
using Microsoft.Extensions.Hosting;
@@ -19,6 +25,32 @@ public Startup(IConfiguration configuration)
1925
// This method gets called by the runtime. Use this method to add services to the container.
2026
public void ConfigureServices(IServiceCollection services)
2127
{
28+
// Scheduled jobs
29+
services.AddCronQuery(Configuration.GetSection("CronQuery"));
30+
services.AddScoped<SchedulerJob>();
31+
32+
// Service bus listeners
33+
services.AddHostedService<ApplicationRegistrationConsumer>();
34+
services.AddHostedService<ScheduledTaskCallbackConsumer>();
35+
36+
// Services
37+
services.AddScoped<IScheduledTaskService, ScheduledTaskService>();
38+
services.AddScoped<IServiceBusService, ServiceBusService>();
39+
40+
// Service bus
41+
services.Configure<ServiceBusConfiguration>(Configuration.GetSection(nameof(ServiceBusConfiguration)));
42+
43+
// Database
44+
services.AddDbContext<DataContext>(options =>
45+
{
46+
options.UseSqlServer(Configuration.GetConnectionString("SqlDatabase"));
47+
#if DEBUG
48+
options.EnableDetailedErrors();
49+
options.EnableSensitiveDataLogging();
50+
#endif
51+
});
52+
53+
// MVC
2254
services.AddControllers();
2355
services.AddSwaggerGen(c =>
2456
{
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"ConnectionStrings": {
10+
"SqlDatabase": ""
11+
},
12+
"CronQuery": {
13+
"Running": true,
14+
"Jobs": [
15+
{
16+
"Cron": "*/10 * * * * *",
17+
"Name": "SchedulerJob",
18+
"Running": true
19+
}
20+
]
721
}
8-
}
922
}

Frank.Scheduler.Api/appsettings.json

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*",
10+
"ConnectionStrings": {
11+
"SqlDatabase": ""
12+
},
13+
"CronQuery": {
14+
"Running": true,
15+
"Jobs": [
16+
{
17+
"Cron": "* */1 * * * *",
18+
"Name": "SchedulerJob",
19+
"Running": true
20+
}
21+
]
722
}
8-
},
9-
"AllowedHosts": "*"
1023
}

Frank.Scheduler.Client/Configuration/SchedulerConfiguration.cs

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Frank.Scheduler.Client.Configuration
4+
{
5+
public class SchedulerRestConfiguration
6+
{
7+
public Uri BaseUrl { get; set; }
8+
9+
public string ApiVersion { get; set; }
10+
11+
public bool ExtendedLogging { get; set; }
12+
13+
public bool ForceInternalToken { get; set; }
14+
15+
public int? Timeout { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Frank.Scheduler.Client.Configuration
2+
{
3+
public class SchedulerServiceBusConfiguration
4+
{
5+
public string ServiceBusEndpoint { get; set; }
6+
public string ServiceBusTopicName { get; set; }
7+
public short LockDurationMinutes { get; set; }
8+
public string MicroServiceConsumerLabel { get; set; }
9+
}
10+
}

Frank.Scheduler.Client/Frank.Scheduler.Client.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
1212
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
14+
<PackageReference Include="RestSharp" Version="106.11.7" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Frank.Scheduler.Client.Configuration;
2+
using RestSharp;
3+
using System;
4+
using System.Text;
5+
6+
namespace Frank.Scheduler.Client.Rest
7+
{
8+
public class AuthenticatorService
9+
{
10+
public AccessToken GetToken(SchedulerRestConfiguration restConfiguration, AuthenticationOptions authenticationOptions)
11+
{
12+
var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(authenticationOptions.ClientId + ":" + authenticationOptions.ClientSecret));
13+
var client = new RestClient(authenticationOptions.BaseUrl);
14+
client.Timeout = 30;
15+
var request = new RestRequest(Method.POST);
16+
request.AddHeader("Authorization", $"Basic {base64String}");
17+
request.AlwaysMultipartFormData = true;
18+
request.AddParameter("grant_type", "password");
19+
request.AddParameter("scope", authenticationOptions.Scope);
20+
request.AddParameter("username", authenticationOptions.UserName);
21+
request.AddParameter("password", authenticationOptions.Password);
22+
var response = client.Execute<AccessToken>(request);
23+
24+
return response.Data;
25+
}
26+
}
27+
28+
public class AccessToken
29+
{
30+
public string? Token { get; set; }
31+
32+
public string? TokenType { get; set; }
33+
34+
public string? Scope { get; set; }
35+
}
36+
37+
public class AuthenticationOptions
38+
{
39+
public AuthenticationOptions(string baseUrl, string userName, string password, string companyId, string clientId, string clientSecret, string scope)
40+
{
41+
BaseUrl = baseUrl;
42+
UserName = userName;
43+
Password = password;
44+
CompanyId = companyId;
45+
ClientId = clientId;
46+
ClientSecret = clientSecret;
47+
Scope = scope;
48+
}
49+
50+
public string? BaseUrl;
51+
public string? UserName;
52+
public string? Password;
53+
public string? CompanyId;
54+
public string? ClientId;
55+
public string? ClientSecret;
56+
public string? Scope;
57+
}
58+
}

0 commit comments

Comments
 (0)