dominoExperts.com - Powered by Domino 8.5.2 Domino Accelerator Pack
- Reduce the number of HTTP requests
Lotus Triple Search DominoExperts + Blogs + R8 forum
dominoExperts.com -> Script vault -> Lotus Script

 Agent which runs every 1 minute... sort of.


Andrei KouvchinnikovPost date: 2007-04-30 19:35

An agent which I created last week. It simulates an agent scheduled with 1 minute interval by executing the function and sleeping the amount of seconds until the minute elapses and then triggering the function again. It does it 5 times and then exits. At that time, next instance of the same scheduled agent is about to start, which makes it possible to continiously invoke e.g. a monitoring function every 1 minute.


For more details and to see my mad MSPaint skillz read my blog post: http://dominounlimited.blogspot.com/2007/04/run-scheduled-agent-every-60-seconds.html


Sub Initialize
    'Created by Andrei Kouvchinnikov 
Print  "*******************  Agent started  *******************"

Dim session As New NotesSession
Dim db As NotesDatabase
Dim expectedruntime As Integer 'time which take to run the function. required only for deciding about ending the loop.
Dim runinterval As Integer 'interval (sec) between runs
Dim runmaxtimes As Integer ' max number of times the function will run
Dim runcounter As Integer 'counter of number of times the function run
Dim runstart As Long, runend As Long, rundiff As Long
Dim notimeleft ' loop must be closed now, no time left to run another round
Dim agentstart As Long 'initial time when agent started. required only for deciding about ending the loop.
Dim agentschedule As Integer 'nr of minutes this agent is scheduled to run. required only for deciding about ending the loop.
Dim dynadjust 'instead of hardcoded runtime value, use last run period for calculation of next period
Dim uselongestperiod 'instead of the last run period, use the longest period the function took to run

expectedruntime=10 ' function is initially expected to take max 10 seconds
runinterval=60  ' function is called with interval of X seconds
runmaxtimes=100  'if function's run time variates, you can limit max number of times function runs
agentschedinterval=5 'agent is scheduled to run every X minutes. From the agent properties.
margininterval=5 'nr of seconds to add to the final round. used for situations when function's execution time is 0.
runcounter=0
dynadjust=True
uselongestperiod=True

agentstart=Timer

While runcounter<runmaxtimes And notimeleft=False
  runstart=Timer
 
  Call MainAction()
 
  runend=Timer
  If dynadjust=True Then expectedruntime=Int(runend-runstart)  'dynamically adjust expected time to actual time it take to run the function
  If uselongestperiod Then
   If expectedruntime<Int(runend-runstart) Then expectedruntime=Int(runend-runstart)
  End If
  timeleft=runinterval-expectedruntime
 
  If Int((agentschedinterval*60)-Int(Timer-agentstart))<timeleft+(expectedruntime+Int(expectedruntime/100*30)+margininterval) Then  'assumes that function can take 30% longer time to run than the last time
   notimeleft=True
   Print "Exit. Function will not manage to finish one more run. Computed time: "+Cstr(Now)+" + "+Cstr((expectedruntime+Int(expectedruntime/100*30)))
  End If
 
  If timeleft>0 And notimeleft=False Then
   Sleep timeleft 'finished before the expected time. sleep until next execution.
  Else
   Sleep Int((agentschedinterval*60)-Int(Timer-agentstart))-margininterval 'sleep X seconds-margin
  End If
  runcounter=runcounter+1
Wend  
Print  "*******************  Agent finished  *******************"
End Sub


Sub MainAction()
Print "Triggered "+Cstr(Now)

%REM
Here goes your code
Delete demo code below.
%END REM


Randomize
sleeprand=Int(Rnd()*10) 
Sleep sleeprand 'Simulates time taken by the function's operations by sleeping X seconds

End Sub


Kenneth HaggmanPost date: 2007-05-23 15:21

While I am sure this agent runs more or less as expected, there is the risk that it will 'suck up' system resources while waiting.

At least, I know it did back in R5 when I created a similar agent.

A much better solution is to create a Java add-in task.
It will be scheduled perfectly and (provided your Java code is recycling properly) will use up any memory.




RSS feed
Subscribe to Forum

Share this page

Top posters
Tomas Nielsen212
Joacim Boive27
Fredrik Stöckel27
Danne14
Niklas Waller13
Kenneth Haggman11
Bryan Kuhn10
Daniel Lehtihet9
Jonas Israelsson8
dm997