Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scheduler as a service?

Status
Not open for further replies.

SpiderFlight

Programmer
Aug 22, 2001
37
US
Its there a way to run the Cognos scheduler as a service?
 
I've also been looking at this for some time, because I have a number of users who want to run reports at regular intervals. I've solved this by having a batch file called by windows scheduler every night. The batch file calls a macro which reads a simple control text file with one line for each report. So far I've got the following parameters available for users: Date (daily, weekly, monthly); output type (Excel, pdf, csv, dbf); report name; prompts. I build the password into the macro as fuhbar suggests.
It's almost finished and when it's up and running, the users will be able do what they like. The scheduler will run in the middle of the night so there shouldn't be any problems with resources.
Simon Rouse
 
DrSimon, all:
Strange but true, since yesterday the macro can't log on to the AccessManager as I wrote above.

Nothing has changed, except there was a reboot of the machine. Now everytime a Cognos logon box pops up. The username and password and namespace are the same as last week. With the same username and password I can logon manually.

Any ideas?

Thanks
f.
 
DrSimon,

Would you be willing to share the functionality you are developing to give users the ability to run setup schedules themselves? We've been trying to work out how to we can give them this ability here, sounds like you've achieved just what we need.

Thanks.
 
For what it's worth, here is the macro I have written which I call BATCH_CONTROL_MACRO.
Apart from the control file which is described in the comments, I also have a BAT (or CMD) file containing the following - this is this is placed in the scheduler and runs every night.
Code:
start "c:\Program Files\\Cognos\\cer3\\bin\\runmac32.exe" "\\server\\Macros Admin\\BATCH_CONTROL_MACRO.MCX"

Apart from changing the server name and removing the password(!), this the macro as it is. I suspect you'll need to modify certain things. One thing you do need to consider is security - I use Dave Griffin's mix() function to help but that may not be to everyone's satifaction.

