Recent

Author Topic: how to create sync application  (Read 5616 times)

Packs

  • Sr. Member
  • ****
  • Posts: 416
how to create sync application
« on: January 13, 2025, 12:14:31 pm »
Hi,

I want to pull master data from one database and push to another database on every 15 minutes and transaction every minutes
and report 30 minutes time .

should I create 3 timer component and execute  queries .

Or should I create 3 thread and execute .

What is the best method .

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: how to create sync application
« Reply #1 on: January 13, 2025, 12:20:28 pm »
You need just one timer and modulo calculation.
But I am sure they don't want the Trumps back...

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #2 on: January 13, 2025, 12:25:36 pm »
Dear Sir,

Not getting you . Please explain in detailed

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: how to create sync application
« Reply #3 on: January 13, 2025, 12:36:12 pm »
One timer:
then:
 mod (1024 * 60)  = 0 for every minute
 mod (15 * 1024 * 60) = 0 for every 15 minutes
 mod (30 * 1024 * 60) = 0 for every 30 minutes
Because timer resolution is msec.

Simple. One timer can handle all three conditions.

Do you understand that? If not report back.
« Last Edit: January 13, 2025, 12:41:00 pm by Thaddy »
But I am sure they don't want the Trumps back...

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #4 on: January 13, 2025, 01:16:23 pm »
Dear sir,

I have never user mod .I am not getting really sorry.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     IdleTimer1: TIdleTimer;
  16.     Label1: TLabel;
  17.     Shape1: TShape;
  18.     Timer1: TTimer;
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.   private
  22.     start: integer;
  23.     procedure one_minutes() ;
  24.     procedure fifteen_minutes() ;
  25.     procedure thirty_minutes() ;
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. uses math ;
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.Timer1Timer(Sender: TObject);
  41. begin
  42.  
  43.   (*
  44.   Label1.Caption := Format('%d sec', [start]);
  45.   Dec(start);
  46.   if (start < 5) then Shape1.Brush.Color := clYellow;
  47.   if (start < 0) then
  48.   begin
  49.     Timer1.Enabled := False;
  50.     Shape1.Brush.Color := clGreen;
  51.     Label1.Caption := 'Finished!';
  52.   end;
  53.   *)
  54.   Label1.Caption := Format('%d sec', [start]);
  55.   if (mod (1024 * 60)  = 0) then one_minutes();
  56.   if mod (15 * 1024 * 60) = 0 then  fifteen_minutes();
  57.   if mod (30 * 1024 * 60) = 0 then  thirty_minutes();
  58.  
  59. end;
  60.  
  61. procedure TForm1.one_minutes();
  62. begin
  63.   Shape1.Shape := stCircle;
  64.   Shape1.Brush.Color := clBlue;
  65.  
  66. end;
  67.  
  68. procedure TForm1.fifteen_minutes();
  69. begin
  70.   Shape1.Shape := stCircle;
  71.   Shape1.Brush.Color := clYellow;
  72.  
  73. end;
  74.  
  75. procedure TForm1.thirty_minutes();
  76. begin
  77.   Shape1.Shape := stCircle;
  78.   Shape1.Brush.Color := clGreen;
  79.  
  80. end;
  81.  
  82. procedure TForm1.FormCreate(Sender: TObject);
  83. begin
  84.   Caption := 'Countdown calculation';
  85.   Timer1.Interval := 1000;
  86.   Timer1.Enabled := True;
  87.   Label1.Caption := '';
  88.   Shape1.Shape := stCircle;
  89.   Shape1.Brush.Color := clRed;
  90.   start := 20;
  91. end;
  92.  
  93. end.
  94.  

I tried to write .


 

Khrys

  • Full Member
  • ***
  • Posts: 146
Re: how to create sync application
« Reply #5 on: January 13, 2025, 02:08:11 pm »
The modulo operation returns the remainder of a division - for example,  23 mod 5 = 3.
You can use this to easily check if a number  n  is exactly divisible by another number  m, because in that case,  n mod m = 0  (no remainder).
So if you have a timer that increments a counter once per minute, you can check if a multiple of 15 minutes have passed by asserting that  Counter mod 15 = 0.

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     // [...]
  4.     Minutes: Cardinal;
  5.   end;
  6.  
  7. procedure TForm1.Timer1Timer(Sender: TObject);
  8. begin
  9.   Inc(Minutes);
  10.   one_minutes();
  11.   if Minutes mod 15 = 0 then fifteen_minutes();
  12.   if Minutes mod 30 = 0 then thirty_minutes();
  13. end;
  14.  
  15. procedure TForm1.FormCreate(Sender: TObject);
  16. begin
  17.   // [...]
  18.   Timer1.Interval := 60000; // 60 seconds * 1000 ms
  19. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: how to create sync application
