Session() { This_ = this; Log.Writer.Exitig += ThreadLog_Exitig; input_item_type_name2input_item_types = (from t in Assembly.GetEntryAssembly().GetTypes() where t.BaseType == typeof(InputItem) select t).ToDictionary(t => t.Name, t => t); Cliver.Bot.InputItem.Initialize(input_item_type_name2input_item_types.Values.ToList()); work_item_type_name2work_item_types = (from t in Assembly.GetEntryAssembly().GetTypes() where (t.BaseType == typeof(WorkItem) && t.Name != typeof(SingleValueWorkItem <>).Name) || (t.BaseType != null && t.BaseType.Name == typeof(SingleValueWorkItem <>).Name) select t).ToDictionary(t => t.Name, t => t); Cliver.Bot.WorkItem.Initialize(work_item_type_name2work_item_types.Values.ToList()); //tag_item_type_name2tag_item_types = (from t in Assembly.GetEntryAssembly().GetTypes() where (t.BaseType == typeof(TagItem) && t.Name != typeof(SingleValueTagItem<>).Name) || (t.BaseType != null && t.BaseType.Name == typeof(SingleValueTagItem<>).Name) select t).ToDictionary(t => t.Name, t => t); tag_item_type_name2tag_item_types = (from t in Assembly.GetEntryAssembly().GetTypes() where t.BaseType == typeof(TagItem) select t).ToDictionary(t => t.Name, t => t); Cliver.Bot.TagItem.Initialize(tag_item_type_name2tag_item_types.Values.ToList()); if (input_item_type_name2input_item_types.Count < 1) { throw new Exception("No InputItem derive was found"); } workflow_xtw = new XmlTextWriter(Log.SessionDir + "\\" + STATES_FILE_NAME, Encoding.UTF8); workflow_xtw.Formatting = Formatting.Indented; workflow_xtw.WriteStartDocument(); workflow_xtw.WriteStartElement("Session"); if (Properties.General.Default.WriteSessionRestoringLog) { items_xtw = new XmlTextWriter(Log.SessionDir + "\\" + ITEMS_FILE_NAME, Encoding.UTF8); items_xtw.Formatting = Formatting.Indented; items_xtw.WriteStartDocument(); items_xtw.WriteStartElement("Items"); } Restored = false; if (Properties.General.Default.RestoreBrokenSession && !ProgramRoutines.IsParameterSet(CommandLineParameters.NOT_RESTORE_SESSION)) { Restored = this.restore(ref StartTime); if (This == null) { return; } } if (!Restored) { StartTime = DateTime.Now; Log.Main.Write("No session was restored so reading input Items from the input file"); read_input_file(); } try { CustomizationApi.SessionCreating(); } catch (Exception e) { LogMessage.Error("SessionCreating: " + Log.GetExceptionMessage(e)); Close(); return; } set_session_state(SessionState.STARTED, "session_start_time", StartTime.ToString("yyyy-MM-dd HH:mm:ss")); }
void read_input_file() { Type start_input_item_type = (from t in Assembly.GetEntryAssembly().GetTypes() where t.IsSubclassOf(typeof(InputItem)) && !t.IsGenericType select t).First(); InputItemQueue start_input_item_queue = GetInputItemQueue(start_input_item_type.Name); CustomizationApi.FillStartInputItemQueue(start_input_item_queue, start_input_item_type); }
void bot_cycle() { try { typeof(BotCycle).GetField("Id", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, Log.Id); lock (id2bot_cycles) { id2bot_cycles[Id] = this; } if (Created != null) { Created.Invoke(Id); } bot = CustomizationApi.CreateBot(); if (bot == null) { throw (new Exception("Could not create Bot instance.")); } typeof(Bot).GetField("BotCycle", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(bot, this); Counter processor_errors = new Counter("processor_errors", Properties.General.Default.MaxProcessorErrorNumber); bot.CycleBeginning(); while (run) { current_item = Session.This.GetNext(); if (current_item == null) { return; } InputItemState state = InputItemState.COMPLETED; try { current_item.PROCESSOR(this); } catch (ThreadAbortException) { return; } catch (Exception e) { if (e is TargetInvocationException) { e = e.InnerException; } if (e is ProcessorException) { switch (((ProcessorException)e).Type) { case ProcessorExceptionType.ERROR: state = InputItemState.ERROR; break; case ProcessorExceptionType.RESTORE_AS_NEW: state = InputItemState.ERROR_RESTORE_AS_NEW; Session.This.IsItem2Restore = true; break; case ProcessorExceptionType.COMPLETED: break; default: throw new Exception("No case for " + ((ProcessorException)e).Type.ToString()); } } else { state = InputItemState.ERROR; } Log.Error(e); } current_item.__State = state; if (state == InputItemState.ERROR || state == InputItemState.ERROR_RESTORE_AS_NEW) { processor_errors.Increment(); } else { processor_errors.Reset(); } Start(); } bot.CycleFinishing(); } catch (ThreadAbortException) { } catch (Exception e) { LogMessage.Exit(e); } finally { close_thread(Id); } }
void close() { lock (This_) { BotCycle.Abort(); if (This.items_xtw != null) { This.items_xtw.WriteEndElement(); This.items_xtw.WriteEndDocument(); This.items_xtw.Close(); } if (This.input_item_queue_name2input_item_queues.Count > 0) { if (This.IsUnprocessedInputItem) { This.set_session_state(SessionState.ABORTED); } else if (This.IsItem2Restore) { This.set_session_state(SessionState.UNCOMPLETED); } else { This.set_session_state(SessionState.COMPLETED); } } This.workflow_xtw.WriteEndElement(); This.workflow_xtw.WriteEndDocument(); This.workflow_xtw.Close(); try { CustomizationApi.SessionClosing(); } catch (Exception e) { LogMessage.Error(e); } try { if (Closing != null) { Closing.Invoke(); } } catch (Exception e) { LogMessage.Error(e); } InputItemQueue.Close(); FileWriter.ClearSession(); Cache.ClearSession(); Proxies.ClearSession(); WebRoutine.ClearSession(); Log.Main.Write("Closing session."); Cliver.Log.ClearSession(); This_ = null; } }
//internal static CacheInfo GetCacheInfo(string url) //{ // lock (static_lock_variable) // { // if (url == null) // return null; // return (CacheInfo)cache_map[url]; // } //} /// <summary> /// /// </summary> /// <returns>false if cashe was not restored due to any reason</returns> static bool restore_cache() { lock (static_lock_variable) { try { cache_map = new Dictionary <string, CacheInfo>(); custom_cache = (ICustomCache)CustomizationApi.CreateCustomCache(); DirectoryInfo di = new DirectoryInfo(Log.WorkDir); DirectoryInfo[] session_dis = di.GetDirectories("Session*", SearchOption.TopDirectoryOnly); Array.Sort(session_dis, new Session.CompareDirectoryInfo()); for (int i = session_dis.Length - 1; i >= 0; i--) { DirectoryInfo d = session_dis[i]; if (!Directory.Exists(d.FullName + "\\" + Log.DownloadDirName)) { continue; } if (d.FullName == Log.SessionDir) { continue; } string cm_file = d.FullName + "\\" + Log.DownloadDirName + "\\" + CACHE_MAP_FILE_NAME; if (!File.Exists(cm_file)) { Log.Main.Error("Could not open cache map: " + d.FullName + "\\" + CACHE_MAP_FILE_NAME); continue; } try { XmlTextReader sr = new XmlTextReader(cm_file); string session_name = Regex.Replace(d.FullName, @".*[\/\\](?=.+)", "", RegexOptions.Compiled | RegexOptions.Singleline); while (sr.Read()) { if (sr.NodeType == XmlNodeType.Element && sr.Name == "File") { string url = sr.GetAttribute("url"); string response_url = sr.GetAttribute("response_url"); string path = sr.GetAttribute("path"); if (!path.Contains(session_name)) { path = "\\" + session_name + "\\" + Log.DownloadDirName + "\\" + path; } bool text; if (!bool.TryParse(sr.GetAttribute("text"), out text)) { text = true; } if (DoNotRestoreErrorFiles) { string error = sr.GetAttribute("error"); if (error != null) { continue; } } add2cache_map(url, path, response_url); } } } catch (XmlException e) { if (!e.Message.Contains("Unexpected end of file has occurred.")) { Log.Main.Error(e); } } } return(true); } catch (Exception e) { LogMessage.Exit(e); } return(false); } }