Skip to content

Commit

Permalink
feat: 新增支持OSX系统的Launch
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 8, 2023
1 parent b20a671 commit 52e1f0e
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 74 deletions.
84 changes: 84 additions & 0 deletions NewLife.Agent/DefaultHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using NewLife.Log;

namespace NewLife.Agent;

/// <summary>服务主机。用于管理控制服务</summary>
public class DefaultHost : DisposeBase, IHost
{
/// <summary>
/// 主服务
/// </summary>
public ServiceBase Service { get; set; }

/// <summary>
/// 是否以服务形式运行
/// </summary>
public Boolean InService { get; set; }

/// <summary>服务是否已安装</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean IsInstalled(String serviceName) => false;

/// <summary>服务是否已启动</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean IsRunning(String serviceName) => false;

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="binPath">文件路径</param>
/// <param name="description">描述信息</param>
/// <returns></returns>
public virtual Boolean Install(String serviceName, String displayName, String binPath, String description) => false;

/// <summary>卸载服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Remove(String serviceName) => false;

/// <summary>启动服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Start(String serviceName) => false;

/// <summary>停止服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Stop(String serviceName) => false;

/// <summary>重启服务</summary>
/// <param name="serviceName">服务名</param>
public virtual Boolean Restart(String serviceName) => false;

/// <summary>开始执行服务</summary>
/// <param name="service"></param>
public virtual void Run(ServiceBase service)
{
if (service == null) throw new ArgumentNullException(nameof(service));

// 以服务运行
InService = true;

try
{
// 启动初始化
service.StartLoop();

// 阻塞
service.DoLoop();

// 停止
service.StopLoop();
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
}

/// <summary>查询服务配置</summary>
/// <param name="serviceName">服务名</param>
public virtual ServiceConfig QueryConfig(String serviceName) => null;
}
67 changes: 0 additions & 67 deletions NewLife.Agent/IHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,71 +55,4 @@ public interface IHost
/// <summary>查询服务配置</summary>
/// <param name="serviceName">服务名</param>
ServiceConfig QueryConfig(String serviceName);
}

/// <summary>服务主机。用于管理控制服务</summary>
public abstract class Host : DisposeBase, IHost
{
/// <summary>
/// 主服务
/// </summary>
public ServiceBase Service { get; set; }

/// <summary>
/// 是否以服务形式运行
/// </summary>
public Boolean InService { get; set; }

/// <summary>服务是否已安装</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean IsInstalled(String serviceName) => false;

/// <summary>服务是否已启动</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean IsRunning(String serviceName) => false;

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="binPath">文件路径</param>
/// <param name="description">描述信息</param>
/// <returns></returns>
public virtual Boolean Install(String serviceName, String displayName, String binPath, String description) => false;

/// <summary>卸载服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Remove(String serviceName) => false;

/// <summary>启动服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Start(String serviceName) => false;

/// <summary>停止服务</summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public virtual Boolean Stop(String serviceName) => false;

/// <summary>重启服务</summary>
/// <param name="serviceName">服务名</param>
public virtual Boolean Restart(String serviceName) => false;

///// <summary>安装为自启动。登录进入桌面后启动</summary>
///// <param name="serviceName">服务名</param>
///// <param name="displayName">显示名</param>
///// <param name="binPath">文件路径</param>
///// <param name="description">描述信息</param>
///// <returns></returns>
//public virtual Boolean InstallAutorun(String serviceName, String displayName, String binPath, String description) => false;

/// <summary>开始执行服务</summary>
/// <param name="service"></param>
public abstract void Run(ServiceBase service);

/// <summary>查询服务配置</summary>
/// <param name="serviceName">服务名</param>
public virtual ServiceConfig QueryConfig(String serviceName) => null;
}
Loading

0 comments on commit 52e1f0e

Please sign in to comment.