« Reply #6 on: January 13, 2025, 09:22:14 pm »
my suggstion is correct, but not 1024, but 1000 ms. That was an oversight.
But I am sure they don't want the Trumps back...

ASerge

  • Hero Member
  • *****
  • Posts: 2373
Re: how to create sync application
« Reply #7 on: January 14, 2025, 01:40:33 am »
I want to pull master data from one database and push to another database on every 15 minutes and transaction every minutes
and report 30 minutes time .

should I create 3 timer component and execute  queries .

Or should I create 3 thread and execute .
If the queries are heavy and you need a quick response to user actions, then it's better to use threads.
GUI timers are lightweight objects, so it's easier to use three timers to avoid confusion. You need to understand that timers are triggered without a guarantee, they may be later than expected because they have the lowest priority.

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #8 on: January 14, 2025, 08:31:48 am »
Dear Sir,

If I create 3 timer component and specify 3 different intervals and execute my queries . it will run in three different thread too.

Please guide me .

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: how to create sync application
« Reply #9 on: January 14, 2025, 08:53:10 am »
*sigh*
and if it's a DBMS like MySQL or MSSQL, why not use a scheduled Task/Job Agent, and let the Database do the work?
Why are we even discussing stuff like this?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #10 on: January 14, 2025, 08:57:54 am »
Not allowed at db level...

Client will not allow us

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: how to create sync application
« Reply #11 on: January 14, 2025, 09:15:42 am »
Not allowed at db level...

Client will not allow us
Then Thaddy has answered your Question (incl. the correction from 1024 to 1000)
One Timer with Interval 1 minute (6000ms)
In the Timer-Event you'd need 1 local writable Const, which counts up everytime the event fires.
Check for 15, execute 15-minute-code, check for 30, execute 30-minute-code
If 30 reset Counter


Oh, and when Timer reaches 30 minutes, should the action for "15 minutes" be executed, too?
What about the action for "1 minute"?

Aircode
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. Const LocalCount:Integer=1;
  3. begin  
  4.    Execute1MinuteCode;
  5.    If LocalCount mod 15=0 Then Execute15MinuteCode;  //This also fires when 30 Minutes
  6.    If LocalCount=30 Then
  7.       Begin
  8.          Execute30MinuteCode;
  9.          LocalCount:=0;
  10.       End;
  11.    Inc(LocalCount);
  12. end;  
« Last Edit: January 14, 2025, 09:54:57 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #12 on: January 14, 2025, 10:25:12 am »
Yes Thaddy ( SIR ) is correct.

Thank you .🙏

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #13 on: January 14, 2025, 10:26:04 am »
Thanks to everyone 🙏

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to create sync application
« Reply #14 on: January 14, 2025, 11:11:36 am »
To keep sync application live . I have created application watch man for windows . it is starting new application .

I am sharing the code . Please highlight my mistake .

Code: Pascal  [Select][+][-]
  1. program WatchdogApp;
  2.  
  3. uses
  4.   Classes, SysUtils, Process;
  5.  
  6. const
  7.   MAIN_APP = 'project1'; // Name of the main application executable (without extension)
  8.   CHECK_INTERVAL = 5000; // Check every 5 seconds
  9.  
  10. function IsProcessRunning(const AProcessName: string): Boolean;
  11. var
  12.   Process: TProcess;
  13.   Output: TStringList;
  14. begin
  15.   Result := False;
  16.   Process := TProcess.Create(nil);
  17.   Output := TStringList.Create;
  18.   try
  19.     Process.Executable := 'tasklist';   // linux pgrep
  20.     Process.Parameters.Add('-f');
  21.     Process.Parameters.Add(AProcessName);
  22.     Process.Options := [poUsePipes, poWaitOnExit];
  23.     Process.Execute;
  24.  
  25.     Output.LoadFromStream(Process.Output);
  26.     Result := Output.Count > 0; // If output is not empty, the process is running
  27.   finally
  28.     Output.Free;
  29.     Process.Free;
  30.   end;
  31. end;
  32.  
  33. procedure RestartProcess(const AProcessName: string);
  34. var
  35.   Process: TProcess;
  36. begin
  37.   Process := TProcess.Create(nil);
  38.   try
  39.     Process.Executable := AProcessName;
  40.     Process.Options := [poDetached]; // Run the process in the background
  41.     Process.Execute;
  42.   finally
  43.     Process.Free;
  44.   end;
  45. end;
  46.  
  47. begin
  48.   while True do
  49.   begin
  50.     if not IsProcessRunning(MAIN_APP) then
  51.     begin
  52.       WriteLn('Main application is not running. Restarting...');
  53.       RestartProcess(MAIN_APP);
  54.     end
  55.     else
  56.     begin
  57.       WriteLn('Main application is running.');
  58.     end;
  59.  
  60.     Sleep(CHECK_INTERVAL); // Wait before checking again
  61.   end;
  62. end.
  63.  
  64.  

 

TinyPortal © 2005-2018