Hello rvk,
thank you for for pointing missing 'end' - I've deleted some procedures/functions I don't use from main code to post it here.
I'm posting 'baba' event after connection|insert|update Firebird database/table. In GUI version it works without any problems, database, table, triggers are the same.
Yes, as far as- I know, the connection should be 're-estabilished' after some period of time, usually few hours, in some cases (router issues) it can be only be few minutes.
Updated code (compiling, activity written to log, events not working):
Program zeStacka;
{$mode objfpc}{$H+}
uses
{$DEFINE UseCThreads}
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
SysUtils, BaseUnix, sqldb, IBConnection, FBEventMonitor;
{ TMyEventAlert } { c }
type
TMyEventAlert = class
class procedure OnFBEvent(Sender: TObject; EventName: string; EventCount: longint;
var CancelAlerts: boolean);
end;
var
{ vars for daemonizing }
bHup,
bTerm: boolean;
textPolaczenia, textZdarzenia, config: text;
SlogPolaczenia, SlogZdarzenia, nazwaHosta, sciezkaBazaDanych, uzytkownik, haslo: string;
aOld,
aTerm,
aHup: pSigActionRec;
ps1: psigset;
sSet: cardinal;
pid: pid_t;
secs: longint;
zerosigs: sigset_t;
EventAlert: TMyEventAlert;
EventsM: TFBEventMonitor;
BConnection: TIBConnection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
{ handle SIGHUP & SIGTERM }
procedure DoSig(sig: longint); cdecl;
begin
case sig of
SIGHUP: bHup := true;
SIGTERM: bTerm := true;
end;
end;
class procedure TMyEventAlert.OnFBEvent(Sender: TObject; EventName: string;
EventCount: longint; var CancelAlerts: boolean);
begin
// some basic do's
SlogZdarzenia := 'SlogZdarzenia.log';
AssignFile(textZdarzenia, SlogZdarzenia);
Rewrite(textZdarzenia);
Writeln(textZdarzenia, 'Cos sie zdarzylo');
CloseFile(textZdarzenia);
end;
procedure WpisPolaczenie;
begin
SlogPolaczenia := 'SlogPolaczenia.log';
AssignFile(textPolaczenia, SlogPolaczenia);
Append(textPolaczenia);
Writeln(textPolaczenia, 'Connected to database at ', formatdatetime('hh:nn:ss', now));
CloseFile(textPolaczenia);
end;
procedure CreateConnection;
begin
BConnection := TIBConnection.Create(nil);
BConnection.DataBaseName := '/home/pi/bazydanych/aaa';
BConnection.Hostname := 'localhost';
BConnection.UserName := 'sysdba';
BConnection.Password := 'masterkey';
EventsM := TFBEventMonitor.Create(nil);
EventsM.Connection := BConnection;
EventsM.Events.Add('baba');
EventsM.OnEventAlert := @EventAlert.OnFBEvent;
EventsM.RegisterEvents;
end;
begin
secs := 15;
fpsigemptyset(zerosigs);
{ set global daemon booleans }
bHup := true; { to open log file }
bTerm := false;
{ block all signals except -HUP & -TERM }
sSet := $FFFFBFFE;
ps1 := @sSet;
fpsigprocmask(sig_block, ps1, nil);
{ setup the signal handlers }
new(aOld);
new(aHup);
new(aTerm);
aTerm^.sa_handler { .sh } := SigactionHandler(@DoSig);
aTerm^.sa_mask := zerosigs;
aTerm^.sa_flags := 0;
{$IFNDEF BSD} { Linux'ism }
aTerm^.sa_restorer := nil;
{$ENDIF}
aHup^.sa_handler := SigactionHandler(@DoSig);
aHup^.sa_mask := zerosigs;
aHup^.sa_flags := 0;
{$IFNDEF BSD} { Linux'ism }
aHup^.sa_restorer := nil;
{$ENDIF}
fpSigAction(SIGTERM, aTerm, aOld);
fpSigAction(SIGHUP, aHup, aOld);
{ daemonize }
pid := fpFork;
case pid of
0:
begin { we are in the child }
Close(input); { close standard in }
Close(output); { close standard out }
Assign(output, '/dev/null');
Rewrite(output);
Close(stderr); { close standard error }
Assign(stderr, '/dev/null');
Rewrite(stderr);
end;
-1: secs := 0; { forking error, so run as non-daemon }
Else Halt; { successful fork, so parent dies }
end;
{ begin processing loop }
Repeat
If bHup Then
begin
{$I-}
Close(textPolaczenia);
{$I+}
IOResult;
{$I+}
// UtworzLogi;
{ fggggggd }
bHup := false;
end;
{ ---------------------- }
{ 'program' part of a daemon }
CreateConnection;
WpisPolaczenie;
{ ---------------------- }
If bTerm Then
BREAK
Else
{ wait a while }
fpSelect(0, nil, nil, nil, secs * 1000);
Until bTerm;
end.
unfortunately, program can't make logFiles (SlogPolaczenie.log and SlogEvents.log), I have to create them manually in a project folder.
tail -f SlogPolaczenie.log // activity log
tail -f SlogEvents.log // events log
ps ax | grep nameofaprogram (here: zeStacka)
kill -TERM processIDListedafterPsAXGrepNameoOfAprogram