#1 2012-02-28 09:06:40

corchi72
Member
Registered: 2010-12-10
Posts: 232

How can I convert my server console application in a service?

I have developed a program using the example "Synopse OpenSource \ SQLite3 \ Samples \ 04 - HTTP Client-Server" creating a server-type Console Application. Now I would like to turn the server on a service, what should I write? I ask this because I have created a "Service appliaction" with delphi and then I added the connection to the server SQLite but it don't work

Offline

#2 2012-02-28 09:19:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: How can I convert my server console application in a service?

How do you make the connection?
This is very difficult to find what's wrong with your code with so few information.

mORMot is used on production with the server installed as a service, without any problem.

Take a look at the "10 - Background Http service" demo, which is just a HTTP server implemented as a Windows service, using our dedicated SQLite3Service unit, which is more lightweight than the default Delphi service implementation.
I forgot to commit this sample file some weeks ago - it is now available at http://synopse.info/fossil/finfo?name=S … ervice.dpr
Sorry!

Offline

#3 2012-02-28 16:34:08

corchi72
Member
Registered: 2010-12-10
Posts: 232

Re: How can I convert my server console application in a service?

I sorry, but your example number 10  not working, it not generated any sqlite files and even the log file

Offline

#4 2012-02-28 18:42:14

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: How can I convert my server console application in a service?

Also for me the demo 10 doesn't work. Tested on Windows XP.

Offline

#5 2012-02-28 18:48:20

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

I didn't look at "10 - Background Http service" example, but maybe is there some issue which is explained here.

I'm having same source code at "3" different application implementations (different compiler defines) and works just fine (at least for named pipes at mORMot level):
1. For simple testing I implemented first as all in one GUI exe.
2. Then I separated on client\server part, client as GUI exe, server as console app.
3. At last I implemented server part as windows service too.


My favorite approach is not via sub-classing, but as composition (TService is just member).
So in short you need somewhere next basic things (approach is similar to the SvcMgr.TService):

winService: TService;
(...)
procedure ServiceControlHandler(CtrlCode: DWord); stdcall;
begin
  winService.DoCtrlHandle(CtrlCode);
end;
(...)
winService := TService.Create(SERVICE_NAME, SERVICE_DESC);
winService.ControlHandler := ServiceControlHandler;
winService.OnStart := StartHandler; //* In this handler you implement mORMot startup
winService.OnStop := StopHandler;  //* ...here the mORMot shutdown
winService.OnResume := StartHandler;
winService.OnPause := StopHandler;
ServicesRun; //* this is blocking function, here you put control to the windows service manager
(...)

Last edited by Leander007 (2012-02-28 18:56:15)


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

#6 2012-02-28 19:30:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: How can I convert my server console application in a service?

Yes I think that the latest modifications made to the SQLite3Service.pas file did break the implementation of this unit.

See http://synopse.info/fossil/finfo?name=S … ervice.pas

But in all cases, there is no problem running mORMot as a service, even if this unit is not 100% working.

I do not have time to fix it yet - so you can either use the regular Delphi service implementation (which is bigger, but more standard), or use a previous revision of the SQLite3Service.pas unit.

If you find the issue, let us post the fix here, so that we may commit a working file.

Offline

#7 2012-02-29 00:12:58

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

Your TService does work in all cases if control handler is explicitly assigned (not via "assembler" trick, this fails) as is already stated by my previous post:

winService.ControlHandler := ServiceControlHandler;

In my example has been used SQLite3Service.TService not SvcMgr.TService.

Last edited by Leander007 (2012-02-29 00:18:49)


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

#8 2012-02-29 11:21:36

lestat
Member
From: Italy
Registered: 2012-02-20
Posts: 11

Re: How can I convert my server console application in a service?

How is possible to patch the code to run the example?

Offline

#9 2012-02-29 14:12:10

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

