Skip to content

Commit

Permalink
Merge pull request #10 from EasyOC/TimerEventUseSiteTimeZone
Browse files Browse the repository at this point in the history
Timer event use site time zone
  • Loading branch information
hyzx86 committed Feb 23, 2023
2 parents 4af2a40 + f2d513d commit ea600a5
Show file tree
Hide file tree
Showing 21 changed files with 393 additions and 5 deletions.
13 changes: 13 additions & 0 deletions EasyOC.Modules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{2C8B
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyOC.Constants", "src\Shared\EasyOC.Constants\EasyOC.Constants.csproj", "{8C6A4607-9E38-43C3-8738-DDFAE12B9B6B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyOC.CMS.Web", "src\EasyOC.CMS.Web\EasyOC.CMS.Web.csproj", "{6EC107CD-4BA4-4F27-B275-BDFFD2403C55}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyOC.Workflows", "src\Modules\EasyOC.Workflows\EasyOC.Workflows.csproj", "{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -58,6 +62,14 @@ Global
{8C6A4607-9E38-43C3-8738-DDFAE12B9B6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C6A4607-9E38-43C3-8738-DDFAE12B9B6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C6A4607-9E38-43C3-8738-DDFAE12B9B6B}.Release|Any CPU.Build.0 = Release|Any CPU
{6EC107CD-4BA4-4F27-B275-BDFFD2403C55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6EC107CD-4BA4-4F27-B275-BDFFD2403C55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EC107CD-4BA4-4F27-B275-BDFFD2403C55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EC107CD-4BA4-4F27-B275-BDFFD2403C55}.Release|Any CPU.Build.0 = Release|Any CPU
{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -68,6 +80,7 @@ Global
{9C4736A2-1005-4132-9548-B2E1F3C830E0} = {2C8B39D9-E86E-4BB6-ABAD-22CA1B2EA299}
{D4572AB9-35A0-4D0A-B6B7-3A49F26BFF31} = {BBA3C688-E8AD-4177-9817-24D3C097D98A}
{8C6A4607-9E38-43C3-8738-DDFAE12B9B6B} = {BBA3C688-E8AD-4177-9817-24D3C097D98A}
{EB0C3FDC-1125-4194-9FCA-1EE7A1B6FA8B} = {2C8B39D9-E86E-4BB6-ABAD-22CA1B2EA299}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {71091DB5-1217-403F-AC42-EB7115ADAB2C}
Expand Down
33 changes: 33 additions & 0 deletions src/EasyOC.CMS.Web/EasyOC.CMS.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<Folder Include="Localization\" />
</ItemGroup>

<!-- Watcher include and excludes -->
<ItemGroup>
<Watch Include="**\*.cs" Exclude="Recipes\**;Assets\**;node_modules\**\*;**\*.js.map;obj\**\*;bin\**\*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Logging.NLog" Version="1.5.0" />
<PackageReference Include="OrchardCore.Application.Cms.Targets" Version="1.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Modules\EasyOC.ReplaceAction\EasyOC.ReplaceAction.csproj" />
<ProjectReference Include="..\Modules\EasyOC.SwaggerUI\EasyOC.SwaggerUI.csproj" />
<ProjectReference Include="..\Modules\EasyOC.Users\EasyOC.Users.csproj" />
<ProjectReference Include="..\Modules\EasyOC.Workflows\EasyOC.Workflows.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/EasyOC.CMS.Web/Localization/.placeholder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
32 changes: 32 additions & 0 deletions src/EasyOC.CMS.Web/NLog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="App_Data/logs/internal-nlog.txt">

<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="OrchardCore.Logging.NLog"/>
</extensions>

<targets>
<!-- file target -->
<target xsi:type="File" name="file"
fileName="${var:configDir}/App_Data/logs/orchard-log-${shortdate}.log"
layout="${longdate}|${orchard-tenant-name}|${aspnet-traceidentifier}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception:format=ToString,StackTrace}"
/>

<!-- console target -->
<target xsi:type="Console" name="console" />

</targets>

<rules>
<!-- all warnings and above go to the file target -->
<logger name="*" minlevel="Warn" writeTo="file" />

<!-- the hosting lifetime events go to the console and file targets -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="file, console" />
</rules>
</nlog>
30 changes: 30 additions & 0 deletions src/EasyOC.CMS.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using OrchardCore.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseNLogHost();

builder.Services
.AddOrchardCms()
// // Orchard Specific Pipeline
// .ConfigureServices( services => {
// })
// .Configure( (app, routes, services) => {
// })
;

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseOrchardCore();

app.Run();
27 changes: 27 additions & 0 deletions src/EasyOC.CMS.Web/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:8080",
"sslPort": 44300
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"EasyOC.CMS.Web": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 9 additions & 0 deletions src/EasyOC.CMS.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
}
1 change: 1 addition & 0 deletions src/EasyOC.CMS.Web/wwwroot/.placeholder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
9 changes: 4 additions & 5 deletions src/Modules/EasyOC.ReplaceAction/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace EasyOC.ReplaceAction
Expand Down Expand Up @@ -69,6 +67,7 @@ public static IServiceCollection ReplaceActionByActionNames<TNew>(this IServiceC
public static IServiceProvider UseReplaceAction(this IServiceProvider serviceProvider)
{
var rOptions = serviceProvider.GetRequiredService<IOptions<ActionReplaceOption>>();
var logger = serviceProvider.GetService<ILogger>();
var config = rOptions.Value;

var descriptors = serviceProvider.GetRequiredService<IActionDescriptorCollectionProvider>()
Expand Down Expand Up @@ -96,9 +95,9 @@ public static IServiceProvider UseReplaceAction(this IServiceProvider servicePro
{
descriptor.ControllerTypeInfo = item.NewController.GetTypeInfo();
descriptor.MethodInfo = item.ActionMapping[descriptor.ActionName];
if (logger != null && logger.IsDebugEnabled)
if (logger != null && logger.IsEnabled(LogLevel.Debug))
{
logger.DebugFormat("The Action:{action} of controller:{type} is replaced by {newContorller}.{method}",
logger.LogDebug("The Action:{action} of controller:{type} is replaced by {newContorller}.{method}",
item.TargetControllerFullName,
descriptor.ActionName,
descriptor.ControllerTypeInfo.FullName,
Expand Down
7 changes: 7 additions & 0 deletions src/Modules/EasyOC.Workflows/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyOC.Workflows
{
public class Constants
{
public const string TimersFeautreId = "EasyOC.Workflows.Timers";
}
}
26 changes: 26 additions & 0 deletions src/Modules/EasyOC.Workflows/EasyOC.Workflows.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>

<PackageReference Include="OrchardCore.Module.Targets" Version="1.5.0" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.ContentTypes.Abstractions" Version="1.5.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.Workflows" Version="$(OrchardCoreVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Shared\EasyOC.Constants\EasyOC.Constants.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Modules/EasyOC.Workflows/Manifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OrchardCore.Modules.Manifest;
using static EasyOC.Constants.ManifestConstants;
[assembly: Module(
Author = Author,
Website = Website,
Version = CurrentVersion,
Description = "EasyOC.Workflows",
Category = "Workflows"
)]
[assembly: Feature(
Id = EasyOC.Workflows.Constants.TimersFeautreId,
//Name = "Timer Workflow Activty (Support site time zone)",
Dependencies = new[] {
"OrchardCore.Workflows.Timers" },
Description = "Make Timer Workflow Activty support site time zone.",
Category = "Workflows"
)]
33 changes: 33 additions & 0 deletions src/Modules/EasyOC.Workflows/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EasyOC.Workflows.Timers;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Modules;
using OrchardCore.Workflows.Options;
using OCTimerEvent = OrchardCore.Workflows.Timers.TimerEvent;
namespace EasyOC.Workflows
{
public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{

}

}
[Feature(Constants.TimersFeautreId)]
[RequireFeatures("OrchardCore.Workflows.Timers")]
public class TimerStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.Configure<WorkflowOptions>(options =>
{
if (options.IsActivityRegistered<OCTimerEvent>())
{
options.UnregisterActivityType<OCTimerEvent>();
}
options.RegisterActivity<TimerEvent, TimerEventDisplayDriver>();
});

}
}
}
85 changes: 85 additions & 0 deletions src/Modules/EasyOC.Workflows/Timers/TimerEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.Extensions.Localization;
using NCrontab;
using OrchardCore.Modules;
using OrchardCore.Settings;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;

namespace EasyOC.Workflows.Timers
{
public class TimerEvent : EventActivity
{
public static string EventName => nameof(TimerEvent);
private readonly IClock _clock;
private readonly IStringLocalizer S;
private readonly ISiteService _siteService;

public TimerEvent(IClock clock, IStringLocalizer<TimerEvent> localizer, ISiteService siteService)
{
_clock = clock;
S = localizer;
_siteService = siteService;
}

public override string Name => EventName;

public override LocalizedString DisplayText => S["Timer Event"];

public override LocalizedString Category => S["Background"];

public string CronExpression
{
get => GetProperty(() => "*/5 * * * *");
set => SetProperty(value);
}
public bool UseSiteTimeZone
{
get => GetProperty(() => false);
set => SetProperty(value);
}

private DateTime? StartedTime
{
get => GetProperty<DateTime?>();
set => SetProperty(value);
}

public override async Task<bool> CanExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return StartedTime == null || await IsExpired();
}

public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Done"]);
}

public override async Task<ActivityExecutionResult> ResumeAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
if (await IsExpired())
{
workflowContext.LastResult = "TimerEvent";
return Outcomes("Done");
}

return Halt();
}

private async Task<bool> IsExpired()
{
DateTime when;
var now = _clock.UtcNow;
if (UseSiteTimeZone)
{
var timeZoneId = (await _siteService.GetSiteSettingsAsync()).TimeZoneId;
now = _clock.ConvertToTimeZone(new DateTimeOffset(now), _clock.GetTimeZone(timeZoneId)).DateTime;
}
StartedTime ??= now;

var schedule = CrontabSchedule.Parse(CronExpression);
when = schedule.GetNextOccurrence(StartedTime.Value);
return now >= when;
}
}
}
Loading

0 comments on commit ea600a5

Please sign in to comment.