Code:
Sub main

   ' *!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*
   ' *!* N.B.B. Impromptu on the server must be in Single Instance Mode (see Impromptu help:               *!* 
   ' *!* "Change the Number of Instances of Impromptu"), or each call will open another Impromptu session! *!* 
   ' *!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*

   ' This macro is the core of a facility that allows PSenterprise users to schedule Impromptu reports to run outside
   ' normal hours and save the file in any of the following formats:
   '  dbf; csv; Excel with Format, pdf
   ' The macro is called by a batch file held in the Windows Scheduler set to run once a day. Initially set to 22:00 hours,
   ' but can be changed if this causes a load etc.
   ' It is controlled by \\server\Control Files\Batch_Control.txt ' 
   ' Creates a log file (Batch_Control_Log_N) for every day of the month where N is the day of the month
    
   ' The data in Batch_Control.txt has the following format with 1 line per report:
   ' There are 4 'pieces to the line separated by semicolons.
   '  1) Date to be run: D (Daily), W (Weekly - every Sunday); F (Fortnightly - Sunday of every odd week);
   '                     M (Monthly - 1st day on the month)
   '  2) Type of output: D (dbf); C (csv) using ` as separator; E (Excel); P(pdf)
   '  3) Report Name: The full report name using the full formal path without mapping e.g.:
   '     \\server\\Reports\report1.imr
   '  4) Any responses to prompts in the report. These must be in the order dictated by the report and separated by |
   '  N.B. There must be a semicolon after the report name whether or not there is a prompt and no unnecessary spaces
   ' Here is an example of an entry to create a weekly pdf output for a report called Dummy that has the prompts "C" and "NUR"
   ' W;D;\\server\\Reports\report1.imr r;C|NUR
   ' If it is also needed in Excel format at monthly intervals another line would need to be added:
   ' M;E; \\server\\Reports\report1.imr r;C|NUR
   ' *!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*
   ' *!* N.B.B. Impromptu on the server must be in Single Instance Mode (see Impromptu help:               *!* 
   ' *!* "Change the Number of Instances of Impromptu"), or each call will open another Impromptu session! *!* 
   ' *!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*

   Dim impApp As object
   Dim impRep As Object
   Dim impPub as Object
   Dim CPath as String
   Dim Batch_Name as String
   Dim CF_File as String
   Dim Log_File as String
   Dim Line_In as String
   Dim Date_Type as String
   Dim OP_Type as String
   Dim Report_File as String
   Dim Prompt_String as String
   Dim pwd as String
   Dim Cat_File as String
   Dim Run_Now as Variant
   Dim P as Integer
   Dim File_Name as string
   Dim b_error as variant
              
   CPath="\\server\Control Files\"
   Batch_Name="Batch_Control"
   CF_File=CPath & Batch_Name & ".txt"
   Log_File=CPath & Batch_Name & "_Log_" & format(Now,"ww") & ".txt"

   Open Log_File for Output as #1
   Print #1, "Log opened: " & format(now, "dd/mm/yy") & " " & format(now,"hh:mm")
   if Dir(CF_File)="" then 
      Print #1,"Control file is missing: " & CF_File
      goto End_Prog
   end if

   pwd=[The Password]
   Cat_File="\\server \catalogs\catalog.cat"
   Set impApp = CreateObject("Impromptu.Application")
   impApp.Visible False
   On error resume next
   impApp.OpenCatalog Cat_File[UserClassID],[UserClassPassword],[DatabaseID],[DatabasePassword],1
   if not impApp.DatabaseConnected then
      Print #1,"Catalog is missing or UseriD/Password in valid. " & Cat_File
      goto End_Prog
   end if
   if err=440 then
      Print #1,"Catalog is missing: " & Cat_File
      goto End_Prog
   elseif err<>0 then
       Print #1,"Error: " & Err & " " & Error$
   end if
   
   Open CF_File For Input as #2
      Do While not EOF(2)

      Line Input #2, Line_In 
         Date_Type=Ucase(getfield(Line_In,1,";"))
         OP_Type=UCase(getfield(Line_In,2,";"))
         Report_File=getfield(Line_In,3,";")
         Prompt_String=getfield(Line_In,4,";")
         
         If instr("DWFM",Date_Type)=0 then 
            Print #1,"***WARNING Date Type '" & Date_Type & "' invalid for " & Report_File
         end if
      
         Run_Now= False
         Select Case Date_Type
            Case "D"
               Run_Now=True
            Case "W"
               if Weekday(Now)=7 then Run_Now=True
            Case "F"
               if Weekday(Now)=1 and val(format(Now,"ww")) mod 2 = 0 then Run_Now=True
            Case "M"
               if day(Now)=1 then Run_Now=True
         End Select
         if Run_Now=False then goto Next_File

      Err=0   ' EOF sets Err to 62         
      Report_Name=left(Report_File,Len(Report_file)-4)

      on error resume next
      set impRep = impApp.OpenReport(Report_File,Prompt_String)
      b_error=err      
      if err<>0 then
         Print #1, "Error: " & Err & " " & Error$
      else
         Print #1, "Report opened: " & Report_File
   
    ' Want file name, this is last "\" piece of Report_Name string but can only find it by looking backwards
         For P=20 to 1 step -1    ' 
            if getfield(Report_Name,P,"\")<>"" then
               File_Name=getfield(Report_File,P,"\")
               File_Name=left(File_Name,len(File_name)-4)
               Exit for
            End if
         Next
   
         Select Case OP_Type
            Case "C"
               impRep.ExportAscii Report_Name & ".csv",True, True,"`"  ' Retain Headers, User quotes, "`" as separator
               Print #1, Spc(5), "'" & File_Name & "' saved as csv"
            Case "D"
               impRep.ExportdBase Report_Name & ".dbf"
               Print #1, Spc(5), "'" & File_Name & "' saved as dbf"
        ' dbf file names cannot be >8 characters. Won't truncate the name here as this could leave to overwriting.
        ' So save as is and send error message to log file if >8 bytes
       
               if len(File_Name)>8 then
                  Print #1, "**WARNING '" & File_Name & ".dbf' is not a valid name - truncate to 8 or less characters"
               end if
            Case "E"
               impRep.ExportExcelWithFormat Report_Name & ".xls"  
               Print #1, Spc(5), "'" & File_Name & "' saved as xls"
            Case "P"
               Set impPub = ImpRep.PublishPDF
               impPub.publish Report_Name & ".pdf"
               Print #1, Spc(5), "'" & File_Name & "' saved as pdf"
            Case Else
               Print #1, Spc(5), "***WARNING - Output type '" & OP_Type & "' not recognised for " & File_Name
         End Select 

         impRep.CloseReport
         set impRep=Nothing              
         
      end if
Next_File:      
   Loop
   Close #2

   ImpApp.ActiveCatalog.Close
   ImpApp.Quit
   Set ImpApp=Nothing

End_Prog:
   Print #1, "Log closed: " & format(now, "dd/mm/yy") & " " & format(now,"hh:mm")
   Close #1
        
End Sub

Get back to me if this doesn't make sense and also if anyone can find a better way to do things than I have. I'm always willing to learn.
Simon Rouse
 
Thanks a lot!

I'll give it a try and let you know how well it works in our environment.
 
I've made some alterations to your script to give me the extra functionality I need:

1. The script emails the exported report to the email addrsdss specified.
2. The report path and the save location are itemised seperately as I want to be able to save in a different location from the report file.
3. I've included the ability to run the report on the 15th of the month.

There are two problems I need to resolve:

1. When the script runs multiple reports it emails the first one, but not subsequent ones, the export is done ok and when I check the email variables they are all populated correctly but no email arrives, any ideas?

2. I need to be able to pass a date prompt relative to the date the report is run. I'm thinking if I put something in the prompt in the text file like |Today - 30| and then run a function over it which replaces "Today" with Format(Cvar(DATE - 31), "yyyy-mm-dd"). I'd then build the select something like this:
strPromptValues = Format(Cvar(DATE - 31), "yyyy-mm-dd") & "|" & Format(Cvar(DATE), "yyyy-mm-dd") & "|||||"
any ideas how to do it?

I'm not a programmer so may not have included all the necessary error traps or formatted it correctly.

Thanks.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Dim objMyMail as object
dim objConfig as object
dim rseMail as object
Dim Fields as object
Dim impApp As object
Dim impRep As Object
Dim impPub as Object
Dim CPath as String
Dim Batch_Name as String
Dim CF_File as String
Dim Log_File as String
Dim Line_In as String
Dim Date_Type as String
Dim OP_Type as String
Dim Report_File as String
Dim Prompt_String as String
Dim pwd as String
Dim Cat_File as String
Dim Run_Now as Variant
Dim P as Integer
Dim File_Name as string
Dim b_error as variant
Dim Report_Open as String
Dim Pub_Path as String
Dim Pub_Report As String
Dim Send_To as String
Dim Send_Text As String


CPath="\\wlnh2\psenterprise$\Impromptu Reports\Report Production\Scheduler Macros\"
Batch_Name="Batch_Control"
CF_File=CPath & Batch_Name & ".txt"
Log_File=CPath & Batch_Name & "_Log_" & format(Now,"ww") & ".txt"

'Prepare Email Objects
Set objMyMail = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.Configuration")

Open Log_File for Output as #1
Print #1, "Log opened: " & format(now, "dd/mm/yy") & " " & format(now,"hh:mm")
if Dir(CF_File)="" then
Print #1,"Control file is missing: " & CF_File
goto End_Prog
end if

pwd=[psadmin]
Cat_File="\\wlnh2\psenterprise$\Impromptu Reports\PSS.Cat"
Set impApp = CreateObject("Impromptu.Application")
impApp.Visible 0
On error resume next
ImpApp.OpenCatalog "\\wlnh2\psenterprise$\Impromptu Reports\PSS.Cat", "psadmin", "psadmin"
if not impApp.DatabaseConnected then
Print #1,"Catalog is missing or UseriD/Password in valid. " & Cat_File
goto End_Prog
end if
if err=440 then
Print #1,"Catalog is missing: " & Cat_File
goto End_Prog
elseif err<>0 then
Print #1,"Error: " & Err & " " & Error$
end if

Open CF_File For Input as #2
Do While not EOF(2)

Line Input #2, Line_In
Date_Type=Ucase(getfield(Line_In,1,";"))
OP_Type=UCase(getfield(Line_In,2,";"))
Report_Path=getfield(Line_In,3,";")
Report_File=getfield(Line_In,4,";")
Pub_Path=getfield(Line_In,5,";")
Prompt_String=getfield(Line_In,6,";")
Send_To=getfield(Line_In,7,";")
Send_Text=getfield(Line_In,8,";")


If instr("DWFMH",Date_Type)=0 then
Print #1,"***WARNING Date Type '" & Date_Type & "' invalid for " & Report_File
end if

Run_Now= False
Select Case Date_Type
Case "D"
Run_Now=True
Case "W"
if Weekday(Now)=7 then Run_Now=True
Case "F"
if Weekday(Now)=1 and val(format(Now,"ww")) mod 2 = 0 then Run_Now=True
Case "M"
if day(Now)=1 then Run_Now=True
Case "H"
if day(Now)=15 then Run_Now=True
End Select
if Run_Now=False then goto Next_File

Err=0 ' EOF sets Err to 62
Report_Name=left(Report_File,Len(Report_file)-4)
Report_Open= Report_Path & Report_File

on error resume next
set impRep = impApp.OpenReport(Report_Open,Prompt_String)
b_error=err
if err<>0 then
Print #1, "Error: " & Err & " " & Error$
else
Print #1, "Report opened: " & Report_Open

'Set the published name and path
Pub_Report = Pub_Path & Report_Name & " " & Format(Cvar(DATE), "yymmdd")

Select Case OP_Type
Case "C"
Output_Format = ".csv"
impRep.ExportAscii Pub_Report & Output_Format,True, True,"`" ' Retain Headers, User quotes, "`" as separator
Print #1, Spc(5), "'" & Pub_Report & "' saved as csv"
Case "D"
Output_Format = ".dbf"
impRep.ExportdBase Pub_Report & Output_Format
Print #1, Spc(5), "'" & Pub_Report & "' saved as dbf"
' dbf file names cannot be >8 characters. Won't truncate the name here as this could leave to overwriting.
' So save as is and send error message to log file if >8 bytes

if len(File_Name)>8 then
Print #1, "**WARNING '" & Pub_Report & Output_Format' is not a valid name - truncate to 8 or less characters"
end if
Case "E"
Output_Format = ".xls"
impRep.ExportExcelWithFormat Pub_Report & Output_Format
Print #1, Spc(5), "'" & Pub_Report & "' saved as xls"
Case "P"
Output_Format = ".pdf"
Set impPub = ImpRep.PublishPDF
impPub.publish Pub_Report & Output_Format
Print #1, Spc(5), "'" & Pub_Report & "' saved as pdf"
Case Else
Print #1, Spc(5), "***WARNING - Output type '" & OP_Type & "' not recognised for " & File_Name
End Select

end if

'Set Email options
objMyMail.From = "pshragent_prod"
objMyMail.To = Send_To
objMyMail.Subject = Report_Name
objMyMail.htmlbody = Send_Text

' Email the output
Set Fields = objConfig.Fields
Fields.Item(" = 2
Fields.Item(" = "172.22.1.208"
Fields.Item(" = 25
Fields.Item(" = "text/html"
Fields.Update

Set objMyMail.Configuration = objConfig
objMyMail.addattachment (Pub_Report & Output_Format)

'.MIMEFormatted = False
objMyMail.Send
Print #1, Spc(5), "'" & Pub_Report & "' sent by email"

impRep.CloseReport
set impRep=Nothing
Set objMyMail = Nothing
Set objConfig = Nothing
Set Fields = Nothing

Next_File:
Loop
Close #2

ImpApp.ActiveCatalog.Close
ImpApp.Quit
Set ImpApp=Nothing

End_Prog:
Print #1, "Log closed: " & format(now, "dd/mm/yy") & " " & format(now,"hh:mm")
Close #1

End Sub
 
Firstly - Dave, thanks for your comments - it makes me feel more confident about what I've been doing.

goofaholix
1. Can't help with emails. I've never been happy with using these Cognos macros to do emails. In fact that's one of the reasons why I've developed this macro. If you need help, open another thread rather than continue with this one.
2. If the date difference is always going to be the same, I'd build that as a calculation in the report itself rather than the prompt:
e.g. 30DaysAgo= add-days(today(),-30)
Alternatively if you need it be to different each time you ruin the report , just create a numeric prompt (e.g. ?DaysAgoPrompt?) in the report and macro and create the following:
VariableDaysAgo= add-days(today(),-?DaysAgoPrompt?)
Obviously if you want that date to be before OR after Today, you'd need to put the sign into the macro rather than the calculation.
Glad that's it's been some help though. Once you've grasped the concepts there's no reason why this can't be extended with many more options by adding more pieces to the control file. The one significant danger is where different people can edit the control file and mess up other people's entries.
Simon Rouse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top