Search

Sunday, 21 April 2013

Sirius: running as windows service

Sirius server part is delivered as a set of Java archives and this part is initiated via command line interface. It brings some inconveniences while running the tests. Thus, before start using client API we should make sure that the server is up and running. Of course, we can do that manually but:

  1. we should be able to do that automatically
  2. when we test client side we shouldn't touch server side at all
So, it's quite convenient to deliver Sirius Server start/stop process to the system. For Windows it can be done by setting server component as the windows service. Since, Sirius Server is just another Java application it requires some additional efforts for making such task however such task appeared multiple times. Here are the related topics on StackOverflow: So, I'm looking for something similar for Sirius Server. In this post I'll describe the settings needed for making Sirius Java application running as the windows service.

The list of existing solutions

The above links contain the references to multiple products which can be used for the purpose of service wrapper. Here are some of them:

I stopped my choice on the last solution as it seemed to be the most convenient (however it's not the only possible).

Creating executable file

The winrun4j usage appears to be pretty simple and requires the following steps:

  1. Copy WinRun4J.exe to [YourApp].exe
  2. Create [YourApp].ini (in the same directory)
  3. Customize [YourApp].ini (see the table below for information)
  4. Create [YourApp].ico (in the same directory)
  5. Run RCEDIT.exe /I [YourApp].exe [YourApp].ico (this will inject your icon into the executable).
  6. Launch [YourApp].exe
So, I've added winrun4j binaries to the repository, created application icon. After that we can follow the above instructions.

The only thing to be mentioned here is the configuration file. Before I had to run the command like:

java -jar sirius.server.engine-<version>.jar
It should start the jar file from the current directory with default parameters. Generally it should start the main function from the org.sirius.server.Starter class. Since I'm going to do the same using windows service interface I have to reflect this information. Additionally, I have to specify service specific parameters. So, for now the ini file looks like:
working.directory=D:\Work\SiriusDev\Sirius\Publish\Server
main.class=org.sirius.server.Starter
service.id=Sirius
service.name=Sirius Service
service.description=Sirius Test Automation Platform Service.

classpath.1=*.jar
The highlighted items reflect existing run options. Other settings are informative and related to the service.

Once we do the RCEdit.exe call we'll have fully operational executable which will run our Sirius Server application with one click. For now the working directory is hard-coded but for now it's OK. I'll look for parametrizing that later. But for now, we have executable file which will do our server start with one click which is quite good progress.

Registering the service

Now we have executable which should be started as windows hosted application. We could use sc.exe for it but there is one more problem with that. If we want to run some application as the windows service it should provide some specific interface. WinRun4j could make an executable but unfortunately it doesn't fit the requirements for the service. Thus when we try to run the service using sc.exe start we are likely to get the error like:

Error 1053: The service did not respond to the start or control request in a timely fashion.
But there's way out from this problem. We can use srvany utility which is the part of the Windows NT Resource Kit. It acts as the service compatible application which runs the command specified by it's parameters. Thus, we can make a run like:
srvany sirius.exe
and that would be identical to simple sirius.exe call but it can be managed by Windows as an ordinary service application.

General flow to setup srvany service can be found here. We should install this utility to the location we need and perform the following settings:

  1. Register new service by running the following command:
    INSTSRV MyService c:\tools\srvany.exe
    
  2. Run the Registry Editor (REGEDT32.EXE):
  3. under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService: create a 'Parameters' key
  4. under the above key, create an 'Application' value of type REG_SZ and specify there the full path of your app executable (including the extension). For example:
      Application: REG_SZ: D:\TOOLS\VI.EXE
    
  5. OPTIONAL: under the above key, create an 'AppParameters' value of type REG_SZ and specify the parameters for your app. For Example:
     AppParameters: REG_SZ: C:\TMP\FOO
    
  6. OPTIONAL: under the above key, create an 'AppDirectory' value of type REG_SZ and specify the current directory to use for the app, eg:
     AppDirectory: REG_SZ: C:\TMP
    
After that our service is ready.

Automating the entire process using batch

The final step here is to automate the process of the Sirius Server update in case we're making some changes into the server side component. Actually, most of the commands have already been described. So, all we have to do is to consolidate under tha single batch. That what I actually did in the installsvc.cmd file which has the following content:

echo "Creating folder structure"

DEL /Q /S .\Server\*.*

rmdir .\Server

mkdir .\Server

echo "Building server modules"
cd ..
call mvn -f Server/pom.xml -Dpackage.version=%1 -Dversion=%1 -DpomFile=pom.xml -Dpackaging=jar -DgeneratePom=true clean package
call mvn -f Sirius-Server-Core/pom.xml -Dpackage.version=%1 -Dversion=%1 -DpomFile=pom.xml -Dpackaging=jar -DgeneratePom=true clean package
call mvn -f Sirius-Server-Win32/pom.xml -Dpackage.version=%1 -Dversion=%1 -DpomFile=pom.xml -Dpackaging=jar -DgeneratePom=true clean package
call mvn -f Sirius-Server-Web/pom.xml -Dpackage.version=%1 -Dversion=%1 -DpomFile=pom.xml -Dpackaging=jar -DgeneratePom=true clean package
cd .\Publish
FOR /f %%i IN ('dir /B /S ..\sirius.server*%1.jar') DO copy /Y %%i .\Server
copy ..\Server\modules.csv .\Server
copy ..\Server\log4j.xml .\Server
copy ..\Server\log4j.dtd .\Server

echo "Preparing service installation"

copy ..\Utils\winrun4j\winrun4j.exe .\Server\sirius.exe
copy .\sirius.ico .\server\sirius.ico
copy .\sirius.ini .\server\sirius.ini
echo working.directory=%CD%\Server >> .\server\sirius.ini

cd .\Server
..\Utils\winrun4j\RCEDIT.exe /I sirius.exe sirius.ico
..\Utils\winrun4j\RCEDIT.exe /A sirius.exe sirius.ico
cd ..

echo "Registering service"

instsrv Sirius "%ProgramFiles%\Windows Resource Kits\Tools\srvany.exe" -a %SIRIUS_USER% -p %SIRIUS_PASSWORD%
reg add HKLM\SYSTEM\CurrentControlSet\Services\Sirius /v Description /t REG_SZ /d "Sirius Test Automation Platform Server v%1" /f
reg add HKLM\SYSTEM\CurrentControlSet\Services\Sirius\Parameters /f
reg add HKLM\SYSTEM\CurrentControlSet\Services\Sirius\Parameters /v AppDirectory /t REG_SZ /d %CD%\Server /f
reg add HKLM\SYSTEM\CurrentControlSet\Services\Sirius\Parameters /v Application /t REG_SZ /d %CD%\Server\sirius.exe /f

echo "Restarting service"

c:\windows\system32\sc.exe start Sirius
This batch can be good basis for automated Sirius Server restart.

Summary

What was done

With the Sirius Server introduction as the windows service we've done the following:

  • We've created a basis for client side tests which no longer require additional step retrieving and triggering server side components
  • We've created generic way to initiate Sirius Server
  • We don't care about handling Sirius Server availability. That was delegated to the operating system

What's still needed to be done

Yet, there're a lot of things to do with all the stuff we have. Here are just some of the items:

  • We should be able to customize Sirius Server service to load specific modules and listen to specific host and port
  • There're some hard-coded locations which should be parametrized in order to make automated service deployment process fully portable
  • Service deployment should be included into the published binaries to provide service installation option to users
Anyway, we've made another step ahead and all that items to do are just another steps we should make further.

No comments:

Post a Comment