Browse Source

TScheduledTasks fpc sample

Unknown 6 years ago
parent
commit
680e0b5329

BIN
samples/fpc/QuickThreads/ScheduledTasks/RunScheduledTasks


+ 60 - 0
samples/fpc/QuickThreads/ScheduledTasks/RunScheduledTasks.lpi

@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
+      <Title Value="RunScheduledTasks"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <BuildModes Count="1">
+      <Item1 Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+      <Modes Count="0"/>
+    </RunParams>
+    <Units Count="1">
+      <Unit0>
+        <Filename Value="RunScheduledTasks.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit0>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <Target>
+      <Filename Value="RunScheduledTasks"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <OtherUnitFiles Value="..\..\..\.."/>
+      <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions Count="3">
+      <Item1>
+        <Name Value="EAbort"/>
+      </Item1>
+      <Item2>
+        <Name Value="ECodetoolError"/>
+      </Item2>
+      <Item3>
+        <Name Value="EFOpenError"/>
+      </Item3>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 111 - 0
samples/fpc/QuickThreads/ScheduledTasks/RunScheduledTasks.lpr

@@ -0,0 +1,111 @@
+program RunScheduledTasks;
+
+{$APPTYPE CONSOLE}
+
+{$MODE DELPHI}
+
+uses
+  SysUtils,
+  DateUtils,
+  Quick.Commons,
+  Quick.Console,
+  Quick.Threads;
+
+type
+  TMyJob = class
+  private
+    fId : Integer;
+    fName : string;
+  public
+    property Id : Integer read fId write fId;
+    property Name : string read fName write fName;
+    procedure DoJob(task : ITask);
+    procedure Failed(task : ITask; aException : Exception);
+    procedure Finished(task : ITask);
+    procedure Expired(task : ITask);
+  end;
+
+var
+  scheduledtasks : TScheduledTasks;
+  myjob : TMyJob;
+  ScheduledDate : TDateTime;
+  ExpirationDate : TDateTime;
+
+{ TMyJob }
+
+procedure TMyJob.DoJob(task : ITask);
+var
+  a, b, i : Integer;
+begin
+  cout('[%s] task "%s" doing a %s job...',[DateTimeToStr(Now()),fName,task.Param[0].AsString],etInfo);
+  Sleep(Random(1000));
+  a := Random(100);
+  b := Random(5) + 1;
+
+  i := a div b;
+  cout('task "%s" result %d / %d = %d',[fName,a,b,i],etSuccess);
+end;
+
+procedure TMyJob.Failed(task : ITask; aException : Exception);
+begin
+  cout('task "%s" failed (%s)',[fName,aException.Message],etError);
+end;
+
+procedure TMyJob.Finished(task : ITask);
+begin
+  cout('task "%s" finished',[fName],etDebug);
+end;
+
+procedure TMyJob.Expired(task : ITask);
+begin
+  cout('task "%s" expired',[fName],etWarning);
+end;
+
+begin
+  Console.LogVerbose := LOG_DEBUG;
+  try
+    scheduledtasks := TScheduledTasks.Create;
+    scheduledtasks.RemoveTaskAfterExpiration := True;
+
+    myjob := TMyJob.Create;
+    myjob.Id := 1;
+    myjob.Name := 'Run now and repeat every 1 minute for 5 times';
+    scheduledtasks.AddTask('Taks1',['blue',7],True,myjob.DoJob
+                          ).OnException(myjob.Failed
+                          ).OnTerminated(myjob.Finished
+                          ).OnExpired(myjob.Expired
+                          ).RepeatEvery(1,TTimeMeasure.tmSeconds,5);
+
+    myjob := TMyJob.Create;
+    myjob.Id := 2;
+    myjob.Name := 'Run now and repeat every 1 second forever';
+    scheduledtasks.AddTask('Task2',['red',14],True,myjob.DoJob
+                          ).OnException(myjob.Failed
+                          ).OnTerminated(myjob.Finished
+                          ).OnExpired(myjob.Expired
+                          ).StartAt(Now()
+                          ).RepeatEvery(1,TTimeMeasure.tmseconds);
+
+    ScheduledDate := IncSecond(Now(),5);
+    ExpirationDate := IncSecond(ScheduledDate,10);
+
+    myjob := TMyJob.Create;
+    myjob.Id := 3;
+    myjob.Name := Format('Run at %s and repeat every 1 second until %s',[DateTimeToStr(ScheduledDate),DateTimeToStr(ExpirationDate)]);
+    scheduledtasks.AddTask('Task3',['white',21],True,myjob.DoJob
+                          ).OnException(myjob.Failed
+                          ).OnTerminated(myjob.Finished
+                          ).OnExpired(myjob.Expired
+                          ).StartAt(ScheduledDate
+                          ).RepeatEvery(1,TTimeMeasure.tmSeconds,ExpirationDate);
+
+
+    scheduledtasks.Start;
+    cout('Running tasks in background!',etInfo);
+    ConsoleWaitForEnterKey;
+    scheduledtasks.Free;
+  except
+    on E: Exception do
+      Writeln(E.ClassName, ': ', E.Message);
+  end;
+end.