Recent

Author Topic: TDaemon Wiki Demo : start event not fired  (Read 2391 times)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 323
TDaemon Wiki Demo : start event not fired
« on: February 02, 2023, 02:05:51 pm »
Hi,
I'm experimenting with linux daemons and could not get start event to fire in demo application.
https://wiki.freepascal.org/Daemons_and_Services
https://github.com/arminlinder/Lazarus-FPC-Wiki-Code-Samples/zipball/master/

I modified the code below and now it works, so looks like the demo needs a small fix to the Format function call (seems it wants 2 parameters string and number )?

See DaemonUnit1.pas
Code: Pascal  [Select][+][-]
  1. procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  2. begin
  3.   LogToFile('STARTED');
  4.   {-m.c.
  5.   LogToFile(Format('Daemon %s received start signal, PID:%d', [GetProcessID]));
  6.   // Create a suspended worker thread - see DaemonWorkerThread unit
  7.   FDaemonWorkerThread := TDaemonWorkerThread.Create;
  8.   // Parametrize it
  9.   FDaemonWorkerThread.FreeOnTerminate := False;
  10.   // Start the worker
  11.   FDaemonWorkerThread.Start;
  12.   }
  13.   OK := True;
  14. end;  

Log:
Code: Text  [Select][+][-]
  1. 12:54:43 Log created
  2. 12:54:43 Daemon received stop signal
  3. 12:54:43 Daemon stopped
  4. 12:55:08 Daemon installing
  5. 12:55:08 Daemon installed
  6. 12:55:39 STARTED

TRon

  • Hero Member
  • *****
  • Posts: 3631
Re: TDaemon Wiki Demo : start event not fired
« Reply #1 on: February 02, 2023, 02:13:51 pm »
https://wiki.freepascal.org/Daemons_and_Services

I modified the code below and now it works, so looks like the demo needs a small fix to the Format function call (seems it wants 2 parameters string and number )?
copy-paste error ?

this is what I read/see:
Code: Pascal  [Select][+][-]
  1. procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  2. begin
  3.   LogToFile(Format('Daemon received start signal, PID:%d', [GetProcessID]));
  4.   // Create a suspended worker thread - see DaemonWorkerThread unit
  5.   FDaemonWorkerThread := TDaemonWorkerThread.Create;
  6.   // Parametrize it
  7.   FDaemonWorkerThread.FreeOnTerminate := False;
  8.   // Start the worker
  9.   FDaemonWorkerThread.Start;
  10.   OK := True;
  11. end;
  12.  
Thus the format only has one used format parameter (did you added the %s yourself, perhaps copied from the second example that does use two format parameter strings ?)  :)

But yes, that call to format as you have shown will fail because it is missing a string for first parameter (you only supplied one integer with the call to GetProcessID() )
« Last Edit: February 02, 2023, 02:15:57 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 323
Re: TDaemon Wiki Demo : start event not fired
« Reply #2 on: February 02, 2023, 02:31:32 pm »
Ahh I think I know what the problem is , I'm using the code from the zip download that contains the error

https://github.com/arminlinder/Lazarus-FPC-Wiki-Code-Samples/zipball/master/

The code in the wiki is correct, but download demo project is wrong?

TRon

  • Hero Member
  • *****
  • Posts: 3631
Re: TDaemon Wiki Demo : start event not fired
« Reply #3 on: February 02, 2023, 02:35:58 pm »
The code in the wiki is correct, but download demo project is wrong?
Indeed i was copying the wiki entry. Because I did not trusted it I downloaded the zipfile and indeed there the error is present.

You can easily solve that with for example:
Code: [Select]
LogToFile(Format('Daemon %s received start signal, PID:%d', ['the_name_of_your_daemon', GetProcessID]));
or replace it as per wiki entry.

At least it keeps you/us alert  :D
« Last Edit: February 02, 2023, 02:37:45 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 323
Re: TDaemon Wiki Demo : start event not fired
« Reply #4 on: February 02, 2023, 08:15:22 pm »
I added an issue in github
https://github.com/arminlinder/Lazarus-FPC-Wiki-Code-Samples/issues/2

Hopefully the demo will be fixed, it's working great for me..

ArminLinder

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
Re: TDaemon Wiki Demo : start event not fired
« Reply #5 on: May 12, 2023, 09:50:59 am »
Corrected! Thanks very much for your contribution.

Armin.
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

 

TinyPortal © 2005-2018