I just checked example. HERE IS THE SOLUTION!
There are two issues, one is minor related to the automatic assigning of handler (I didn't investigate further, because "basic pascal" works always as I told already in previous posts) and the other to "not assigned global constants in class creation" (I think compiler stuff, Arnaud will know about this better smile ).

So, Arnaud if you can include this corrections:
1. Problem: Constants should be moved to the class (it is better looking anyway smile, more object approach smile):

TSQLite3HttpService = class(TService)
  public
    const
    SERVICENAME = 'mORMotHttpServerService';
    SERVICEDISPLAYNAME = 'mORMot Http Server Service';
  var
(...)

if param='/install' then
          TServiceController.CreateNewService('','',TSQLite3HttpService.SERVICENAME,
              TSQLite3HttpService.SERVICEDISPLAYNAME, paramstr(0),'','','','',

2. Problem:
then next part of code, should be included\changed. Asm magic does not properly stop service, but startup and communication was ok with it:

procedure ServiceControlHandler(CtrlCode: DWord); stdcall;
begin
  Service.DoCtrlHandle(CtrlCode);
end;

{ TSQLite3HttpService }

constructor TSQLite3HttpService.Create;
begin
  inherited Create(SERVICENAME,SERVICEDISPLAYNAME);
  ControlHandler := ServiceControlHandler;
(...)

Last edited by Leander007 (2012-02-29 14:13:49)


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

#10 2012-02-29 14:19:24

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

This was tested with current code as basis and compiled with Delphi 2009, running on Vista smile.

Last edited by Leander007 (2012-02-29 14:20:36)


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

#11 2012-04-10 13:17:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: How can I convert my server console application in a service?

Offline

#12 2012-04-10 16:21:34

BrentG
Member
Registered: 2012-04-05
Posts: 31

Re: How can I convert my server console application in a service?

I hate to burst everyone's bubble, but the problem still persists, at least with D2007 or XE2, even with the latest download http://synopse.info/fossil/info/215e44e66b.

I have tested it on Win7 (32 bit or 64-bit) and XP SP3 in a VirtualBox as Administrator.
I can install it, but it won't start. If I try and start it manually in the Services window it I get "Error 1053: The service did not respond to the start or control request in a timely fashion.".

I checked the latest Synopse source code (downloaded today) and Leander007's changes are in there. If someone wants to give me a link to their "httpservice.exe" I can run it here to see if the problem is Windows or my installation of mORMot.

Like I said, I just downloaded "Synopse OpenSource-215e44e66b1226ae.zip" and installed it an hour ago.

Brent

Offline

#13 2012-04-10 16:42:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: How can I convert my server console application in a service?

Did you try to start it with HttpService /start on command line?

Is there no log file created?

Offline

#14 2012-04-10 16:53:31

BrentG
Member
Registered: 2012-04-05
Posts: 31

Re: How can I convert my server console application in a service?

Yes. I opened a DOS window as Administrator and typed:

httpservice /install
httpservice /start

The /install worked (appears in Services window), but the /start is ignored and no log file produced. I assume the log file would be produced in same directory as the .exe. The /uninstall also does NOT work. I had to execute a "SC Delete mORMotHttpServerService" to uninstall it from the Services window before I try again.

Brent

Win 7 32-bit (default OS)
Delphi XE2 32-bit (default, I have all Delphi versions installed in XP Virtual Box)

Offline

#15 2012-04-10 21:21:15

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

Arnaud missed the first problem with constants (is not yet corrected in source, all my changes are not there), so if you made corrections as I suggested (http://synopse.info/forum/viewtopic.php?pid=3683#p3683) then it must worked .
Esmondb obviously did follow both suggestions (I'm using this as daily practice, so it is WORKING).
I'm repeating myself to often smile.

Last edited by Leander007 (2012-04-10 21:25:32)


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

#16 2012-04-10 22:15:11

BrentG
Member
Registered: 2012-04-05
Posts: 31

Re: How can I convert my server console application in a service?

Leander007,
     When I took a look at the code changes in today's download, I realized Arnaud missed including the constants in the TSQLite3HttpService class, but I thought it was all aesthetics as to where the constants were declared. My Bad! smile


Leander007 wrote:

I'm repeating myself to often smile.

Yeah, I'm getting a little dense in my old age. It's working now. Thanks for the help.

Brent

Offline

#17 2012-04-11 07:30:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: How can I convert my server console application in a service?

Using const within class is not compatible with all versions of Delphi handled by the framework.
That is the reason why it was not included in the trunk.

In fact, the error was that there was already a property of TService named SERVICENAME (ServiceName).

I renamed those, and it worked as expected:

const
  HTTPSERVICENAME = 'mORMotHttpServerService';
  HTTPSERVICEDISPLAYNAME = 'mORMot Http Server Service';

In fact, the trick of ServiceControlHandler() with a global was not even necessary.

See http://synopse.info/fossil/info/13be8e5a4c

Offline

#18 2012-04-11 07:37:12

Leander007
Member
From: Slovenia
Registered: 2011-04-29
Posts: 113

Re: How can I convert my server console application in a service?

See answer .


"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal

Offline

Board footer

Powered by FluxBB