Lazarus

Programming => General => Topic started by: JD on January 28, 2022, 11:15:36 pm

Title: Run application as a service or as a GUI application via command line parameters
Post by: JD on January 28, 2022, 11:15:36 pm
Hi there everyone,

I have an existing GUI application server on a network that handles client requests. I would like to modify this server such that it can be run as a service or as the usual GUI application just by launching it with the appropriate command line parameters. 

This is where I need some assistance because I do not develop console applications.

I would appreciate your kind assistance.

Cheers,

JD
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: trev on January 29, 2022, 12:07:19 am
See the Wiki article:  Daemons and Services (https://wiki.lazarus.freepascal.org/Daemons_and_Services).
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Warfley on January 29, 2022, 04:10:56 am
This is not that easy, at least for Windows, because on Windows GUI applications and Services are structured differently and handled differently by the OS.
On Unix this is a different story, because there is no OS level difference between a GUI application and a Daemon, as a GUI application is just an application that calls drawing routines, while a daemon is just an application that doesnt.

What you could do is to write two programs, a Service and a GUI. The service is always executed and runs in the background, while the GUI just connects to the service and interacts with it (e.g. through IPC)
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Thaddy on January 29, 2022, 07:58:07 am
What you could do is to write two programs, a Service and a GUI. The service is always executed and runs in the background, while the GUI just connects to the service and interacts with it (e.g. through IPC)
Indeed, and that is usually how it should be done (not only on Windows, but also on Linux (rights))
1) a Controlling app with a Userinterface
2) a Daemon/Service that has no userinterface and is optionally controlled by the Controller.

Note that 1. is not strictly necessary if one uses Daemon start-up options and/or config files, depends on purpose.
This goes for both Daemons/Services on Windows and Linux.
And I disagree about this being difficult: it is not. It may be that most programmers do not often write Daemons/Services.
The example in the manual is a good starting point.
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Warfley on January 29, 2022, 11:47:04 am
And I disagree about this being difficult: it is not. It may be that most programmers do not often write Daemons/Services.
The example in the manual is a good starting point.
This wasn't meant to be to the "solution" of two applications, the "this is not that easy" was specifically for the 2 in one solution, because while there might be a solution for Windows, it would be hacky as hell.
Creating two applications isn't that hard, but it can be tideous to implement all the controlling functionality through IPC (as you basically have to define a fully fledged communication interface that covers all usecases, this is much more effort than just accessing the datastructures directly)
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Remy Lebeau on January 31, 2022, 12:41:25 am
This is not that easy, at least for Windows, because on Windows GUI applications and Services are structured differently and handled differently by the OS.

It is not THAT hard, though.  It is just a matter of which API the application uses at runtime.  For instance, when running as a GUI, using a standard TForm.  When running as a service, using a TService (Delphi) or TDaemon (FPC).  In my Delphi VCL service+GUI apps, I use the SvCom framework (https://www.aldyn-software.com/svcom.html) to handle this difference for me, but it is not very hard to do it manually.
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Zvoni on January 31, 2022, 08:36:42 am
Depending on what that application-server is doing:
Don't forget, that a service/daemon can be setup to run without a user-context (e.g. DB-Server which runs, despite no user being logged-in)
a GUI-App requires a logged-in User resp. a Desktop-Session
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Remy Lebeau on January 31, 2022, 06:34:19 pm
Don't forget, that a service/daemon can be setup to run without a user-context (e.g. DB-Server which runs, despite no user being logged-in)
a GUI-App requires a logged-in User resp. a Desktop-Session

On Windows, there is no such thing as a user-less process, or a session-less process.  All processes run under a user account and a session, even services.  They always run under Session 0, and under the LocalSystem (https://docs.microsoft.com/en-us/windows/win32/services/localsystem-account) account by default unless specified differently.
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Zvoni on January 31, 2022, 07:55:28 pm
Remy, agreed.
Wasn‘t precise enough (more in „common“ terms)
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: JD on February 01, 2022, 01:02:02 am
Hi there everyone,

Thanks a lot for your advice and contributions.

Taken inspiration from this post: https://forum.lazarus.freepascal.org/index.php/topic,20274.msg430834.html#msg430834 (https://forum.lazarus.freepascal.org/index.php/topic,20274.msg430834.html#msg430834), I have built a skeletal Service application.

However even without functioning code, I am having memory leaks. And I cannot find the problem.

I attached the published project which was created from the template for the standard Daemon (service) application in the Lazarus IDE.

I would appreciate your help to locate the memory leaks.

Cheers,

JD
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Zvoni on February 01, 2022, 09:34:01 am
Is it just me?
I have never seen the heaptrc-unit in a Uses-clause..... (in OP's case in the lpr-file)
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: JD on February 01, 2022, 10:05:17 am
Is it just me?
I have never seen the heaptrc-unit in a Uses-clause..... (in OP's case in the lpr-file)

It is needed by SetHeapTraceOutput.

JD
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: DonAlfredo on February 01, 2022, 10:16:47 am
You might put heaptrc as first unit in your uses clause ?
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Zvoni on February 01, 2022, 10:23:09 am
It is needed by SetHeapTraceOutput.

JD
??????
Quote
Warning: Do not add the Heaptrc unit manually. The Heaptrc unit needs to be loaded before Lineinfo and only the compiler can do that. Heaptrc is also a memory manager, so do not try to use any memory manager – including Heaptrc itself – in the uses clause, because that may lead to memory corruption and false results. This topic contains a note with example code to handle such a case in a generic way.
And for the TraceOutput: Just do a {$IFDEF UseHeapTrace}SetHeapTraceOutput('Trace.log');{$ENDIF}
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Thaddy on February 01, 2022, 10:31:42 am
You might put heaptrc as first unit in your uses clause ?
What have you been drinking this weekend... Of course not! See the docs and the wiki. NEVER add heaptrc as a unit yourself. And you of all people know that.
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: Zvoni on February 01, 2022, 10:37:01 am
As Thaddy said:
remove HeapTrc from uses-Clause, encase your SetHeapTraceOutput with an {$IFDEF},
either in project-options check "Use HeapTrace" or compile with -gh/-glh
Run
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: JD on February 01, 2022, 12:25:36 pm
As Thaddy said:
remove HeapTrc from uses-Clause, encase your SetHeapTraceOutput with an {$IFDEF},
either in project-options check "Use HeapTrace" or compile with -gh/-glh
Run

Thanks a lot for all your suggestions. The memory leaks in the base service are now gone.

JD
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: JD on February 01, 2022, 12:30:27 pm
You might put heaptrc as first unit in your uses clause ?

I've removed the heaptrc unit completely and followed Zvoni and Thaddy's suggestion.

BTW it is my exisiting mORMot HTTP Server that I'm trying to convert to a service.

If anyone has some code, suggestions that they can share with me, I'll be very grateful.

Cheers,

JD
Title: Re: Run application as a service or as a GUI application via command line parameters
Post by: JD on February 04, 2022, 02:36:30 pm
Hi there everyone,

I've been able to get my service application up and running thanks to your suggestions and contributions.

I completely abandoned the hybrid application and opted for a simple service application.

Thanks a million.  :D

Cheers,

JD
TinyPortal © 2005-2018