/// <summary> /// Performs a schedulation. Called by timer. /// </summary> /// <param name="timerState"></param> protected override void Schedule(object timerState) { lock (this) { localTimerState.Counter++; Stop(); //Stop timer while scheduler works while (_cache.Count != 0) { SchedulerTask task = (SchedulerTask)_cache.GetKey(0); if (task.DueTime > DateTime.Now) { break; } try { ExecuteTask(task); } catch { //TODO: We have to apply some policy here... //i.e. Move failed tasks on a log, call a Module feedback interface,.... //now task is removed always } RemoveTask(task); } if (_cache.Count == 0) { FillCache(); if (_cache.Count == 0) { return; //avoid loop in case of empty tasks-queue in db. } Schedule(timerState); } Start(); //restart timer } }
/// <summary> /// Compare two tasks first order by dueTime. If dueTimes are equal they are ordered by IDTask. /// Used in sortedlist. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int Compare(object x, object y) { SchedulerTask xtsk = x as SchedulerTask; SchedulerTask ytsk = y as SchedulerTask; if (x == null || y == null) { throw new ArgumentException("object is not a SchedulerTask"); } if (xtsk.DueTime < ytsk.DueTime) { return(-1); } if (xtsk.DueTime > ytsk.DueTime) { return(1); } if (xtsk.DueTime == ytsk.DueTime) { if (xtsk.IDTask < ytsk.IDTask) { return(-1); } if (xtsk.IDTask > ytsk.IDTask) { return(1); } if (xtsk.IDTask == ytsk.IDTask) { return(0); } } throw new ArgumentException("Impossible exception"); //... to compile. }
/// <summary> /// Inserts the task. /// </summary> /// <param name="task">The task.</param> /// <returns>A int value...</returns> public int InsertTask(SchedulerTask task) { if (task.DueTime < DateTime.Now) { throw new SchedulerException("Cannot schedule an expired task"); } byte[] arg; using (MemoryStream ss = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ss, task.Argument); arg = ss.ToArray(); ss.Close(); } int idTask; _cn.Open(); using (IDbCommand cmd = _cn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "rb_SchedulerAddTask"; IDbDataParameter par; par = cmd.CreateParameter(); par.ParameterName = "@IDOwner"; par.DbType = DbType.Int32; par.Direction = ParameterDirection.Input; par.Value = task.IDModuleOwner; cmd.Parameters.Add(par); par = cmd.CreateParameter(); par.ParameterName = "@IDTarget"; par.DbType = DbType.Int32; par.Direction = ParameterDirection.Input; par.Value = task.IDModuleTarget; cmd.Parameters.Add(par); par = cmd.CreateParameter(); par.ParameterName = "@DueTime"; par.DbType = DbType.DateTime; par.Direction = ParameterDirection.Input; par.Value = task.DueTime; cmd.Parameters.Add(par); par = cmd.CreateParameter(); par.ParameterName = "@Description"; par.DbType = DbType.String; par.Size = 150; par.Direction = ParameterDirection.Input; par.Value = task.Description; cmd.Parameters.Add(par); par = cmd.CreateParameter(); par.ParameterName = "@Argument"; //par.DbType = DbType; par.Direction = ParameterDirection.Input; par.Value = arg; cmd.Parameters.Add(par); par = cmd.CreateParameter(); par.ParameterName = "@IDTask"; par.DbType = DbType.Int32; par.Direction = ParameterDirection.Output; cmd.Parameters.Add(par); cmd.ExecuteNonQuery(); idTask = (int)((IDataParameter)cmd.Parameters["@IDTask"]).Value; } _cn.Close(); if (idTask == -1) { throw new SchedulerException("Task add fail in DB"); } return(idTask); }