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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program Frequency into a scheduler app?

Status
Not open for further replies.

Solo4357

MIS
Jun 21, 2004
105
0
0
US
I have an application I'm trying to create that will poll every 15 minutes. When they enter in the job, they pick Days of week, start time, frequency, times per month.

I got it to check correctly for whether it's before the start time and date, whether it's a day of the week. I'm stumped on the best way to do frequency.... Here's what I got so far...
Code:
'Arrays have all the info regarding the jobs. Each job has different running times.
 For I = 0 To ArrName.Length - 2
                    StartTimeTemp = CType(ArrStartTime(I), DateTime)
                    'Is the start time before current time? Then run job, otherwise a future job.
                    If DateDiff(DateInterval.DayOfYear, StartTimeTemp, DateTime.Now) > 0 Then
                        ' *******************************  Check if Job Day is today.
                        checkdays = ArrDays(I).Split(",")
                        yesno = False
                        For J = 1 To checkdays.Length - 1
                            If checkdays(J) = Date.Now.DayOfWeek Then
                                yesno = True
                            End If

                        Next
                        If yesno = True Then

' Now here is where I check frequency and times per month I think. Now I'm stumped.  If everything is good, then run the job. 

                            currentJob.Name = ArrName(I)
                            currentJob.Type = ArrType(I)
                            currentJob.Frequency = ArrFrequency(I)
                            currentJob.Days = ArrDays(I)
                            currentJob.TimePerMonth = ArrTimePerMonth(I)
                            currentJob.StartTime = ArrStartTime(I)
                            currentJob.Script = ArrScript(I)
                            currentJob.Message = ArrMessage(I)
                            currentJob.Emailto = ArrEmailto(I)

                            runjob(currentJob)
                        End If
                    End If



                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

If I need to change the data I'm capturing for the job's schedule or add things, I can do that... any help would be great.
 
Do you store whether or not you have already run the job? Because if you are supposed to run it 3x's a month how do you know if you have already ran it?

-The answer to your problem may not be the answer to your question.
 
Ok I got something that works without having to deal with the how often it's been updated. I do my frequency per day but there's no reason the interval couldn't be per month, per week, etc. with whatever timeintervals are available.

Basically what I wanted to was to set up jobs that ran on a user defined frequency. Twice a day? 96 times a day? Anywhere in between?



There are other things that check day/date, enabled, etc but if those were correct, it then checked the frequency. If it was 1, then it just ran the job at a specified time. But what If it was more than once? I didn’t want it running 96 times between 8 and 8:30.



Here’s what I came up with.

Code:
  Private Function CalcDays(ByVal frequency As Integer) As Boolean

        Dim YesNo As Boolean = False

        ' if the function returns correct 

        Dim i, Freq, TotalMinutes, CalcTime As Integer

        'Set some integer variables... 

        Dim time, Midnight As DateTime

        'This all works within a single day so set Midnight and Time to Today at midnight.

        Freq = frequency

        'Frequency is set by the job entry. How many times per day does a job run? No more than 96 which is every 15 minutes for this, but there's no limit.

        Midnight = DateTime.Today

        time = DateTime.Today

        TotalMinutes = 1440 / Freq

        'Total minutes divided by the Frequency. There are 1440 minutes in a day

        For i = 0 To 1439 Step TotalMinutes

            'I run through once a minute. Step is how much I increment "I" per loop. 

            time = DateAdd(DateInterval.Minute, i, Midnight)

            'Add I to Time to start the counting. First is 0 so midnight. 

            CalcTime = DateDiff(DateInterval.Minute, time, DateTime.Now)

            'calc the difference between the time and now. As you get closer and closer to now, the number gets smaller. Sometimes NOW will

            'never work in which case it wasn't the right time. So YesNo will never be true.

            If CalcTime = 0 Then YesNo = True

        Next

        Return YesNo

    End Function


As you can see, I increments by 180 or 3 hours every time. 24 hours in a day… 8 x 3 is 24. I have well spread out jobs. Now I ran this at 2:21. So NOW would be 2:21pm. None of those times match so I would get a false returned to me. It would only get a “true” at midnight, 3, 6, 9, noon, etc. When I get a true, I run the job. This will work with any number for the frequency, though putting in 1440 or higher would make the job run more than the minutes in a day. I limited my textbox extry to 96 which is every 15 minutes. Also you wouldn’t want to poll less than one minute at a time, otherwise a job would run several times within the same minute until the clock ticked over to the next minute.

I haven't seen this anywhere so I hope it helps someone.
 
what if whatever process(es) you are running take longer than a minute? Or the app goes down at (for example) 2:59 and the box doesnt come back up till 3:01? Is it a big deal, or will it just get picked up the next time it runs?

Just things that can throw a wrench in your function...

-The answer to your problem may not be the answer to your question.
 
I'm going to run my processes every minute but my frequency will never be more than 96 times. Yeah, it would get lost, but for my purposes, it wouldn't be a big deal. I'm just writing a scheduler to run scripts in the database and email them out.
 
Just to throw another idea out there... You could create a 'calender' that tracks when events should be fired. It could be a data table that stores all of the events and the time that they should fire. When ever you add an event with a frequency, just loop through time incrementing by that frequency for a few years, and add each of those to the calender. Then set a timer to fire every 15 seconds. Every time the timer fires, check to see if any events are scheduled to be launched.

I would say add the event name/id, start date/time, and frequency in a data table (that gets written/loaded to XML). That way, any time you start up the app, it can rebuild the calender.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top