Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit unThreadCrons;
- interface
- uses Classes {$IFDEF MSWINDOWS}, Windows {$ENDIF}, Dialogs, SysUtils, ShellApi,ZAbstractRODataset, ZAbstractDataset, ZDataset,
- ZAbstractConnection, ZConnection, BotLibrary;
- type
- ThreadCrons = class(TThread)
- protected
- private
- FComando : String;
- FLocal : String;
- FMsgKill : String;
- FAdendo : String;
- FPID : Cardinal;
- FDisparador : integer;
- FCron : integer;
- FForcar : boolean;
- Interrupcao : Cardinal;
- ProcessHandle : Cardinal;
- // ExitCode : Cardinal;
- // StartupInfo : TStartupInfo;
- // ProcessInfo : TProcessInformation;
- FCountConcorrencia : Integer;
- Query : TZQuery;
- Conexao : TZConnection;
- Mensagem : String;
- FConcorrencia : Boolean;
- TerminoExterno,FProcessoVivo : boolean;
- FSituacao: integer;
- public
- procedure Execute; override;
- property Cron : integer read FCron write FCron;
- property Comando : string read FComando write FComando;
- property Local : string read FLocal write FLocal;
- property PID : Cardinal read FPID Write FPID;
- property Disparador : integer read FDisparador write FDisparador;
- property Situacao : integer read FSituacao write FSituacao;
- property ProcessoVivo : boolean read FProcessoVivo write FProcessoVivo;
- property Forcar : boolean read FForcar write FForcar;
- property MsgKill : String read FMsgKill write FMsgKill ;
- property Concorrencia : Boolean read FConcorrencia write FConcorrencia;
- property CountConcorrencia : Integer read FCountConcorrencia write FCountConcorrencia;
- constructor Create (VForcar : boolean; VPID : Cardinal; VCron, VDisparador : integer ; VInterrupcao : Cardinal); overload;
- { destructor Destroy; override;}
- procedure ProcessTerminate;
- end;
- implementation
- { ThreadCrons }
- constructor ThreadCrons.Create(VForcar : Boolean; VPID : Cardinal ; VCron, VDisparador : integer ; VInterrupcao : Cardinal);
- begin
- inherited Create(True);
- FreeOnTerminate := False;
- Disparador := VDisparador;
- PID := VPID;
- Interrupcao := VInterrupcao;
- Cron := VCron;
- Forcar := VForcar;
- TerminoExterno := false;
- Concorrencia := false;
- MsgKill := '';
- Situacao := 0;
- CountConcorrencia := 0;
- try
- Conexao := TZConnection.Create(nil);
- Conexao.DataBase := 'WebBot';
- Conexao.HostName := 'webglobaldb1.com.br';
- Conexao.LoginPrompt := false;
- Conexao.Password := 'botcron123';
- Conexao.Protocol := 'postgresql-9';
- Conexao.User := 'BotCron';
- Conexao.Connect;
- Query := TZQuery.Create(nil);
- Query.Connection := Conexao;
- except
- on e: exception do
- LogErro('Erro ao Conectar a base de dados - Thread Crons','',e.Message,'BotCronThread');
- end;
- end;
- procedure ThreadCrons.Execute;
- var IDLog : Integer;
- Retorno : DWORD;
- procedure WaitCron;
- begin
- GravaMensagem(Disparador,'Executando');
- //StrPCopy(zAppName, FileName);
- //FillChar(StartupInfo, Sizeof(StartupInfo), #0);
- //StartupInfo.cb := Sizeof(StartupInfo);
- //if CreateProcessW(nil, zAppName,nil,nil, false,0, nil, nil, StartupInfo, ProcessInfo) then
- //begin
- ProcessoVivo := true;
- try
- Query.SQL.Clear;
- Query.SQL.Add('insert into log_crons(cron,inicio,disparador,PID) values (:pCron,clock_timestamp(),:pDisparador,:ppid) returning ID');
- Query.ParamByName('pCron').AsInteger := Cron;
- Query.ParamByName('pdisparador').AsInteger := Disparador;
- Query.ParamByName('ppid').AsInteger := PID;
- Query.Open;
- IDLog := Query.FieldByName('id').AsInteger;
- Query.SQL.Clear;
- Query.SQL.Add('update crons_disparadores set PID = :pPID where disparador = :pDisparador');
- Query.ParamByName('pPID').AsInteger := PID;
- Query.ParamByName('pDisparador').AsInteger := Disparador;
- Query.ExecSQL;
- except
- on e: exception do
- begin
- LogErro('Erro ao gravar Log Cron! ID: ' + IntToStr(Disparador), 'Serviço Thread Crons', e.Message, 'BotCronThread');
- end;
- end;
- FAdendo := '';
- ProcessHandle := OpenProcess($00100000, false, PID);
- Retorno := WaitforSingleObject(ProcessHandle, Interrupcao);
- if Retorno = WAIT_TIMEOUT then
- begin
- FAdendo := 'WAIT_TIMEOUT';
- MsgKill := 'Parado Por Tempo de Interrupção';
- if Concorrencia then
- MsgKill := MsgKill + ' - Extendida Execução ('+InttoStr(CountConcorrencia)+')';
- ProcessTerminate;
- end
- else
- begin
- case Retorno of
- WAIT_FAILED : FAdendo := 'WAIT_FAILED';
- WAIT_OBJECT_0 : FAdendo := 'WAIT_OBJECT_0';
- WAIT_ABANDONED : FAdendo := 'WAIT_ABANDONED';
- end;
- if not TerminoExterno then
- begin
- if Forcar then
- Mensagem := 'Execução Forçada Pelo Usuário.'
- else
- begin
- Mensagem := 'Executado Com Sucesso' ;
- if Concorrencia then
- Mensagem := Mensagem + ' - Extendida Execução ('+InttoStr(CountConcorrencia)+')';
- end;
- GravaMensagem(Disparador,Mensagem);
- //CloseHandle(ProcessInfo.hThread);
- CloseHandle(ProcessHandle);
- end;
- end;
- if Retorno = WAIT_FAILED then
- LogErro('Erro CreateProcess Thread Crons - Disparador : ' +inttostr(Disparador) + ' - PID : ' + IntToStr(PID) ,'CreateProcess','Interrupção : ' +IntToStr(Interrupcao) + ' - Last Error : ' + IntToStr(HResultFromWin32(GetLastError)) ,'BotCronThread');
- // atualiza banco de dados
- try
- if (MsgKill <> '') then
- Mensagem := MsgKill;
- Query.SQL.Clear;
- Query.SQL.Add('update log_crons set fim = clock_timestamp(), status_process =:pStatusProcess, status = :pstatus where id = :pId ');
- Query.ParamByName('pId').AsInteger := IDLog;
- Query.ParamByName('pstatus').AsString := Mensagem;
- Query.ParamByName('pStatusProcess').AsString := FAdendo;
- Query.ExecSQL;
- except
- on e: exception do
- begin
- LogErro('Erro ao gravar Log Cron - ID: ' + IntToStr(Disparador), 'Serviço Thread Crons', e.Message, 'BotCronThread');
- end;
- end;
- //end;
- ProcessoVivo := false;
- end;
- begin
- try
- if Conexao.PingServer then
- WaitCron;
- except
- on e: exception do
- begin
- LogErro('Erro ao executar Cron Thread! ID: ' + IntToStr(Disparador), 'Serviço Thread Crons', e.Message, 'BotCronThread');
- end;
- end;
- Query.Free;
- Conexao.Free;
- Situacao := 1;
- end;
- procedure ThreadCrons.ProcessTerminate();
- begin
- try
- if ProcessoVivo then
- begin
- TerminoExterno := true;
- GravaMensagem(Disparador, FMsgKill);
- // GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
- // TerminateProcess(ProcessInfo.hProcess, ExitCode);
- // CloseHandle(ProcessInfo.hThread);
- CloseHandle(ProcessHandle);
- ProcessHandle := OpenProcess(PROCESS_TERMINATE, false, PID);
- TerminateProcess(ProcessHandle, ExitCode);
- CloseHandle(ProcessHandle);
- ProcessoVivo := false;
- end;
- except
- on e : exception do
- LogErro('Erro ao Matar Processo - PID :'+inttostr(PID)+ ' Disparador : ' +IntToStr(Disparador),'',e.Message,'BotCronThread');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement