private static bool CorrectTime(Sheduler sheduler) { bool cng = false; if (sheduler == null) { return(false); } if (!sheduler.NextRun.HasValue) { return(false); } if (sheduler.NextRun.Value <= DateTime.Now) { sheduler.UpdateShedule(); cng = true; } return(cng); }
private void InstanceTripped(SchedulingEventArg arg) { lock (_heart) { Sheduler sheduler = null; using ( ISession session = NHibernateHelper.SessionManager.GetSessionFor <WebEnvironmentFiact>().SessionFactory.OpenSession() ) { try { sheduler = session.Get <TSheduler>(_id); if (sheduler == null) { arg.Interval = -1; return; } if (_cancelTokSource != null) { arg.Interval = -1; return; } State = TaskExecutionType.Running; sheduler.LastRun = _heart.LastRun; sheduler.Iteration = _heart.CountLaunches; _cancelTokSource = new CancellationTokenSource(); _status = new StatusTask { StartRun = DateTime.Now, EndRun = null, Error = null, Sheduler = session.Get <Sheduler>(Id), TaskExecType = TaskExecType.Running }; session.Save(_status); _recId = _status.Id; var keyPair = new KeyValuePair <int, CancellationToken>(_status.Id, _cancelTokSource.Token); Task task = null; try { task = Task.Factory.StartNew(x => { var key = (KeyValuePair <int, CancellationToken>)x; if (!key.Value.IsCancellationRequested) { var t = new Thread(DoWork); t.Start(key.Key); while (t.IsAlive && !key.Value.IsCancellationRequested) { } if (t.IsAlive) { t.Abort(); } } }, keyPair, _cancelTokSource.Token); if (sheduler.IsKill && sheduler.Runtime.HasValue) { task.Wait(new TimeSpan(0, sheduler.Runtime.Value, 0)); if (task.IsCompleted && _cancelTokSource != null && !_cancelTokSource.IsCancellationRequested) { State = TaskExecutionType.Succeed; } else if (task.Status != TaskStatus.Canceled && task.Status != TaskStatus.RanToCompletion && task.Status != TaskStatus.Faulted) { _cancelTokSource.Cancel(); State = TaskExecutionType.Failure; } else { State = TaskExecutionType.Failure; } } else { task.Wait(); if (task.IsCompleted && _cancelTokSource != null && !_cancelTokSource.IsCancellationRequested) { State = TaskExecutionType.Succeed; } else { State = TaskExecutionType.Failure; } } Task.WaitAll(new[] { task }); if (task.Status != TaskStatus.Canceled && task.Status != TaskStatus.RanToCompletion && task.Status != TaskStatus.Faulted) { task.Dispose(); } } catch (InvalidOperationException) { State = TaskExecutionType.Failure; if (task != null) { Task.WaitAll(new[] { task }); } } catch (AggregateException) { State = TaskExecutionType.Failure; if (task != null) { Task.WaitAll(new[] { task }); } } catch (OperationCanceledException) { State = TaskExecutionType.Failure; if (task != null) { Task.WaitAll(new[] { task }); } } catch (ThreadAbortException) { State = TaskExecutionType.Failure; Thread.ResetAbort(); if (task != null) { Task.WaitAll(new[] { task }); } } finally { if (task != null && task.Status != TaskStatus.RanToCompletion) { Task.WaitAll(new[] { task }); if (task.Status != TaskStatus.Canceled && task.Status != TaskStatus.Faulted) { task.Dispose(); } } } } catch { State = TaskExecutionType.Failure; } finally { if (sheduler != null) { if (_status != null) { session.Refresh(_status); _status = session.Get <StatusTask>(_recId); if (_status.TaskExecType != TaskExecType.Failure) { _status.TaskExecType = (TaskExecType)Enum.Parse(typeof(TaskExecType), ((int)State).ToString()); _status.EndRun = DateTime.Now; session.Save(_status); } } sheduler.Duration = _heart.LastDuration; sheduler.UpdateShedule(); DateTime?next = sheduler.NextRun; arg.Interval = !next.HasValue ? -1 : (long)next.Value.Subtract(DateTime.Now).TotalMilliseconds; if (IsRemove) { arg.Interval = -1; } sheduler.IsEnabled = next.HasValue; session.Save(sheduler); session.Flush(); _cancelTokSource = null; _status = null; } } } } }
public ActionResult Index(SchedulerModel model) { var session = Db <WebEnvironmentFiact> .NSession; if (model.Id == 0) { switch (model.Type) { case "import": _shaper = new ImportSheduling(); break; case "export": _shaper = new ExportSheduling(); break; default: _shaper = new MoreSheduling(); break; } } else { _shaper = session.Get <Sheduler>(model.Id); try { using (var factory = new ChannelFactory <IImportAxapta>("MyImportAxapta")) { IImportAxapta channel = factory.CreateChannel(); var id = _shaper.Id; channel.RemoveShedule(id); } Thread.Sleep(2000); }catch (Exception e) { } } _shaper.IsEnabled = model.IsEnabled; _shaper.IsKill = model.IsKill; _shaper.Runtime = model.Runtime; _shaper.Param = model.Param; _shaper.TimeGetData = model.TimeGetData; _shaper.Name = model.Name; SheduleFrequencyProperty oldFreq = _shaper.Frequency; session.Delete(oldFreq); _shaper.Frequency = ApplySheduling(model); _shaper.UpdateShedule(); model = LoadSheduling(_shaper.Frequency); model.IsEnabled = _shaper.IsEnabled; model.Name = _shaper.Name; if (_shaper.Frequency is SingleLanchFrequency) { model.FrequencyType = 0; } else { model.FrequencyType = 1; } model.Id = _shaper.Id; model.Runtime = _shaper.Runtime; model.IsKill = _shaper.IsKill; model.Param = _shaper.Param; model.TimeGetData = _shaper.TimeGetData; LoadEntity(model); ModelState.Clear(); session.Save(_shaper); session.Flush(); if (_shaper.IsEnabled) { try { using (var factory = new ChannelFactory <IImportAxapta>("MyImportAxapta")) { IImportAxapta channel = factory.CreateChannel(); channel.AddShedule(_shaper.Id); } }catch (Exception e) { } } return(View(model)); }