Example #1
0
        Session()
        {
            This_ = this;

            ThreadLog.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"));
        }
Example #2
0
        void close()
        {
            lock (This_)
            {
                try
                {
                    Log.Main.Write("Closing the bot session: " + Session.State.ToString());
                    BotCycle.Abort();

                    if (This.IsUnprocessedInputItem)
                        State = SessionState.BROKEN;
                    else if (This.IsItem2Restore)
                        State = SessionState.UNCOMPLETED;
                    else
                        State = SessionState.COMPLETED;

                    This.Storage.WriteState(State, new { });

                    try
                    {
                        __Closing();
                    }
                    catch (Exception e)
                    {
                        Session.State = SessionState.FatalError;
                        This.Storage.WriteState(State, new { });
                        LogMessage.Error(e);
                        __FatalError(e.Message);
                    }

                    try
                    {
                        Closing?.Invoke();
                    }
                    catch (Exception e)
                    {
                        Session.State = SessionState.FatalError;
                        This.Storage.WriteState(State, new { });
                        LogMessage.Error(e);
                        __FatalError(e.Message);
                    }

                    InputItemQueue.Close();
                    FileWriter.ClearSession();
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception e)
                {
                    Session.State = SessionState.FatalError;
                    This.Storage.WriteState(State, new { });
                    LogMessage.Error(e);
                    __FatalError(e.Message);
                }
                finally
                {
                    Storage.Close();
                    switch (State)
                    {
                        case SessionState.NULL:
                        case SessionState.STARTING:
                        case SessionState.COMPLETED:
                        case SessionState.FatalError:
                            Directory.Move(Dir, Dir + "_" + TimeMark + "_" + State);
                            break;
                        case SessionState.RESTORING:
                        case SessionState.RUNNING:
                        case SessionState.CLOSING:
                        case SessionState.UNCOMPLETED:
                        case SessionState.BROKEN:
                            break;
                        default:
                            throw new Exception("Unknown option: " + State);
                    }
                    This_ = null;
                    Cliver.Log.ClearSession();
                }
            }

            try
            {
                Closed?.Invoke();
            }
            catch (Exception e)
            {
                LogMessage.Error(e);
                __FatalError(e.Message);
            }
        }
Example #3
0
        protected Session()
        {
            This_ = this;
            State = SessionState.STARTING;

            Log.Main.Inform("Loading configuration from " + Config.DefaultStorageDir);
            Config.Reload(Config.DefaultStorageDir);

            ConfigurationDir = Dir + "\\" + Config.CONFIG_FOLDER_NAME;

            Restored = false;
            Storage = new SessionStorage();
            DateTime old_start_time;
            string old_time_mark;
            SessionState old_state = Storage.ReadLastState(out old_start_time, out old_time_mark);
            switch (old_state)
            {
                case SessionState.NULL:
                    break;
                case SessionState.STARTING:
                case SessionState.COMPLETED:
                case SessionState.FatalError:
                    break;
                case SessionState.RESTORING:
                case SessionState.RUNNING:
                case SessionState.CLOSING:
                case SessionState.UNCOMPLETED:
                case SessionState.BROKEN:
                    if (Settings.Engine.RestoreBrokenSession && !ProgramRoutines.IsParameterSet(CommandLineParameters.NOT_RESTORE_SESSION))
                    {
                        if (LogMessage.AskYesNo("Previous session " + old_time_mark + " is not completed. Restore it?", true))
                        {
                            StartTime = old_start_time;
                            TimeMark = old_time_mark;
                            Storage.WriteState(SessionState.RESTORING, new { restoring_time = RestoreTime, restoring_session_time_mark = get_time_mark(RestoreTime) });
                            Log.Main.Inform("Loading configuration from " + ConfigurationDir);
                            Config.Reload(ConfigurationDir);
                            Storage.RestoreSession();
                            Restored = true;
                        }
                    }
                    break;
                default:
                    throw new Exception("Unknown option: " + old_state);
            }

            if (!Restored)
            {
                if (old_state != SessionState.NULL)
                {
                    string old_dir_new_path = Log.WorkDir + "\\Data" + "_" + old_time_mark + "_" + old_state;
                    Log.Main.Write("The old session folder moved to " + old_dir_new_path);
                    Storage.Close();
                    if(Directory.Exists(Dir))
                        Directory.Move(Dir, old_dir_new_path);
                    PathRoutines.CreateDirectory(Dir);
                    Storage = new SessionStorage();
                }

                StartTime = Log.MainSession.CreatedTime;// DateTime.Now;
                TimeMark = get_time_mark(StartTime);
                Storage.WriteState(SessionState.STARTING, new { session_start_time = StartTime, session_time_mark = TimeMark });
                read_input_file();
                Config.CopyFiles(Dir);
            }

            Creating?.Invoke();

            __Creating();
        }
Example #4
0
        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();
                Log.ClearSession();
                FileWriter.ClearSession();
                Cache.ClearSession();
                Proxies.ClearSession();
                WebRoutine.ClearSession();

                if (Cliver.Bot.Program.Mode == Cliver.Bot.Program.ProgramMode.AUTOMATIC)
                {
                    try
                    {
                        Environment.Exit(0);
                    }
                    catch
                    { }
                }

                This_ = null;
            }
        }