diff --git a/src/DotXxlJob.Core/CommandExecutors/BeatCommandExecutor.cs b/src/DotXxlJob.Core/CommandExecutors/BeatCommandExecutor.cs new file mode 100644 index 0000000..41746f0 --- /dev/null +++ b/src/DotXxlJob.Core/CommandExecutors/BeatCommandExecutor.cs @@ -0,0 +1,19 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using DotXxlJob.Core.Models; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core.CommandExecutors +{ + public class BeatCommandExecutor:ICommandExecutor + { + public string CommandName => "Beat"; + + public Task ExecuteAsync(byte[] payload,CancellationToken cancellationToken = default) + { + return Task.FromResult(ExecutorResult.Success()); + } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/CommandExecutors/IdleBeatCommandExecutor.cs b/src/DotXxlJob.Core/CommandExecutors/IdleBeatCommandExecutor.cs new file mode 100644 index 0000000..381d840 --- /dev/null +++ b/src/DotXxlJob.Core/CommandExecutors/IdleBeatCommandExecutor.cs @@ -0,0 +1,30 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using DotXxlJob.Core.Models; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core.CommandExecutors +{ + public class IdleBeatCommandExecutor:ICommandExecutor + { + private readonly ISerializer _serializer; + + public IdleBeatCommandExecutor(ISerializer serializer) + { + _serializer = serializer; + } + public string CommandName => "IdleBeat"; + + public Task ExecuteAsync(byte[] payload,CancellationToken cancellationToken = default) + { + var idleBeat = _serializer.Deserialize(payload); + if (idleBeat == null) + { + return Task.FromResult(ExecutorResult.Failure("Command[IdleBrat],parameter is empty")); + } + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/CommandExecutors/KillCommandExecutor.cs b/src/DotXxlJob.Core/CommandExecutors/KillCommandExecutor.cs new file mode 100644 index 0000000..1bd94a1 --- /dev/null +++ b/src/DotXxlJob.Core/CommandExecutors/KillCommandExecutor.cs @@ -0,0 +1,29 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using DotXxlJob.Core.Models; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core.CommandExecutors +{ + public class KillCommandExecutor: ICommandExecutor + { + private readonly ISerializer _serializer; + + public KillCommandExecutor(ISerializer serializer) + { + _serializer = serializer; + } + public string CommandName => "kill"; + public Task ExecuteAsync(byte[] payload, CancellationToken cancellationToken) + { + var command = _serializer.Deserialize(payload); + if (command == null) + { + return Task.FromResult(ExecutorResult.Failure("Command[Kill],parameter is empty")); + } + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/CommandExecutors/TriggerCommandExecutor.cs b/src/DotXxlJob.Core/CommandExecutors/TriggerCommandExecutor.cs new file mode 100644 index 0000000..08ccc0c --- /dev/null +++ b/src/DotXxlJob.Core/CommandExecutors/TriggerCommandExecutor.cs @@ -0,0 +1,29 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using DotXxlJob.Core.Models; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core.CommandExecutors +{ + public class TriggerCommandExecutor: ICommandExecutor + { + private readonly ISerializer _serializer; + + public TriggerCommandExecutor(ISerializer serializer) + { + _serializer = serializer; + } + public string CommandName => "Run"; + public Task ExecuteAsync(byte[] payload, CancellationToken cancellationToken) + { + var command = _serializer.Deserialize(payload); + if (command== null) + { + return Task.FromResult(ExecutorResult.Failure("command[run],parameter is empty")); + } + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/DotXxlJob.Core.csproj b/src/DotXxlJob.Core/DotXxlJob.Core.csproj index b4b43f4..e56f99a 100644 --- a/src/DotXxlJob.Core/DotXxlJob.Core.csproj +++ b/src/DotXxlJob.Core/DotXxlJob.Core.csproj @@ -5,4 +5,8 @@ enable + + + + diff --git a/src/DotXxlJob.Core/ICommandExecutor.cs b/src/DotXxlJob.Core/ICommandExecutor.cs new file mode 100644 index 0000000..1195b17 --- /dev/null +++ b/src/DotXxlJob.Core/ICommandExecutor.cs @@ -0,0 +1,17 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using DotXxlJob.Core.Models; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core +{ + public interface ICommandExecutor + { + string CommandName { get; } + Task ExecuteAsync(byte[] payload,CancellationToken cancellationToken); + } + + +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/ICommandExecutorFactory.cs b/src/DotXxlJob.Core/ICommandExecutorFactory.cs new file mode 100644 index 0000000..3c88a32 --- /dev/null +++ b/src/DotXxlJob.Core/ICommandExecutorFactory.cs @@ -0,0 +1,10 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +namespace DotXxlJob.Core +{ + public interface ICommandExecutorFactory + { + ICommandExecutor GetCommandExecutor(string commandName); + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/ISerializer.cs b/src/DotXxlJob.Core/ISerializer.cs new file mode 100644 index 0000000..334b53f --- /dev/null +++ b/src/DotXxlJob.Core/ISerializer.cs @@ -0,0 +1,18 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core +{ + public interface ISerializer + { + T? Deserialize(byte[] data) where T : class; + byte[] Serialize(T item) where T : class; + object? Deserialize(byte[] data,Type type); + byte[] Serialize(object item,Type type); + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Internal/TextJsonSerializer.cs b/src/DotXxlJob.Core/Internal/TextJsonSerializer.cs new file mode 100644 index 0000000..e833234 --- /dev/null +++ b/src/DotXxlJob.Core/Internal/TextJsonSerializer.cs @@ -0,0 +1,39 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System; +using System.IO; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace DotXxlJob.Core.Internal +{ + public class TextJsonSerializer:ISerializer + { + public T? Deserialize(byte[] data) where T : class + { + var json = Encoding.UTF8.GetString(data); + return JsonSerializer.Deserialize(json); + } + + public byte[] Serialize(T item) where T : class + { + return JsonSerializer.SerializeToUtf8Bytes(item); + } + + public object? Deserialize(byte[] data, Type type) + { + var json = Encoding.UTF8.GetString(data); + return JsonSerializer.Deserialize(json,type); + } + + public byte[] Serialize(object item,Type type) + { + return JsonSerializer.SerializeToUtf8Bytes(item); + } + + + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Models/ExecutorResult.cs b/src/DotXxlJob.Core/Models/ExecutorResult.cs new file mode 100644 index 0000000..80fdac6 --- /dev/null +++ b/src/DotXxlJob.Core/Models/ExecutorResult.cs @@ -0,0 +1,40 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System.Runtime.Serialization; + +namespace DotXxlJob.Core.Models +{ + public class ExecutorResult + { + protected const int SUCCESS_CODE = 200; + protected const int FAILURE_CODE = 500; + protected const int TIMEOUT_CODE = 502; + [DataMember(Name = "code",Order = 1)] + public int Code { get; set; } + [DataMember(Name = "msg",Order = 2)] + public string? Message { get; set; } + + public static ExecutorResult Success(string message="") + { + return new ExecutorResult(){Code = SUCCESS_CODE, Message = message}; + } + public static ExecutorResult Failure(string message) + { + return new ExecutorResult(){Code = FAILURE_CODE, Message = message}; + } + + } + + public class ExecutorResult : ExecutorResult where T : class + { + [DataMember(Name = "content",Order = 3)] + public T Data { get; set; } = default!; + + public static ExecutorResult Success(string message, T data) + { + return new ExecutorResult(){Code = SUCCESS_CODE, Message = message,Data = data}; + } + + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Models/IdleBeatCommand.cs b/src/DotXxlJob.Core/Models/IdleBeatCommand.cs new file mode 100644 index 0000000..9ba9073 --- /dev/null +++ b/src/DotXxlJob.Core/Models/IdleBeatCommand.cs @@ -0,0 +1,14 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System.Runtime.Serialization; + +namespace DotXxlJob.Core.Models +{ + [DataContract] + public class IdleBeatCommand + { + [DataMember(Name = "jobId", Order = 1)] + public int JobId { get; set; } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Models/KillCommand.cs b/src/DotXxlJob.Core/Models/KillCommand.cs new file mode 100644 index 0000000..70f682f --- /dev/null +++ b/src/DotXxlJob.Core/Models/KillCommand.cs @@ -0,0 +1,14 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System.Runtime.Serialization; + +namespace DotXxlJob.Core.Models +{ + [DataContract] + public class KillCommand + { + [DataMember(Name = "jobId", Order = 1)] + public int JobId { get; set; } + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Models/TriggerCommand.cs b/src/DotXxlJob.Core/Models/TriggerCommand.cs new file mode 100644 index 0000000..2d3d257 --- /dev/null +++ b/src/DotXxlJob.Core/Models/TriggerCommand.cs @@ -0,0 +1,46 @@ +// Copyright (c) Xuanye Wong. All rights reserved. +// Licensed under MIT license + +using System.Runtime.Serialization; + +namespace DotXxlJob.Core.Models +{ + [DataContract] + public class TriggerCommand + { + [DataMember(Name = "jobId", Order = 1)] + public int JobId { get; set; } + + [DataMember(Name = "executorHandler", Order = 2)] + public string ExecutorHandler { get; set; } = null!; + + [DataMember(Name = "executorParams", Order = 3)] + public string ExecutorParams{ get; set; } = null!; + + [DataMember(Name = "executorBlockStrategy", Order = 4)] + public string ExecutorBlockStrategy{ get; set; }= null!; + + [DataMember(Name = "executorTimeout", Order = 5)] + public int ExecutorTimeout{ get; set; } + + [DataMember(Name = "logId",Order = 5)] + public long LogId { get; set; } + [DataMember(Name = "logDateTime", Order = 6)] + public long LogDateTime{ get; set; } + + + [DataMember(Name = "glueType",Order = 7)] + public string? GlueType{ get; set; } + + [DataMember(Name = "glueSource",Order = 8)] + public string? GlueSource{ get; set; } + + [DataMember(Name = "glueUpdatetime", Order = 9)] + public long GlueUpdateTime{ get; set; } + + [DataMember(Name = "broadcastIndex",Order = 10)] + public int BroadcastIndex{ get; set; } + [DataMember(Name = "broadcastTotal",Order = 11)] + public int BroadcastTotal{ get; set; } + } +} \ No newline at end of file