Recent

Author Topic: TDaemon and FB Events not work  (Read 1636 times)

tomek1

  • New Member
  • *
  • Posts: 16
TDaemon and FB Events not work
« on: May 18, 2018, 12:45:58 pm »
Hi
I've problem with simple daemon app created using Lazarus wizard. It ignores FB Events from firebird. Same code in Console App and Forms App works OK, problem is only in daemon app.
Lazarus 1.9.0. FPC 3.1.1. Win 10. I've tried both TFBEventMonitor and TIBEventAlerter from Zeos.
Below the daemon code. Project's lpr and daemonmapper unit default from wizard.
Code: Pascal  [Select][+][-]
  1. unit DaemonUnit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, DaemonApp, IBConnection, FBEventMonitor;
  9.  
  10. type
  11.  
  12.   { TDaemon2 }
  13.  
  14.   TDaemon2 = class(TDaemon)
  15.     zEvent: TFBEventMonitor;
  16.     zConn: TIBConnection;
  17.     procedure DataModuleCreate(Sender: TObject);
  18.     procedure DataModuleDestroy(Sender: TObject);
  19.     procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  20.     procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  21.     procedure zEventEventAlert(Sender: TObject; EventName: string;
  22.       EventCount: longint; var CancelAlerts: boolean);
  23.   private
  24.     sLogFileName: string;
  25.     procedure Log(aLog: string);
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Daemon2: TDaemon2;
  32.  
  33. implementation
  34.  
  35. procedure RegisterDaemon;
  36. begin
  37.   RegisterDaemonClass(TDaemon2)
  38. end;
  39.  
  40. {$R *.lfm}
  41.  
  42.  
  43. procedure TDaemon2.Log(aLog: string);
  44. var
  45.  sTemp: TStringList;
  46.  F: TextFile;
  47. begin
  48.   sTemp := TStringList.Create;
  49.   try
  50.     if FileExists(sLogFileName) then
  51.       sTemp.LoadFromFile(sLogFileName);
  52.     sTemp.Add(DateTimeToStr(Now()) + '  ' + aLog);
  53.     sTemp.SaveToFile(sLogFileName);
  54.   finally
  55.     sTemp.Free
  56.   end;
  57. end;
  58.  
  59. procedure TDaemon2.DataModuleCreate(Sender: TObject);
  60. begin
  61.   sLogFileName := ExtractFilePath(Application.ExeName) + 'log.txt';
  62.   Log('ServiceCreate');
  63. end;
  64.  
  65. procedure TDaemon2.DataModuleDestroy(Sender: TObject);
  66. begin
  67.   Log('ServiceDestroy');
  68. end;
  69.  
  70. procedure TDaemon2.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  71. begin
  72.   Log('ServiceStart');
  73.   try
  74.     zConn.HostName := '192.168.0.12';
  75.     zConn.DatabaseName := 'D:\DB\db.fdb';
  76.     zConn.UserName := 'SYSDBA';
  77.     zConn.Password := '****';
  78.     zConn.Connected := True;
  79.  
  80.     zEvent.Connection := zConn;
  81.     zEvent.Events.Add('test_event');
  82.     zEvent.Registered := True;
  83.     if zEvent.Registered then
  84.       Log('registered ok')
  85.     else
  86.       Log('not registered');
  87.   except on E: exception do
  88.     Log(E.Message);
  89.   end;
  90.  
  91. end;
  92.  
  93. procedure TDaemon2.DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  94. begin
  95.   try
  96.     zEvent.Registered := False; //zeos is hanging there
  97.     zConn.Connected := False;
  98.   except on E: exception do
  99.     Log(E.Message);
  100.   end;
  101.   Log('ServiceStop');
  102. end;
  103.  
  104. procedure TDaemon2.zEventEventAlert(Sender: TObject; EventName: string;
  105.   EventCount: longint; var CancelAlerts: boolean);
  106. begin
  107.   Log('IB_EVENT: ' + EventName);
  108. end;
  109.  
  110. initialization
  111.   RegisterDaemon;
  112. end.

d.ioannidis

  • Full Member
  • ***
  • Posts: 221
    • Nephelae
