public static void StartEvent(AMSQueueItem message, CancellationToken cancellationToken) { AMSEvent eventData = null; try { eventData = AMSEventSqlAdapter.Instance.LoadByID(message.ResourceID); if (eventData != null) { if (eventData.State == AMSEventState.NotStart) AMSEventSqlAdapter.Instance.UpdateState(eventData.ID, AMSEventState.Starting); AMSEventChannelCollection ecs = AMSEventSqlAdapter.Instance.LoadEventAndChannel(eventData.ID); Task[] startChannelTasks = new Task[ecs.Count]; for (int i = 0; i < startChannelTasks.Length; i++) { AMSEventChannel ec = ecs[i]; startChannelTasks[i] = Task.Factory.StartNew(() => StartOneChannelAndCreateProgram(eventData, ec, cancellationToken)); } Task.WaitAll(startChannelTasks); AMSEventSqlAdapter.Instance.UpdateRunningStateByChannels(eventData.ID); } } finally { if (eventData != null) LockHelper.Unlock(eventData); } }
public static void GenerateDeleteProgramMessages(CancellationToken cancellationToken) { AMSQueueItem message = new AMSQueueItem(); message.ItemType = AMSQueueItemType.DeleteProgram; GetQueue().AddMessages(string.Empty, message); }
public static void DeleteProgram(AMSQueueItem message, CancellationToken cancellationToken) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60015, "Delete expired programs"); int count = LiveChannelManager.DeleteAllExpiredPrograms(AMSWorkerSettings.GetConfig().Durations.GetDuration("programExpireTime", TimeSpan.FromDays(1))); TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60015, "{0} programs deleted", count); }
public static void GenerateSyncChannelInfoMessages(CancellationToken cancellationToken) { AMSQueueItem message = new AMSQueueItem(); message.ItemType = AMSQueueItemType.SyncChannelInfo; GetQueue().AddMessages(string.Empty, message); }
private static AMSQueueItem CreateMessage() { AMSQueueItem message = new AMSQueueItem(); message.ResourceID = UuidHelper.NewUuidString(); message.ItemType = AMSQueueItemType.StartEvent; return(message); }
private static AMSQueueItem CreateMessage() { AMSQueueItem message = new AMSQueueItem(); message.ResourceID = UuidHelper.NewUuidString(); message.ItemType = AMSQueueItemType.StartEvent; return message; }
public static void SyncChannelInfo(AMSQueueItem message, CancellationToken cancellationToken) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60013, "Start Sync Channel Info"); AMSChannelCollection channels = LiveChannelManager.GetAllChannels(true); AMSChannelSqlAdapter.Instance.UpdateAllChannels(channels); TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60013, "Complete Sync Channel Info"); }
public static string AddFakeStatEvent() { AMSQueueItem message = new AMSQueueItem(); message.ResourceID = UuidHelper.NewUuidString(); message.ItemType = AMSQueueItemType.StatEventViews; IQueue<AMSQueueItem> queue = QueueManager.GetQueue<AMSQueueItem>("amsQueue"); queue.AddMessages(string.Empty, message); return message.ResourceID; }
private static void RunQueueTask(AMSQueueItem message, CancellationToken cancellationToken, Action <AMSQueueItem, CancellationToken> action) { Task.Run(() => { try { action(message, cancellationToken); } catch (System.Exception ex) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Error, 60014, ex.ToString()); } }); }
/// <summary> /// 启动节目 /// </summary> /// <param name="message"></param> /// <param name="cancellationToken"></param> public static void StartProgram(AMSQueueItem message, CancellationToken cancellationToken) { AMSEvent eventData = null; try { eventData = AMSEventSqlAdapter.Instance.LoadByID(message.ResourceID); if (eventData != null) { if (eventData.State == AMSEventState.Starting) { AMSEventChannelCollection ecs = AMSEventSqlAdapter.Instance.LoadEventAndChannel(eventData.ID); AMSChannelCollection channels = AMSChannelSqlAdapter.Instance.LoadByInBuilder(builder => builder.AppendItem(ecs.ToChannelIDs()), "ID"); if (channels.AllRunning()) { //LockHelper.ExtendLockTime(eventData); Task[] startProgramTasks = new Task[ecs.Count]; for (int i = 0; i < startProgramTasks.Length; i++) { AMSEventChannel ec = ecs[i]; AMSChannel channel = channels.Find(c => c.ID == ec.ChannelID); if (channel != null) startProgramTasks[i] = Task.Factory.StartNew(() => InnerStartProgram(eventData, channel, ec, cancellationToken)); } Task.WaitAll(startProgramTasks); AMSEventSqlAdapter.Instance.UpdateRunningStateByChannels(eventData.ID); } } } } finally { if (eventData != null) LockHelper.Unlock(eventData); } }
public static void StopChannel(AMSQueueItem message, CancellationToken cancellationToken) { AMSChannel channel = AMSChannelSqlAdapter.Instance.LoadByID(message.ResourceID); if (channel != null) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60014, "Stop Channel:\n{0}", channel.ToTraceInfo()); if (channel.State == AMSChannelState.Stopped) { AMSChannelSqlAdapter.Instance.UpdateState(channel.ID, AMSChannelState.Stopping); } SimulateOrExecuteAction(() => LiveChannelManager.StopChannel(channel), () => channel.State = AMSChannelState.Stopped); AMSChannelSqlAdapter.Instance.Update(channel); TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60014, "Channel Stopped:\n{0}", channel.ToTraceInfo()); } }
public static void GenerateStopChannelMessages(CancellationToken cancellationToken) { AMSWorkerSettings settings = AMSWorkerSettings.GetConfig(); AMSChannelCollection channels = AMSChannelSqlAdapter.Instance.LoadNeedStopChannels(settings.Durations.GetDuration("stopChannelLeadTime", TimeSpan.FromHours(1))); AMSQueueItemCollection messages = new AMSQueueItemCollection(); foreach (AMSChannel channel in channels) { AMSQueueItem message = new AMSQueueItem(); message.ItemType = AMSQueueItemType.StopChannel; message.ResourceID = channel.ID; message.ResourceName = channel.Name; messages.Add(message); } GetQueue().AddMessages(string.Empty, messages.ToArray()); }
public static void StopEvent(AMSQueueItem message, CancellationToken cancellationToken) { AMSEvent eventData = null; try { eventData = AMSEventSqlAdapter.Instance.LoadByID(message.ResourceID); if (eventData != null) { if (eventData.State == AMSEventState.Running) { AMSEventSqlAdapter.Instance.UpdateState(eventData.ID, AMSEventState.Stopping); } AMSEventChannelCollection ecs = AMSEventSqlAdapter.Instance.LoadEventAndChannel(eventData.ID); Task[] stopProgramTasks = new Task[ecs.Count]; for (int i = 0; i < stopProgramTasks.Length; i++) { AMSEventChannel ec = ecs[i]; stopProgramTasks[i] = Task.Factory.StartNew(() => StopOneProgram(eventData, ec, cancellationToken)); } Task.WaitAll(stopProgramTasks.ToArray()); AMSEventSqlAdapter.Instance.UpdateCompletedStateByChannels(eventData.ID); } } finally { if (eventData != null) { LockHelper.Unlock(eventData); } } }
public static void ReadQueue(CancellationToken cancellationToken) { AMSQueueItem message = GetQueue().GetMessages(string.Empty).SingleOrDefault(); if (message != null) { //Trace.TraceInformation("Message: ID={0}, ResourceID={1}, Name={2}, ItemType={3}", // message.ID, message.ResourceID, message.ResourceID, message.ItemType); if (AMSWorkerSettings.GetConfig().ItemTypes.IsEnabled(message.ItemType)) { switch (message.ItemType) { case AMSQueueItemType.StartEvent: RunQueueTask(message, cancellationToken, AMSOperations.StartEvent); break; case AMSQueueItemType.StopEvent: RunQueueTask(message, cancellationToken, AMSOperations.StopEvent); break; case AMSQueueItemType.SyncChannelInfo: RunQueueTask(message, cancellationToken, AMSOperations.SyncChannelInfo); break; case AMSQueueItemType.StopChannel: RunQueueTask(message, cancellationToken, AMSOperations.StopChannel); break; case AMSQueueItemType.DeleteProgram: RunQueueTask(message, cancellationToken, AMSOperations.DeleteProgram); break; } } } }
public static void StopChannel(AMSQueueItem message, CancellationToken cancellationToken) { AMSChannel channel = AMSChannelSqlAdapter.Instance.LoadByID(message.ResourceID); if (channel != null) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60014, "Stop Channel:\n{0}", channel.ToTraceInfo()); if (channel.State == AMSChannelState.Stopped) AMSChannelSqlAdapter.Instance.UpdateState(channel.ID, AMSChannelState.Stopping); SimulateOrExecuteAction(() => LiveChannelManager.StopChannel(channel), () => channel.State = AMSChannelState.Stopped); AMSChannelSqlAdapter.Instance.Update(channel); TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60014, "Channel Stopped:\n{0}", channel.ToTraceInfo()); } }
private static void RunQueueTask(AMSQueueItem message, CancellationToken cancellationToken, Action<AMSQueueItem, CancellationToken> action) { Task.Run(() => { try { action(message, cancellationToken); } catch (System.Exception ex) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Error, 60014, ex.ToString()); } }); }
public static void UpdateViewCount(AMSQueueItem message, CancellationToken cancellationToken) { TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60015, "Complete Update Event View Count: {0}", message.ResourceID); AMSEventSqlAdapter.Instance.UpdateViewCount(message.ResourceID, AMSWorkerSettings.GetConfig().Durations.GetDuration("onlinePeriod", TimeSpan.FromMinutes(5))); TraceHelper.AMSTaskTraceSource.TraceEvent(TraceEventType.Verbose, 60015, "End Update Event View Count: {0}", message.ResourceID); }
public static AMSEventCollection StartAllPrograms() { AMSEventCollection events = AMSEventSqlAdapter.Instance.Load(builder => builder.AppendItem("State", AMSEventState.Starting.ToString())); AMSQueueItemCollection messages = new AMSQueueItemCollection(); foreach (AMSEvent eventData in events) { AMSQueueItem message = new AMSQueueItem(); message.ResourceID = eventData.ID; message.ItemType = AMSQueueItemType.StartProgram; messages.Add(message); } IQueue<AMSQueueItem> queue = QueueManager.GetQueue<AMSQueueItem>("amsQueue"); queue.AddMessages(string.Empty, messages.ToArray()); return events; }