Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Scheduler SDK & SDK UnitTest #102

Merged
merged 7 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BasicAbility", "BasicAbilit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Scheduler", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Scheduler\Masa.BuildingBlocks.BasicAbility.Scheduler.csproj", "{0EB0CB69-4C44-4462-A92A-A9B9FDA171DE}"
EndProject

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Scheduler.Tests", "test\Masa.Contrib.BasicAbility.Scheduler.Tests\Masa.Contrib.BasicAbility.Scheduler.Tests.csproj", "{5980D054-E2F3-4143-93D1-01993A955AE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Scheduler", "src\BasicAbility\Masa.Contrib.BasicAbility.Scheduler\Masa.Contrib.BasicAbility.Scheduler.csproj", "{8D84666E-C79E-4D49-B73D-360E62D312EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1230,6 +1235,38 @@ Global
{789ABED5-7C94-4F6F-ADCA-E97F3DFC9479}.Release|Any CPU.Build.0 = Release|Any CPU
{789ABED5-7C94-4F6F-ADCA-E97F3DFC9479}.Release|x64.ActiveCfg = Release|Any CPU
{789ABED5-7C94-4F6F-ADCA-E97F3DFC9479}.Release|x64.Build.0 = Release|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Debug|x64.ActiveCfg = Debug|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Debug|x64.Build.0 = Debug|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Release|Any CPU.Build.0 = Release|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Release|x64.ActiveCfg = Release|Any CPU
{D91C3145-C31C-4301-A493-96F94C903CF2}.Release|x64.Build.0 = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Debug|x64.ActiveCfg = Debug|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Debug|x64.Build.0 = Debug|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.Build.0 = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.ActiveCfg = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.Build.0 = Release|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Debug|x64.ActiveCfg = Debug|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Debug|x64.Build.0 = Debug|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Release|Any CPU.Build.0 = Release|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Release|x64.ActiveCfg = Release|Any CPU
{5980D054-E2F3-4143-93D1-01993A955AE7}.Release|x64.Build.0 = Release|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Debug|x64.ActiveCfg = Debug|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Debug|x64.Build.0 = Debug|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Release|Any CPU.Build.0 = Release|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Release|x64.ActiveCfg = Release|Any CPU
{8D84666E-C79E-4D49-B73D-360E62D312EF}.Release|x64.Build.0 = Release|Any CPU
{981E883E-CCDC-400B-8FB1-76E1E65C32AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{981E883E-CCDC-400B-8FB1-76E1E65C32AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{981E883E-CCDC-400B-8FB1-76E1E65C32AF}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -1393,6 +1430,8 @@ Global
{FF3FD53D-D23E-48AC-98B6-3B028B4DE8AF} = {0D34A7F0-DC77-4789-A136-93089CBD15C3}
{83FA668F-C838-4883-996D-AF2ECF00FDF8} = {5DFAF4A2-ECB5-46E4-904D-1EA5F48B2D48}
{789ABED5-7C94-4F6F-ADCA-E97F3DFC9479} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{5980D054-E2F3-4143-93D1-01993A955AE7} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{8D84666E-C79E-4D49-B73D-360E62D312EF} = {5DFAF4A2-ECB5-46E4-904D-1EA5F48B2D48}
{981E883E-CCDC-400B-8FB1-76E1E65C32AF} = {0D34A7F0-DC77-4789-A136-93089CBD15C3}
{0D34A7F0-DC77-4789-A136-93089CBD15C3} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{0EB0CB69-4C44-4462-A92A-A9B9FDA171DE} = {0D34A7F0-DC77-4789-A136-93089CBD15C3}
Expand Down
10 changes: 10 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Scheduler/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Scheduler;

internal class Constants
{
internal const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.scheduler";
internal const string ENVIRONMENT_KEY = "env_key";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="$(MasaUtilsPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Scheduler\Masa.BuildingBlocks.BasicAbility.Scheduler.csproj" />
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\Identity\Masa.BuildingBlocks.Identity.IdentityModel\Masa.BuildingBlocks.Identity.IdentityModel.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Scheduler;

public class SchedulerClient : ISchedulerClient
{
public ISchedulerJobService SchedulerJobService { get; }

public ISchedulerTaskService SchedulerTaskService { get; }

public SchedulerClient(ICallerProvider callerProvider, ILoggerFactory? loggerFactory = null)
{
SchedulerJobService = new SchedulerJobService(callerProvider, loggerFactory);
SchedulerTaskService = new SchedulerTaskService(callerProvider, loggerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSchedulerClient(this IServiceCollection services, string schedulerServiceBaseAddress)
{
if (string.IsNullOrWhiteSpace(schedulerServiceBaseAddress))
{
throw new ArgumentNullException(nameof(schedulerServiceBaseAddress));
}

services.AddSingleton<IRequestMessage, JsonRequestMessage>();
return services.AddSchedulerClient(callerOptions =>
{
callerOptions.UseHttpClient(builder =>
{
builder.Name = DEFAULT_CLIENT_NAME;
builder.Configure = opt => opt.BaseAddress = new Uri(schedulerServiceBaseAddress);
});
});
}

public static IServiceCollection AddSchedulerClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
{
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions));

if (services.Any(service => service.ImplementationType == typeof(SchedulerProvider)))
return services;

services.AddSingleton<SchedulerProvider>();
services.AddHttpContextAccessor();
services.AddCaller(callerOptions);

services.AddScoped<ISchedulerClient>(serviceProvider =>
{
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
var schedulerClient = new SchedulerClient(callProvider, loggerFactory);
return schedulerClient;
});

return services;
}

private class SchedulerProvider
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Scheduler.Services;

public class SchedulerJobService : ISchedulerJobService
{
const string API = "/api/scheduler-job";

readonly ICallerProvider _callerProvider;
readonly ILogger<SchedulerJobService>? _logger;

public SchedulerJobService(ICallerProvider callerProvider, ILoggerFactory? loggerFactory = null)
{
_callerProvider = callerProvider;
_logger = loggerFactory?.CreateLogger<SchedulerJobService>();
}

public async Task<Guid> AddbAsync(AddSchedulerJobRequest request)
{
if (string.IsNullOrWhiteSpace(request.ProjectIdentity))
{
throw new ArgumentNullException(nameof(request.ProjectIdentity));
}

switch (request.JobType)
{
case BuildingBlocks.BasicAbility.Scheduler.Enum.JobTypes.JobApp:
ArgumentNullException.ThrowIfNull(request.JobAppConfig, nameof(request.JobAppConfig));
break;
case BuildingBlocks.BasicAbility.Scheduler.Enum.JobTypes.Http:
ArgumentNullException.ThrowIfNull(request.HttpConfig, nameof(request.HttpConfig));
break;
case BuildingBlocks.BasicAbility.Scheduler.Enum.JobTypes.DaprServiceInvocation:
ArgumentNullException.ThrowIfNull(request.DaprServiceInvocationConfig, nameof(request.DaprServiceInvocationConfig));
break;
}

try
{
var requestUri = $"{API}/addSchedulerJobBySdk";
return await _callerProvider.PostAsync<AddSchedulerJobRequest, Guid>(requestUri, request);
}
catch (Exception ex)
{
_logger?.LogError(ex, "AddSchedulerJobAsync Error");
return Guid.Empty;
}

}

public async Task<bool> DisableAsync(BaseSchedulerJobRequest request)
{
try
{
var requestData = new ChangeEnabledStatusRequest()
{
JobId = request.JobId,
OperatorId = request.OperatorId,
Enabled = false
};
var requestUri = $"{API}/changeEnableStatus";
await _callerProvider.PutAsync<ChangeEnabledStatusRequest>(requestUri, requestData);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "DisableSchedulerJob Error");
return false;
}
}

public async Task<bool> EnableAsync(BaseSchedulerJobRequest request)
{
try
{
var requestData = new ChangeEnabledStatusRequest()
{
JobId = request.JobId,
OperatorId = request.OperatorId,
Enabled = true
};
var requestUri = $"{API}/changeEnableStatus";
await _callerProvider.PutAsync<ChangeEnabledStatusRequest>(requestUri, requestData);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "EnableSchedulerJob Error");
return false;
}
}

public async Task<bool> RemoveAsync(BaseSchedulerJobRequest request)
{
try
{
var requestUri = $"{API}";
await _callerProvider.DeleteAsync(requestUri, request);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "RemoveSchedulerJobAsync Error");
return false;
}
}

public async Task<bool> StartAsync(BaseSchedulerJobRequest request)
{
try
{
var requestUri = $"{API}/startJob";
await _callerProvider.PutAsync(requestUri, request);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "StartSchedulerJobAsync Error");
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Scheduler.Services;

public class SchedulerTaskService : ISchedulerTaskService
{
const string API = "/api/scheduler-task";

readonly ICallerProvider _callerProvider;
readonly ILogger<SchedulerTaskService>? _logger;

public SchedulerTaskService(ICallerProvider callerProvider, ILoggerFactory? loggerFactory)
{
_callerProvider = callerProvider;
_logger = loggerFactory?.CreateLogger<SchedulerTaskService>();
}

public async Task<bool> StopAsync(BaseSchedulerTaskRequest request)
{
try
{
var requestUri = $"{API}/stop";
await _callerProvider.PutAsync(requestUri, request);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "StopSchedulerTaskAsync Error");
return false;
}
}

public async Task<bool> StartAsync(BaseSchedulerTaskRequest request)
{
try
{
var requestData = new StartSchedulerTaskRequest()
{
TaskId = request.TaskId,
OperatorId = request.OperatorId,
IsManual = true
};

var requestUri = $"{API}/start";
await _callerProvider.PutAsync(requestUri, requestData);
return true;
}
catch (Exception ex)
{
_logger?.LogError(ex, "StopSchedulerTaskAsync Error");
return false;
}
}
}
13 changes: 13 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Scheduler/_Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.BasicAbility.Scheduler;
global using Masa.BuildingBlocks.BasicAbility.Scheduler.Request;
global using Masa.BuildingBlocks.BasicAbility.Scheduler.Service;
global using Masa.BuildingBlocks.Identity.IdentityModel;
global using Masa.Contrib.BasicAbility.Scheduler;
global using Masa.Contrib.BasicAbility.Scheduler.Services;
global using Masa.Utils.Caller.Core;
global using Masa.Utils.Caller.HttpClient;
global using Microsoft.Extensions.Logging;
global using static Masa.Contrib.BasicAbility.Scheduler.Constants;
2 changes: 1 addition & 1 deletion src/BuildingBlocks/MASA.BuildingBlocks
Submodule MASA.BuildingBlocks updated 22 files
+4 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth.Contracts/Model/NavModel.cs
+0 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth.Contracts/_Imports.cs
+0 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/_Imports.cs
+13 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/HttpMethods.cs
+12 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/HttpVerifyTypes.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/JobTypes.cs
+10 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/RunTimeoutStrategyTypes.cs
+12 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/ScheduleBlockStrategyTypes.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Enum/ScheduleExpiredStrategyTypes.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/ISchedulerClient.cs
+4 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Model/JobContext.cs
+17 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Model/SchedulerJobAppConfig.cs
+15 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Model/SchedulerJobDaprServiceInvocationConfig.cs
+21 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Model/SchedulerJobHttpConfig.cs
+48 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Request/AddSchedulerJobRequest.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Request/BaseSchedulerJobRequest.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Request/BaseSchedulerTaskRequest.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Request/ChangeEnabledStatusRequest.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Request/StartSchedulerTaskRequest.cs
+17 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Service/ISchedulerJobService.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/Service/ISchedulerTaskService.cs
+3 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Scheduler/_Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
<PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BasicAbility\Masa.Contrib.BasicAbility.Scheduler\Masa.Contrib.BasicAbility.Scheduler.csproj" />
<ProjectReference Include="..\..\src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Scheduler\Masa.BuildingBlocks.BasicAbility.Scheduler.csproj" />
</ItemGroup>

</Project>
Loading