Click here to Skip to main content
15,895,781 members
Articles / Web Development / IIS

Scheduled Tasks Web Service

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
23 Nov 2007CPOL6 min read 127.6K   2.5K   65  
A web service that acts as a scheduler for tasks.
Public Class TaskEditViewController



    ''' <summary>
    ''' holds all the schedule tasks
    ''' </summary>
    Private _TaskData As SchedulerService.Entities.ScheduleTaskDataset
    Public ReadOnly Property TaskData() As SchedulerService.Entities.ScheduleTaskDataset
        Get
            If Me._TaskData Is Nothing Then
                LoadData()
            End If
            Return Me._TaskData
        End Get
    End Property


    ''' <summary>
    ''' Holds a list of all available tasks
    ''' </summary>
    Private _AvailableTasks As String()
    Public ReadOnly Property AvailableTasks() As String()
        Get
            If Me._AvailableTasks Is Nothing Then
                LoadAvailableTasks()
            End If
            Return Me._AvailableTasks
        End Get
    End Property


    ''' <summary>
    ''' Loads all Available Tasks
    ''' </summary>
    Private Sub LoadAvailableTasks()
        Me._AvailableTasks = ServiceLayer.GetAvailableTasks
    End Sub

    ''' <summary>
    ''' Loads all the Tasks 
    ''' </summary>
    Private Sub LoadData()
        Me._TaskData = ServiceLayer.GetTaskList
    End Sub


    ''' <summary>
    ''' Determines if the Task is new
    ''' </summary>
    Private _IsNew As Boolean
    Public ReadOnly Property IsNew() As Boolean
        Get
            Return Me._IsNew
        End Get
    End Property


    ''' <summary>
    ''' ID of the current Task
    ''' </summary>
    Private _TaskID As Integer
    Public ReadOnly Property TaskID() As Integer
        Get
            Return Me._TaskID
        End Get
    End Property


    ''' <summary>
    ''' Determines the position in the dataset of the current Task
    ''' </summary>
    Private _Position As Nullable(Of Integer)
    Public ReadOnly Property Position() As Integer
        Get
            'If already dont have the position
            If Not Me._Position.HasValue Then
                Dim pos As Integer = 0
                For Each TaskRow As SchedulerService.Entities.ScheduleTaskDataset.TaskRow In Me.TaskData.Task.Rows
                    If TaskRow.TaskID = Me._TaskID Then
                        Exit For
                    Else

                        pos += 1
                    End If
                Next
                Me._Position = pos
            End If
            'return the position
            Return Me._Position.Value
        End Get
    End Property


    ''' <summary>
    ''' Store errors on last save operation
    ''' </summary>
    Private _errorString As String
    Public ReadOnly Property errorString() As String
        Get
            Return Me._errorString
        End Get
    End Property


    ''' <summary>
    ''' Contructor in Add mode
    ''' </summary>
    Public Sub New()
        'Set internal variables
        Me._IsNew = False
        'Create a new Task
        CreateTask()
    End Sub


    ''' <summary>
    ''' constructor in Edit mode
    ''' </summary>
    ''' <param name="TaskID">ID of the task to Edit</param>
    Public Sub New(ByVal TaskID As Integer)
        'Load the data and set internal variables
        Me.LoadData()
        Me._IsNew = False
        Me._TaskID = TaskID
    End Sub


    ''' <summary>
    ''' Create a new task
    ''' </summary>
    Private Sub CreateTask()
        'Load the data
        Me.LoadData()
        Dim newTask As SchedulerService.Entities.ScheduleTaskDataset.TaskRow = Me._TaskData.Task.NewTaskRow
        'Set the task
        newTask.StartTime = DateTime.Now
        newTask.TaskStatus = SchedulerService.Entities.Constants.TaskStatusList.WAITING_FOR_FIRST_RUN
        newTask.Enabled = True
        'Add the task
        Me._TaskData.Task.AddTaskRow(newTask)
        'Set the TaskID
        Me._TaskID = newTask.TaskID
    End Sub


    ''' <summary>
    ''' Save current data
    ''' </summary>
    Public Function SaveData(Optional ByVal IsDelete As Boolean = False) As Boolean
        'If no changes has been made
        If Not Me._TaskData.HasChanges Then
            Return True
        End If
        'Do not run this if comes from a delete
        If Not IsDelete Then
            'Validate the row 
            Me._errorString = Me._TaskData.Task(Me.Position).Validate
            'If errors were found
            If Me._errorString.Length > 0 Then
                Return False
            End If

            'Set the next run 
            If Not Me._TaskData.Task(Me.Position).Enabled Then
                'Erase next run
                Me._TaskData.Task(Me.Position).SetNextRunNull()
                'Set the new status
                Me._TaskData.Task(Me.Position).TaskStatus = SchedulerService.Entities.Constants.TaskStatusList.DISABLED
            ElseIf Not Me._TaskData.Task(Me.Position).IsLastRunNull AndAlso Not Me._TaskData.Task(Me.Position).IsRepeatEveryNull Then
                'Set the next run after the last run
                Me._TaskData.Task(Me.Position).NextRun = Me._TaskData.Task(Me.Position).LastRun.AddMinutes(Me._TaskData.Task(Me.Position).RepeatEvery)
                'Set the proper Status 
                Me._TaskData.Task(Me.Position).TaskStatus = SchedulerService.Entities.Constants.TaskStatusList.WAITING_FOR_NEXT_RUN
                'Fix next run if necesary
                SetNextRun()
            Else
                'set the next run
                Me._TaskData.Task(Me.Position).NextRun = Me._TaskData.Task(Me.Position).StartTime
                'Set the proper Status 
                Me._TaskData.Task(Me.Position).TaskStatus = SchedulerService.Entities.Constants.TaskStatusList.WAITING_FOR_FIRST_RUN
                'Fix next run if necesary
                SetNextRun()
            End If
        End If
        'update the task list
        ServiceLayer.UpdateScheduleTaskList(Me._TaskData)
        Return True
    End Function


    ''' <summary>
    ''' Delete current Task
    ''' </summary>
    Public Sub DeleteTask()
        'Delete current Task
        Me._TaskData.Task(Me.Position).Delete()
        'update the changes
        SaveData(True)
    End Sub


    ''' <summary>
    ''' Check the value for the next run and adjust the value based on other fields
    ''' </summary>
    Private Sub SetNextRun()
        'Check the Perform From and To Values
        If Not Me._TaskData.Task(Me.Position).IsPerformTaskFromNull AndAlso Not Me._TaskData.Task(Me.Position).IsPerformTaskToNull Then
            'Create tmp dates for From and To Times
            Dim FromTime As Date = Me._TaskData.Task(Me.Position).NextRun.Date + Me._TaskData.Task(Me.Position).PerformTaskFrom.TimeOfDay
            Dim ToTime As Date = Me._TaskData.Task(Me.Position).NextRun.Date + Me._TaskData.Task(Me.Position).PerformTaskTo.TimeOfDay
            'Set the Next Run as FromTime if NextRun is lower
            If Me._TaskData.Task(Me.Position).NextRun < FromTime Then
                'set the new Time
                Me._TaskData.Task(Me.Position).NextRun = FromTime
            End If
            'If the Next Run is higher than ToTime, set the Next Run as the FromTime of the Next Day
            If Me._TaskData.Task(Me.Position).NextRun > ToTime Then
                'set the new Time
                Me._TaskData.Task(Me.Position).NextRun = FromTime.AddDays(1)
            End If
        End If

        'Check if we have set the an End Time
        If Not Me._TaskData.Task(Me.Position).IsEndTimeNull AndAlso Me._TaskData.Task(Me.Position).EndTime < Me._TaskData.Task(Me.Position).NextRun Then
            'the task has finished its execution cycle
            Me._TaskData.Task(Me.Position).TaskStatus = SchedulerService.Entities.Constants.TaskStatusList.FINISHED
            Me._TaskData.Task(Me.Position).NextRun = Nothing
        End If
    End Sub


    ''' <summary>
    ''' Set as dbnull Perform values
    ''' </summary>
    ''' <param name="Enable">Perform is enabled or disabled</param>
    Public Sub FixDailySchedule(ByVal Enable As Boolean)
        If Not Enable Then
            Me._TaskData.Task(Me.Position).SetPerformTaskFromNull()
            Me._TaskData.Task(Me.Position).SetPerformTaskToNull()
        End If
    End Sub


    ''' <summary>
    ''' Set as dbnull End Time
    ''' </summary>
    ''' <param name="Enable">Perform is enabled or disabled</param>
    Public Sub FixEndTime(ByVal Enable As Boolean)
        If Not Enable Then
            Me._TaskData.Task(Me.Position).SetEndTimeNull()
        End If
    End Sub


    ''' <summary>
    ''' Determines if the Task has enabled the end time
    ''' </summary>
    Public Function EndTimeIsEnabled() As Boolean
        Return Not Me._TaskData.Task(Me.Position).IsEndTimeNull
    End Function


    ''' <summary>
    ''' Determines if the daily schedule is enabled
    ''' </summary>
    Public Function DailyScheduleIsEnabled() As Boolean
        If Me._TaskData.Task(Me.Position).IsPerformTaskFromNull OrElse Me._TaskData.Task(Me.Position).IsPerformTaskToNull Then
            Return False
        Else
            Return True
        End If
    End Function


    ''' <summary>
    ''' Initialize the Perform From and To values if has not been initialized
    ''' </summary>
    Public Sub InitializePerformTimeValues()
        If Me._TaskData.Task(Me.Position).IsPerformTaskFromNull Then
            Me._TaskData.Task(Me.Position).PerformTaskFrom = DateTime.Now
        End If
        If Me._TaskData.Task(Me.Position).IsPerformTaskToNull Then
            Me._TaskData.Task(Me.Position).PerformTaskTo = DateTime.Now
        End If
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Mexico Mexico
I am Pedro Ramirez from mexico, work for www.sciodev.com, the company is located in Mexico, we do outsourcing and nearshore development, we are focused on SaaS nearshore development, I started with VB.Net, but now I am ambidextrous using VB.Net or C#.

Comments and Discussions