Add WelsonJS.Service component

This commit is contained in:
Namhyeon Go 2024-07-22 16:04:40 +09:00
parent 31fb594904
commit 575e0cf6fc
7 changed files with 318 additions and 8 deletions

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
namespace WelsonJS.Service
{
internal static class Program
{
/// <summary>
/// 해당 애플리케이션의 주 진입점입니다.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("WelsonJS.Service")]
[assembly: AssemblyDescription("Service for WelsonJS framework")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Catswords")]
[assembly: AssemblyProduct("WelsonJS")]
[assembly: AssemblyCopyright("Catswords OSS, C-2021-000237, Opensource licensed under GPLv3 or MS-RL")]
[assembly: AssemblyTrademark("WelsonJS")]
[assembly: AssemblyCulture("")]
// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에
// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면
// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요.
[assembly: ComVisible(false)]
// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다.
[assembly: Guid("09f295ee-5edb-4327-abbe-ddccdf5edd9e")]
// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다.
//
// 주 버전
// 부 버전
// 빌드 번호
// 수정 버전
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를
// 기본값으로 할 수 있습니다.
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.7.20")]
[assembly: AssemblyFileVersion("1.2.7.20")]

View File

@ -0,0 +1,37 @@
namespace WelsonJS.Service
{
partial class Service1
{
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
/// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
#endregion
}
}

View File

@ -0,0 +1,128 @@
using System;
using System.ServiceProcess;
using System.Timers;
using MSScriptControl;
using System.IO;
using System.Collections.Generic;
namespace WelsonJS.Service
{
public partial class Service1 : ServiceBase
{
private Timer timer;
private string workingDirectory;
private string scriptFilePath;
private string scriptText;
private ScriptControl scriptControl;
private string appName;
private readonly string logFileName = "ServiceLog.txt";
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// mapping arguments to each variables
var arguments = ParseArguments(args);
foreach (KeyValuePair<string, string> entry in arguments)
{
switch (entry.Key)
{
case "working-directory":
workingDirectory = entry.Value;
break;
case "app-name":
appName = entry.Value;
break;
}
}
// set working directory
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WelsonJS");
Log("Working directory not provided. Using default value.");
}
Directory.SetCurrentDirectory(workingDirectory);
// set script file path
scriptFilePath = Path.Combine(workingDirectory, "app.js");
// try load the script
if (File.Exists(scriptFilePath))
{
scriptText = File.ReadAllText(scriptFilePath);
scriptControl = new ScriptControl
{
Language = "JScript",
AllowUI = false
};
scriptControl.AddCode(scriptText);
}
else
{
Log($"Script file not found: {scriptFilePath}");
}
// initialize
InvokeScriptMethod("initializeService", appName, "start");
// set interval
timer = new Timer();
timer.Interval = 60000; // 1 minute
timer.Elapsed += OnElapsedTime;
timer.Start();
}
protected override void OnStop()
{
InvokeScriptMethod("initializeService", appName, "stop");
timer.Stop();
scriptControl.Reset();
scriptControl = null;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
InvokeScriptMethod("initializeService", appName, "elapsedTime");
}
private string InvokeScriptMethod(string methodName, params object[] parameters)
{
return scriptControl.Run(methodName, parameters).ToString();
}
private void Log(string message)
{
string logFilePath = Path.Combine(workingDirectory, logFileName);
using (StreamWriter writer = new StreamWriter(logFilePath, true))
{
writer.WriteLine($"{DateTime.Now}: {message}");
}
}
private Dictionary<string, string> ParseArguments(string[] args)
{
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string arg in args)
{
if (arg.StartsWith("--"))
{
var index = arg.IndexOf('=');
if (index > 2)
{
var key = arg.Substring(2, index - 2);
var value = arg.Substring(index + 1);
arguments[key] = value;
}
}
}
return arguments;
}
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{09F295EE-5EDB-4327-ABBE-DDCCDF5EDD9E}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>WelsonJS.Service</RootNamespace>
<AssemblyName>WelsonJS.Service</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Service1.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Service1.Designer.cs">
<DependentUpon>Service1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="MSScriptControl">
<Guid>{0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -6,11 +6,11 @@ using System.Runtime.InteropServices;
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("WelsonJS.Toolkit")]
[assembly: AssemblyDescription("Native component for WelsonJS framework")]
[assembly: AssemblyDescription("Toolkit for WelsonJS framework")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Catswords Research")]
[assembly: AssemblyCompany("Catswords")]
[assembly: AssemblyProduct("WelsonJS")]
[assembly: AssemblyCopyright("Catswords Research, C-2021-000237, GPLv3, MS-RL")]
[assembly: AssemblyCopyright("Catswords OSS, C-2021-000237, Opensource licensed under GPLv3 or MS-RL")]
[assembly: AssemblyTrademark("WelsonJS")]
[assembly: AssemblyCulture("")]

34
app.js
View File

@ -554,17 +554,17 @@ function initializeWindow(name, args, w, h) {
}
var app = require(name);
// "set default size of window";
// set default size of window
if (typeof w !== "undefined" && typeof h !== "undefined") {
window.resizeTo(w, h);
}
// "load app";
// load the application
if (app) {
if (app.main) {
var exitStatus = app.main.call(app, args);
if (exitStatus > 0) {
exit(exitStatus);
var status = app.main.call(app, args);
if (status > 0) {
exit(status);
}
} else {
console.error("Missing main entry point in", name + ".js");
@ -576,6 +576,30 @@ function initializeWindow(name, args, w, h) {
}
}
function initializeService(name, eventType) {
var app = require(name);
// load the service
if (app) {
(function(action) {
if (eventType in action) {
try {
action[eventType]();
} catch (e) {
console.error("Exception:", e.message);
}
}
})({
start: app.onServiceStart,
stop: app.onServicsStop,
elapsedTime: app.onServiceElapsedTime
});
} else {
console.error("Could not find", name + ".js");
exit(1);
}
}
// JSON 2
if (typeof JSON === "undefined") {
__evalFile("app/assets/js/json2");