Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit service_process;
- interface
- uses SysUtils;
- type
- TQueryResult = record
- success : Boolean;
- message : String;
- end;
- procedure DoRoutine;
- var
- error: boolean;
- const
- SERVICE_NAME = 'MK_Kulgram';
- implementation
- uses ZAbstractConnection, ZConnection, Classes, DB, StrUtils, ZDataset,
- idHTTPServer, WinSock, uMyConfigFile, IdContext, idCustomHTTPServer,
- uHelper;
- var
- con1 : TZConnection;
- qry1 : TZQuery;
- flog : TStrings;
- AppF : String;
- conf : TINIFile;
- http : TidHTTPServer;
- const
- Ver = '2.0.4';
- procedure Println(AText: string);
- begin
- if (ParamStr(1) = '/I') or (ParamStr(1) = '/U') then
- Writeln(AText);
- end;
- function ThisTime: string;
- var
- thn, bln, tgl, jam, men, det, mil: Word;
- begin
- DecodeTime(Now, jam, men, det, mil);
- DecodeDate(Now, thn, bln, tgl);
- result := format('%.4d-%.2d-%.2d %.2d:%.2d:%.2d', [thn, bln, tgl,
- jam, men, det]);
- end;
- function FileName: string;
- var
- thn, bln, tgl: Word;
- begin
- DecodeDate(Now, thn, bln, tgl);
- result := format('%.4d-%.2d-%.2d.txt', [thn, bln, tgl]);
- end;
- procedure SaveLog;
- begin
- flog.SaveToFile(AppF + 'log/' + FileName);
- end;
- procedure WriteLog(teks: String);
- begin
- flog.Add(format('[%s] - %s', [ThisTime, teks]));
- SaveLog;
- end;
- function isSelect(SQL: String): boolean;
- begin
- SQL := trim(LowerCase(SQL));
- result := Copy(SQL, 1, 6) = 'select';
- end;
- function RemoveDoubleQuote(Text: string): string;
- begin
- result := StringReplace(Text, '"', '', [rfReplaceAll]);
- end;
- function ParseSQL(ASQL: String): String;
- begin
- ASQL := StringReplace(ASQL, '"NULL"', 'NULL', [rfReplaceAll, rfIgnoreCase]);
- result := ASQL;
- end;
- function ExecQuery(qry: TZQuery): TQueryResult;
- var
- str: string;
- begin
- str := ParseSQL(qry.SQL.Text);
- qry.SQL.Text := str;
- try
- if isSelect(qry.SQL.Text) then
- qry.Open else
- qry.ExecSQL;
- result.success := true;
- result.message := '';
- except
- on E:Exception do
- begin
- WriteLog(E.Message + #13#10#13#10#13#10 + qry.SQL.Text);
- result.success := false;
- result.message := E.Message;
- end;
- on E:EDatabaseError do
- begin
- WriteLog(E.Message + #13#10#13#10#13#10 + qry.SQL.Text);
- result.success := false;
- result.message := E.Message;
- end;
- end;
- end;
- function MySQLDate(ADate: TDate): String;
- begin
- result := FormatDateTime('YYYY-mm-dd', ADate);
- result := ifthen(result = '1899-12-30', 'NULL', result);
- end;
- procedure ReConnectDatabase;
- begin
- con1.Disconnect;
- try
- con1.Connect;
- except
- on E:Exception do
- begin
- WriteLog('Database GL gagal terkoneksi');
- WriteLog(E.Message);
- WriteLog(format('host:%s;port:%d;user:%s;pass:%s;dbname:%s',
- [con1.HostName, con1.Port, con1.User, con1.Password,
- con1.Database]));
- Error := true;
- end;
- on E:EDatabaseError do
- begin
- WriteLog('Database GL gagal terkoneksi');
- WriteLog(E.Message);
- WriteLog(format('host:%s;port:%d;user:%s;pass:%s;dbname:%s',
- [con1.HostName, con1.Port, con1.User, con1.Password,
- con1.Database]));
- Error := true;
- end;
- end;
- end;
- procedure CekDatabaseConnection;
- begin
- con1.HostName := conf.ReadString ('database', 'host', 'localhost');
- con1.Port := conf.ReadInteger('database', 'port', 3306);
- con1.User := conf.ReadString ('database', 'user', 'root');
- con1.Password := conf.ReadString ('database', 'password', '');
- con1.Database := conf.ReadString ('database', 'database', 'webserver');
- con1.Protocol := 'mysql';
- con1.LibraryLocation := AppF + 'libmysql.dll';
- PrintLn(SERVICE_NAME + ' : Connecting to Database.....');
- try
- con1.Connect;
- WriteLog('Berhasil konek ke database');
- PrintLn(SERVICE_NAME + ' : Database is Connected.....');
- except
- on E:Exception do
- begin
- PrintLn(SERVICE_NAME + ' : Database is failure Connection.....');
- WriteLog('Gagal konek ke database');
- WriteLog(E.Message);
- WriteLog(format('host:%s;port:%d;user:%s;pass:%s;dbname:%s',
- [con1.HostName , con1.Port, con1.User, '[hidden]',
- con1.Database]));
- Error := true;
- end;
- on E:EDatabaseError do
- begin
- PrintLn(SERVICE_NAME + ' : Database is failure Connection.....');
- WriteLog('Gagal konek ke database');
- WriteLog(E.Message);
- WriteLog(format('host:%s;port:%d;user:%s;pass:%s;dbname:%s',
- [con1.HostName , con1.Port, con1.User, '[hidden]',
- con1.Database]));
- Error := true;
- end;
- end;
- end;
- procedure LoadLog;
- begin
- if FileExists(AppF + 'log/' + FileName) then
- flog.LoadFromFile(AppF + 'log/' + FileName);
- end;
- procedure DoRoutine;
- var
- Jam, Min, Det, per : Word;
- begin
- DecodeTime(time, Jam, Min, Det, per);
- if (jam = 0) and (min = 0) and (det = 0) then
- flog.Clear;
- //reconect tiap 2 jam;
- if (jam mod 2 = 0) and (min = 0) and (det = 0) then
- ReconnectDatabase;
- end;
- initialization
- AppF := ExtractFilePath(paramstr(0));
- con1 := TZConnection.Create(nil);
- qry1 := TZQuery.Create(nil);
- conf := TINIFile.Create(AppF + 'kulgram.kppdi');
- http := TidHTTPServer.Create(nil);
- fLog := TStringList.Create;
- LoadLog;
- http.DefaultPort := conf.ReadInteger('webserver', 'port', 8080);
- try
- http.Active := true;
- WriteLog(format('Webserver telah aktif pada Port %d',
- [http.DefaultPort]));
- except
- on E:Exception do
- begin
- WriteLog('Webserver tidak dapat diaktifkan. ' + E.Message);
- end;
- end;
- http.OnCommandGet := Helper.CommandGet;
- qry1.Connection := con1;
- if not DirectoryExists(AppF + 'log') then
- ForceDirectories(AppF + 'log');
- WriteLog('Service Started (v' + Ver + ')..');
- if ParamStr(1) <> '/U' then
- CekDatabaseConnection;
- finalization
- WriteLog('Service Stopped (v' + Ver + ')..');
- http.Bindings.Clear;
- SaveLog;
- fLog.Free;
- con1.Free;
- qry1.Free;
- conf.Free;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement