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

feature:Add a custom action method group #16

Merged
merged 2 commits into from
Nov 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,11 @@ private void ParameterVerification(int clientType, string clientVersion, string
if (versions == null) throw new ArgumentNullException(@"versions cannot be null !");
if (string.IsNullOrEmpty(clientAppkey) || string.IsNullOrEmpty(appSecretKey)) throw new NullReferenceException("The APP key does not exist !");
}


public string Report(int clientType, string clientVersion, string clientAppkey, string appSecretKey, string meesage, string dumpBase64, string logBase64, Exception exception) => string.Empty;


private void ParameterVerification(int clientType, string clientVersion, string clientAppkey, string appSecretKey, string meesage, string dumpBase64, string logBase64, Exception exception) { }
}
}
17 changes: 17 additions & 0 deletions src/c#/GeneralUpdate.AspNetCore/Services/IUpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GeneralUpdate.Core.Domain.DTO;
using System;
using System.Collections.Generic;

namespace GeneralUpdate.AspNetCore.Services
Expand All @@ -17,5 +18,21 @@ public interface IUpdateService
/// <param name="versions"></param>
/// <returns>Json object.</returns>
string Update(int clientType, string clientVersion, string serverLastVersion, string clientAppKey, string appSecretKey, bool isForce, List<VersionDTO> versions);

/// <summary>
/// When this web api is called at the end of the automatic update, it does not mean that every call is successful.
/// Failure, rollback, and success scenarios will inform the server of the result of the update through this web api.
/// If there is an exception let the decision maker decide whether to fix the problem by pushing the latest version of the update again.
/// </summary>
/// <param name="clientType">1:ClientApp 2:UpdateApp</param>
/// <param name="clientVersion">Current version of the client.</param>
/// <param name="clientAppkey">The appkey agreed by the client and server.</param>
/// <param name="appSecretKey">Appkey is stored in the database.</param>
/// <param name="meesage">The message from the client is used to describe the current situation to the decision maker.</param>
/// <param name="dumpBase64">If an exception occurs, the dump file is returned.</param>
/// <param name="logBase64">If an exception occurs, the log log is returned</param>
/// <param name="exception">If an exception occurs, the object is returned.</param>
/// <returns></returns>
string Report(int clientType, string clientVersion, string clientAppkey, string appSecretKey, string meesage, string dumpBase64, string logBase64, Exception exception);
}
}
76 changes: 68 additions & 8 deletions src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using GeneralUpdate.Core.Exceptions.CustomException;
using GeneralUpdate.Core.Strategys;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -21,11 +22,16 @@ namespace GeneralUpdate.ClientCore
/// </summary>
public class GeneralClientBootstrap : AbstractBootstrap<GeneralClientBootstrap, IStrategy>
{
private Func<bool> _customOption;
private Func<Task<bool>> _customTaskOption;
private Func<bool> _customSkipOption;
private Func<Task<bool>> _customSkipTaskOption;

private List<Func<bool>> _customOptions;
private List<Func<Task<bool>>> _customTaskOptions;

public GeneralClientBootstrap() : base()
{
_customOptions = new List<Func<bool>>();
_customTaskOptions = new List<Func<Task<bool>>>();
}

#region Public Methods
Expand All @@ -48,6 +54,8 @@ public override GeneralClientBootstrap LaunchAsync()

private async Task<GeneralClientBootstrap> BaseLaunch()
{
//bool isSuccess = await ExecuteCustomOptions();
//if (!isSuccess) return this;
var versionService = new VersionService();
var mainResp = await versionService.ValidationVersion(Packet.MainUpdateUrl);
var upgradeResp = await versionService.ValidationVersion(Packet.UpdateUrl);
Expand Down Expand Up @@ -126,10 +134,10 @@ public GeneralClientBootstrap Config(Configinfo info)
/// </summary>
/// <param name="func">Custom function ,Custom actions to let users decide whether to update. true update false do not update .</param>
/// <returns></returns>
public GeneralClientBootstrap SetCustomOption(Func<bool> func)
public GeneralClientBootstrap SetCustomSkipOption(Func<bool> func)
{
if (func == null) throw new ArgumentNullException(nameof(func));
_customOption = func;
_customSkipOption = func;
return this;
}

Expand All @@ -139,10 +147,38 @@ public GeneralClientBootstrap SetCustomOption(Func<bool> func)
/// <param name="func">Custom function ,Custom actions to let users decide whether to update. true update false do not update .</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public GeneralClientBootstrap SetCustomOption(Func<Task<bool>> func)
public GeneralClientBootstrap SetCustomSkipOption(Func<Task<bool>> func)
{
if (func == null) throw new ArgumentNullException(nameof(func));
_customTaskOption = func;
_customSkipTaskOption = func;
return this;
}

/// <summary>
/// Add an asynchronous custom operation.
/// In theory, any custom operation can be done. It is recommended to register the environment check method to ensure that there are normal dependencies and environments after the update is completed.
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public GeneralClientBootstrap AddCustomOption(Func<bool> func)
{
if(func == null) throw new ArgumentNullException(nameof(func));
_customOptions.Add(func);
return this;
}

/// <summary>
/// Add a synchronization custom operation.
/// In theory, any custom operation can be done. It is recommended to register the environment check method to ensure that there are normal dependencies and environments after the update is completed.
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public GeneralClientBootstrap AddCustomOption(Func<Task<bool>> func)
{
if (func == null) throw new ArgumentNullException(nameof(func));
_customTaskOptions.Add(func);
return this;
}

Expand Down Expand Up @@ -173,8 +209,8 @@ private async Task<bool> IsSkip(bool isForcibly)
{
bool isSkip = false;
if (isForcibly) return false;
if (_customTaskOption != null) isSkip = await _customTaskOption.Invoke();
if (_customOption != null) isSkip = _customOption.Invoke();
if (_customSkipTaskOption != null) isSkip = await _customSkipTaskOption.Invoke();
if (_customSkipOption != null) isSkip = _customSkipOption.Invoke();
return isSkip;
}
catch (Exception ex)
Expand All @@ -183,6 +219,30 @@ private async Task<bool> IsSkip(bool isForcibly)
}
}

/// <summary>
/// Performs all injected custom operations.
/// </summary>
/// <returns></returns>
private async Task<bool> ExecuteCustomOptions()
{
if (_customOptions.Any())
{
_customOptions.ForEach(option => option.Invoke());
return true;
}

if (_customTaskOptions.Any())
{
foreach (var option in _customTaskOptions)
{
await option.Invoke();
}
return true;
}

return true;
}

#endregion Private Methods
}
}
5 changes: 1 addition & 4 deletions src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ namespace GeneralUpdate.Core
{
public class GeneralUpdateBootstrap : AbstractBootstrap<GeneralUpdateBootstrap, IStrategy>
{
public GeneralUpdateBootstrap() : base()
{
Remote();
}
public GeneralUpdateBootstrap() : base()=> Remote();

/// <summary>
/// Gets values from system environment variables (ClientParameter object to base64 string).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private bool Clear()
/// <param name="callbackAction"></param>
private void WaitForProcessToStart(string applicationPath, TimeSpan timeout, Action<string> callbackAction = null)
{
using (Process process = Process.Start(applicationPath))
using (var process = Process.Start(applicationPath))
{
var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < timeout)
Expand Down
Loading