Re: TDaemon and FB Events not work
« Reply #1 on: May 18, 2018, 01:14:11 pm »
Hi,

  AFAIR, ( it's been a while to write a deamon ) you should create a thread and start it on module start.  Put in that thread the TIBConnection and TFBEventMonitor logic .

  In my experiense daemon Start and Stop it's better only to start ( create ), stop ( free/destroy ) a separate thread and exit as soon is possible.

regards,

tomek1

  • New Member
  • *
  • Posts: 16
Re: TDaemon and FB Events not work
« Reply #2 on: May 21, 2018, 11:44:31 am »
Hi
Thanks for answer.
I did as you suggested, but it's still not working. I'm not sure if thread code is correct however:
Code: Pascal  [Select][+][-]
  1. unit DaemonUnit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, DaemonApp, IBConnection, FBEventMonitor, FBAdmin;
  9.  
  10. type
  11.   { TEventsThread }
  12.  
  13.   TEventsThread = class(TThread)
  14.   private
  15.     fEvent: TFBEventMonitor;
  16.     fConn: TIBConnection;
  17.     sLogFileName: string;
  18.     procedure Log(aLog: string);
  19.     procedure MyEventAlert(Sender: TObject; EventName: string;
  20.       EventCount: longint; var CancelAlerts: boolean);
  21.   public
  22.     constructor Create;
  23.     destructor Destroy; override;
  24.     procedure Execute; override;
  25.   end;
  26.  
  27.  
  28.   { TDaemon3 }
  29.  
  30.   TDaemon3 = class(TDaemon)
  31.     procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  32.     procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  33.   private
  34.     MyThread: TEventsThread;
  35.   public
  36.  
  37.   end;
  38.  
  39. var
  40.   Daemon3: TDaemon3;
  41.  
  42. implementation
  43.  
  44. procedure RegisterDaemon;
  45. begin
  46.   RegisterDaemonClass(TDaemon3)
  47. end;
  48.  
  49. {$R *.lfm}
  50.  
  51. { TEventsThread }
  52.  
  53. constructor TEventsThread.Create;
  54. begin
  55.   inherited Create(True);
  56.   FreeOnTerminate := False;
  57.   fEvent := TFBEventMonitor.Create(nil);
  58.   fConn  := TIBConnection.Create(nil);
  59.   sLogFileName := ExtractFilePath(Application.ExeName) + 'log.txt';
  60.   Log('thread create');
  61.   Start;
  62. end;
  63.  
  64. destructor TEventsThread.Destroy;
  65. begin
  66.   Log('thread destroy');
  67.   fEvent.Free;
  68.   fConn.Free;
  69.   inherited Destroy;
  70. end;
  71.  
  72. procedure TEventsThread.Execute;
  73. begin
  74.   Log('thread execute');
  75.   try
  76.     fConn.HostName     := '192.168.0.12';
  77.     fConn.DatabaseName := 'D:\DB\db.fdb';
  78.     fConn.UserName     := 'SYSDBA';
  79.     fConn.Password     := '****';
  80.     fConn.Connected    := True;
  81.  
  82.     fEvent.Connection   := fConn;
  83.     fEvent.OnEventAlert := @MyEventAlert;
  84.     fEvent.Events.Add('test_event');
  85.     fEvent.Registered := True;
  86.     if fEvent.Registered then
  87.       Log('registered ok (thread)')
  88.     else
  89.       Log('not registered (thread)');
  90.   except on E: exception do
  91.     Log(E.Message);
  92.   end;
  93.   while not Terminated do begin
  94.     Sleep(100);
  95.     CheckSynchronize();
  96.   end;
  97. end;
  98.  
  99. procedure TEventsThread.MyEventAlert(Sender: TObject; EventName: string;
  100.   EventCount: longint; var CancelAlerts: boolean);
  101. begin
  102.   Log('IB_EVENT: ' + EventName);
  103. end;
  104.  
  105. procedure TEventsThread.Log(aLog: string);
  106. var
  107.  sTemp: TStringList;
  108.  F: TextFile;
  109. begin
  110.   sTemp := TStringList.Create;
  111.   try
  112.     if FileExists(sLogFileName) then
  113.       sTemp.LoadFromFile(sLogFileName);
  114.     sTemp.Add(DateTimeToStr(Now()) + '  ' + aLog);
  115.     sTemp.SaveToFile(sLogFileName);
  116.   finally
  117.     sTemp.Free
  118.   end;
  119. end;
  120.  
  121.  
  122. procedure TDaemon3.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  123. begin
  124.   MyThread := TEventsThread.Create;
  125. end;
  126.  
  127. procedure TDaemon3.DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  128. begin
  129.   MyThread.Terminate;
  130. end;
  131.  
  132. initialization
  133.   RegisterDaemon;
  134. end.
  135.  
Regards, Tomek

 

TinyPortal © 2005-2018