From 68b30613f5452afd9ec228b9a6e120df79c32cf8 Mon Sep 17 00:00:00 2001 From: April Ayres-Griffiths Date: Thu, 18 Apr 2019 10:21:28 +1000 Subject: [PATCH] auto restart process if needed --- backup/main.lfm | 5 +++-- backup/main.pas | 37 +++++++++++++++++++++++++++++++++++-- main.lfm | 5 +++-- main.pas | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/backup/main.lfm b/backup/main.lfm index 0f074ff..758683b 100644 --- a/backup/main.lfm +++ b/backup/main.lfm @@ -1,7 +1,7 @@ object GUIForm: TGUIForm - Left = 371 + Left = 275 Height = 683 - Top = 112 + Top = 109 Width = 1078 AlphaBlendValue = 128 Caption = 'microM8 GUI' @@ -10549,6 +10549,7 @@ object GUIForm: TGUIForm WindowTop = 0 WindowWidth = 0 FillAttribute = 0 + OnTerminate = MicroM8ProcessTerminate left = 240 top = 160 end diff --git a/backup/main.pas b/backup/main.pas index 8474161..14b61b4 100644 --- a/backup/main.pas +++ b/backup/main.pas @@ -7,7 +7,7 @@ interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, ComCtrls, ExtCtrls, StdCtrls, fphttpclient, LCLType, Buttons, AsyncProcess, DateUtils, - Clipbrd, + Clipbrd, lclintf, {$IFDEF WINDOWS} Windows, {$ENDIF} @@ -369,6 +369,7 @@ type procedure MenuItem14Click(Sender: TObject); procedure MenuItem15Click(Sender: TObject); procedure MenuItem16Click(Sender: TObject); + procedure MicroM8ProcessTerminate(Sender: TObject); procedure miD1InsBlankClick(Sender: TObject); procedure miD2InsBlankClick(Sender: TObject); procedure MenuItem1Click(Sender: TObject); @@ -568,6 +569,7 @@ type procedure WaitReposTimer(Sender: TObject); procedure SendOSDMessage(msg: string); procedure LaunchPAK(disk: string); + procedure CheckMicroM8RunState; private lx, ly, lw, lh: integer; lastShowTime: TDateTime; @@ -582,6 +584,7 @@ type VolPosition, WarpPosition: integer; VolDrag, WarpDrag: boolean; Caps: boolean; + NeedsRestart, Quitting: boolean; public procedure AppActivate(Sender: TObject); procedure AppDeactivate(Sender: TObject); @@ -2373,7 +2376,9 @@ begin result := self.httpc.Get(url) except on e: Exception do begin - // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2386,6 +2391,9 @@ begin except on e: Exception do begin // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2409,6 +2417,9 @@ begin except on e: Exception do begin // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2623,6 +2634,7 @@ end; procedure TGUIForm.FormCloseQuery(Sender: TObject; var CanClose: boolean); begin + Quitting := true; SimpleGet( baseUrl+'/api/control/quit' ); CanClose := true; end; @@ -3203,6 +3215,11 @@ begin end; end; +procedure TGUIForm.MicroM8ProcessTerminate(Sender: TObject); +begin + //lblTerm.Caption := 'core has terminated'; +end; + procedure TGUIForm.miD1InsBlankClick(Sender: TObject); begin SimpleGet(baseUrl + '/api/control/hardware/disk/blank/0'); @@ -3221,9 +3238,25 @@ end; procedure TGUIForm.MenuItem2Click(Sender: TObject); begin + Quitting := true; SimpleGet( baseUrl+'/api/control/quit' ); Application.Terminate(); end; +procedure TGUIForm.CheckMicroM8RunState; +begin + if Quitting then + exit; + if not NeedsRestart then + exit; + if MicroM8Process.Active then + exit; + { okay not running and we want to start it } + MicroM8Process.Active := true; + Sleep(50); + lx := -1; + ReposWindow; +end; + end. diff --git a/main.lfm b/main.lfm index 0f074ff..758683b 100644 --- a/main.lfm +++ b/main.lfm @@ -1,7 +1,7 @@ object GUIForm: TGUIForm - Left = 371 + Left = 275 Height = 683 - Top = 112 + Top = 109 Width = 1078 AlphaBlendValue = 128 Caption = 'microM8 GUI' @@ -10549,6 +10549,7 @@ object GUIForm: TGUIForm WindowTop = 0 WindowWidth = 0 FillAttribute = 0 + OnTerminate = MicroM8ProcessTerminate left = 240 top = 160 end diff --git a/main.pas b/main.pas index 4495692..c9b39e5 100644 --- a/main.pas +++ b/main.pas @@ -369,6 +369,7 @@ type procedure MenuItem14Click(Sender: TObject); procedure MenuItem15Click(Sender: TObject); procedure MenuItem16Click(Sender: TObject); + procedure MicroM8ProcessTerminate(Sender: TObject); procedure miD1InsBlankClick(Sender: TObject); procedure miD2InsBlankClick(Sender: TObject); procedure MenuItem1Click(Sender: TObject); @@ -568,6 +569,7 @@ type procedure WaitReposTimer(Sender: TObject); procedure SendOSDMessage(msg: string); procedure LaunchPAK(disk: string); + procedure CheckMicroM8RunState; private lx, ly, lw, lh: integer; lastShowTime: TDateTime; @@ -582,6 +584,7 @@ type VolPosition, WarpPosition: integer; VolDrag, WarpDrag: boolean; Caps: boolean; + NeedsRestart, Quitting: boolean; public procedure AppActivate(Sender: TObject); procedure AppDeactivate(Sender: TObject); @@ -2373,7 +2376,9 @@ begin result := self.httpc.Get(url) except on e: Exception do begin - // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2386,6 +2391,9 @@ begin except on e: Exception do begin // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2409,6 +2417,9 @@ begin except on e: Exception do begin // nothing much + // restart backend if not running + NeedsRestart:=true; + CheckMicroM8RunState; end; end; end; @@ -2623,6 +2634,7 @@ end; procedure TGUIForm.FormCloseQuery(Sender: TObject; var CanClose: boolean); begin + Quitting := true; SimpleGet( baseUrl+'/api/control/quit' ); CanClose := true; end; @@ -3203,6 +3215,11 @@ begin end; end; +procedure TGUIForm.MicroM8ProcessTerminate(Sender: TObject); +begin + //lblTerm.Caption := 'core has terminated'; +end; + procedure TGUIForm.miD1InsBlankClick(Sender: TObject); begin SimpleGet(baseUrl + '/api/control/hardware/disk/blank/0'); @@ -3221,9 +3238,25 @@ end; procedure TGUIForm.MenuItem2Click(Sender: TObject); begin + Quitting := true; SimpleGet( baseUrl+'/api/control/quit' ); Application.Terminate(); end; +procedure TGUIForm.CheckMicroM8RunState; +begin + if Quitting then + exit; + if not NeedsRestart then + exit; + if MicroM8Process.Active then + exit; + { okay not running and we want to start it } + MicroM8Process.Active := true; + Sleep(1000); + lx := -1; + ReposWindow; +end; + end.