Atozed Forums
Auto-start a new session from ServerController - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Atozed Software Products (https://www.atozed.com/forums/forum-1.html)
+--- Forum: IntraWeb (https://www.atozed.com/forums/forum-3.html)
+---- Forum: English (https://www.atozed.com/forums/forum-16.html)
+----- Forum: IntraWeb General Discussion (https://www.atozed.com/forums/forum-4.html)
+----- Thread: Auto-start a new session from ServerController (/thread-3273.html)



Auto-start a new session from ServerController - zsleo - 06-01-2023

I want to auto-start a new session from ServerController that does not have a user interface.

Is there a better way of starting the session other than by calling the URL with the required parameters?

TIA


RE: Auto-start a new session from ServerController - Alexandre Machado - 06-02-2023

Do you really need to create a session or just to do some background task once or regularly?


RE: Auto-start a new session from ServerController - zsleo - 06-03-2023

(06-02-2023, 11:09 PM)Alexandre Machado Wrote: Do you really need to create a session or just to do some background task once or regularly?

It is multiple background tasks regulary


RE: Auto-start a new session from ServerController - Alexandre Machado - 06-03-2023

Ideally you should create a background thread and run these tasks from it. If they can run sequentially, it's even better.

You can use our TIWTimedThread class that will handle this nicely:


Code:
uses
  IW.Common.Threads;

type
  TMyThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  end;

procedure TMyThread.DoExecute;
begin
  // do your stuff here
end;

// In your ServerController unit:

// Declare the thread as a field in our ServerController:
type
  TIWServerController = class(TIWServerControllerBase)
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
    procedure IWServerControllerBaseConfig(Sender: TObject);
    procedure IWServerControllerBaseDestroy(Sender: TObject);
  private
    FMyThread: TMyThread;
  public
  end;

// Create and start it from the ServerController.OnConfig event:
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  FMyThread := TMyThread.Create('My background thread', Interval_in_miliseconds);
  FMyThread.Start;
end;

// Finalize the thread in ServerController's OnDestroy event:
procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
  if Assigned(FMyThread) then   // always check because creation of ServerController may fail if something is wrong
    FMyThread.Finish;           // Finish will terminate and destroy the thread
end;

TIWTimedThread will call the DoExecute method at regular intervals, which is specified when you create the thread.

I recommend you to put the code of the thread in a separate unit. Also, when working with threads: If you need any database connectivity from within the thread, create an exclusive connection in the thread itself, don't try to "share" an external connection. 

I'll see if I can publish a demo showing an usage example like this.

Cheers


RE: Auto-start a new session from ServerController - zsleo - 06-03-2023

(06-03-2023, 08:28 PM)Alexandre Machado Wrote: Ideally you should create a background thread and run these tasks from it. If they can run sequentially, it's even better.

You can use our TIWTimedThread class that will handle this nicely:


Code:
uses
  IW.Common.Threads;

type
  TMyThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  end;

procedure TMyThread.DoExecute;
begin
  // do your stuff here
end;

// In your ServerController unit:

// Declare the thread as a field in our ServerController:
type
  TIWServerController = class(TIWServerControllerBase)
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
    procedure IWServerControllerBaseConfig(Sender: TObject);
    procedure IWServerControllerBaseDestroy(Sender: TObject);
  private
    FMyThread: TMyThread;
  public
  end;

// Create and start it from the ServerController.OnConfig event:
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  FMyThread := TMyThread.Create('My background thread', Interval_in_miliseconds);
  FMyThread.Start;
end;

// Finalize the thread in ServerController's OnDestroy event:
procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
  if Assigned(FMyThread) then   // always check because creation of ServerController may fail if something is wrong
    FMyThread.Finish;           // Finish will terminate and destroy the thread
end;

TIWTimedThread will call the DoExecute method at regular intervals, which is specified when you create the thread.

I recommend you to put the code of the thread in a separate unit. Also, when working with threads: If you need any database connectivity from within the thread, create an exclusive connection in the thread itself, don't try to "share" an external connection. 

I'll see if I can publish a demo showing an usage example like this.

Cheers
Thanks Alexandre, 

Much appreciated


RE: Auto-start a new session from ServerController - Alexandre Machado - 06-04-2023

I've just published a new demo showing how to do it:

https://github.com/Atozed/IntraWeb/tree/master/15/Delphi/TaskInThread


RE: Auto-start a new session from ServerController - zsleo - 06-05-2023

Thank you Alexandre.

Much appreciated