Tips_tutorials   >   Studiojs202   >   Timer Object Emails
Now that we have the oEmail object working we can set up a timer object to email us web stats at the end of every day, week, month, year.
We need to make sure the
external component is being loaded on startup.Create an object class that is subclassed from the timer external component.
The timer object superclass has several public methods which are inherited by our oWebStatsEmailTimer object.
; Set the properties.
Calculate $cinst.$autoreset as kTrue
Calculate $cinst.$reentrant as kFalse
Calculate $cinst.$useseconds as kTrue
Calculate $cinst.$timervalue as 60*60 ;; Hourly
; Copy the parameter values to ivars.
Calculate iToEmailAddr as pToEmailAddr
Calculate ikInterval as pkInterval
; Set the begin and the end period for the current interval period.
Calculate iEndDay as #D
Calculate iEndWeek as lday(kWeek,#D)
Calculate iEndMonth as lday(kMonth,#D)
Calculate iEndYear as lday(kYear,#D)
Quit method kTrue
The $timer method is called each time the timer reaches the specified $timervalue duration.
; Is there another method currently running?
Calculate StackList as sys(192)
Calculate FlagOK as kTrue ;; Default the flag to true.
If StackList.$linecount>1
; Do nothing, another method is running.
Else
; Have we started the day after the current end date?
If #D>iEndDay
If ikInterval=kDay ;; kWeek, kMonth, kYear
; Time to send a web stats email.
Calculate Interval as 'Daily'
Do $cinst.$sendWebStatsEmail(iEndDay,iEndDay,Interval) Returns FlagOK
End If
If FlagOK
; Set the end day to today
Calculate iEndDay as #D
End If
If FlagOK
; Have we started the day after the current end week?
If #D>iEndWeek
; Check to make sure the interval is not monthly or yearly.
If ikInterval<>kMonth&ikInterval<>kYear
; Time to send a web stats email.
Calculate BeginIntervalDate as fday(kWeek,iEndWeek)
Calculate Interval as 'Weekly'
Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndWeek,Interval) Returns FlagOK
End If
If FlagOK
Calculate iEndWeek as dadd(kWeek,1,iEndWeek)
End If
End If
End If
If FlagOK
; Have we started the day after the current end week?
If #D>iEndMonth
; Check to make sure the interval is not yearly.
If ikInterval<>kYear
; Time to send a web stats email.
Calculate BeginIntervalDate as fday(kMonth,iEndMonth)
Calculate Interval as 'Monthly'
Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndMonth,Interval) Returns FlagOK
End If
If FlagOK
Calculate iEndMonth as lday(kMonth,iEndDay)
End If
End If
End If
If FlagOK
; Have we started the day after the current end year?
If #D>iEndYear
; Time to send a web stats email.
Calculate BeginIntervalDate as fday(kYear,iEndYear)
Calculate Interval as 'Yearly'
Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndYear,Interval) Returns FlagOK
If FlagOK
Calculate iEndYear as lday(kMonth,iEndDay)
End If
End If
End If
Else
; Do nothing, autoreset will call the method again later.
Calculate FlagOK as kTrue
End If
End If
If not(FlagOK)
; We have to report an error to the user.
; Since this is a web server, we can't prompt the user.
; Send the error to the trace log.
Do errhndlr.$getonceLastError(Mssg,Method)
Send to trace log {----- START ERROR MESSAGE ---- [#D] ---- [$cmethod().$name] ----- [$ctask().$name] -----}
Send to trace log {----- The '[Method]' method logged the following message:}
Send to trace log {----- [Mssg]}
Send to trace log {----- END ERROR MESSAGE ----}
End If
Quit method FlagOK
; Get the web stats for the current period.
Do webmon.$retWebStatsList(pBeginDate,pEndDate) Returns StatsList
If StatsList.$colcount
; Convert the list to text for the email body.
Do webmon.$convertWebStatsListToText(StatsList) Returns Body
If len(Body)
; Send the email.
Calculate Subject as con("Web App Server Stats - ",pBeginDate," to ",pEndDate)
If len(pInterval)>0
Calculate Subject as con(pInterval," ",Subject)
End If
Calculate Body as con(kCr,Subject,kCr,kCr,Body)
Do eml.$sendEmail(Subject,Body,iToEmailAddr) Returns FlagOK
End If
End If
Quit method FlagOK
We will instantiate and initialize the oWebStatsEmailTimer object from the oWebMonitor object class.
Do iRow.$definefromsqlclass('tWebappstat')
Do iRow.$sessionobject.$assign($ctask.dbsessionobj)
; Default flag to true.
Calculate FlagOK as kTrue
; Initialize the oEmailWebStatsTime object if a 'To' email address has been provided.
If len(pToEmailAddrWebStats)
Do ioWebStatsEmailTimer.$initialize(pToEmailAddrWebStats,pkBaseIntervalEmailWebStats) Returns FlagOK
If FlagOK
; Start the timer.
Do ioWebStatsEmailTimer.$starttimer()
End If
End If
Quit method FlagOK
Do ioWebStatsEmailTimer.$sendWebStatsEmail(pBeginDate,pEndDate) Returns FlagOK
Quit method FlagOK
We need to modify the $construct method of the Startup_Task to send the parameters we added to the $initialize method of oWebMonitor.
; Initialize the web monitoring object.
Calculate ToEmailAddr as 'Your_Name <your_email_address>'
Do webmon.$initialize(ToEmailAddr,kDay) Returns FlagOK
If FlagOK
; Initialize the email object.
Do eml.$initialize(SMTPServer,SMTPUser,SMTPPassword,POP3Server,POP3User,POP3Password,DefaultFromEmailAddr,DefaultFromName)
End If
Quit method FlagOK
Test the oWebStatsEmailTimer object using the .
Calculate BeginDate as fday(kMonth,#D)
Calculate EndDate as lday(kMonth,#D)
Do webmon.$sendWebStatsEmail(BeginDate,EndDate) Returns FlagOK