Exemple #1
0
        public void Start(GraphContextWrapper context)
        {
            this.currentContext  = context;
            this.MemoryVariables = new Dictionary <string, object>();
            GraphsContainer.UpdateStorageStateGraph(context, Enums.GraphStateEnum.STARTED);

            // Init connectors
            foreach (var x in this.Nodes.ToList().FindAll(x => x.Value.NodeBlockType == NodeTypeEnum.Connector))
            {
                try
                {
                    x.Value.SetupConnector();
                }
                catch (Exception ex)
                {
                    this.AppendLog("error", "Can't setup the connector : " + x.Value.FriendlyName + ", " + ex.Message);
                    this.Stop(true);
                    return;
                }
            }

            if (this.Nodes.ToList().FindAll(x => x.Value.IsEventNode).Count == 0)
            {
                try
                {
                    IsRunning = true;
                    this.ExecuteFromEntryPoint();
                    return;
                }
                catch (Exception ex)
                {
                    this.AppendLog("error", "Error when executing the graph : " + ex.Message);
                    this.Stop(true);
                    return;
                }
            }

            IsRunning = true;
            foreach (var x in this.Nodes.ToList().FindAll(x => x.Value.IsEventNode))
            {
                try
                {
                    x.Value.SetupEvent();
                }
                catch (Exception ex)
                {
                    this.AppendLog("error", "Can't setup the event : " + x.Value.FriendlyName + ", " + ex.Message);
                    this.Stop(true);
                    return;
                }
            }
            this.ExecuteFromEntryPoint();
        }
        public static void UpdateGraphInStorage(GraphContextWrapper graphContext, bool engineInit)
        {
            try
            {
                var graphStorage = RedisStorage.SetGraphStorage(graphContext.graph,
                                                                graphContext.walletIdentifier, graphContext.currentGraphState);

                // to avoid redis update on each started graph at engine init
                if (!engineInit)
                {
                    UpdateAliveStorage();
                }
            }
            catch (Exception error)
            {
                logger.Error(error);
            }
        }
        public static bool AddNewGraph(BlockGraph graph,
                                       int walletIdentifier, GraphStateEnum initialState = GraphStateEnum.STOPPED, bool engineInit = false)
        {
            try
            {
                // lock once to check if the graph is already loaded
                lock (mutex)
                {
                    if (_graphs.ContainsKey(graph.UniqueHash))
                    {
                        RestartLoadedGraph(graph, walletIdentifier, initialState);
                        return(true);
                    }
                }

                new Thread(new ThreadStart(() =>
                {
                    // init the thread and init his context
                    lock (mutex)
                    {
                        if (_graphs.ContainsKey(graph.UniqueHash))
                        {
                            return;
                        }
                        //create and save the graph in the hashmap
                        var wrapper = new GraphContextWrapper(graph, walletIdentifier, initialState);
                        _graphs.Add(graph.UniqueHash, wrapper);

                        // update graph context in database
                        UpdateGraphInStorage(wrapper, engineInit);

                        // start the graph execution context
                        wrapper.InitContext(engineInit);
                    }
                })).Start();

                return(true);
            }
            catch (Exception error)
            {
                logger.Error(error);
                return(false);
            }
        }
        public static void UpdateStorageStateGraph(GraphContextWrapper context, GraphStateEnum newState)
        {
            try
            {
                context.currentGraphState = newState;
                var graphStorage = RedisStorage.SetGraphStorage(context.graph,
                                                                context.walletIdentifier, newState);
                if (context.graph != null && context.graph.IsRunning &&
                    newState == GraphStateEnum.STOPPED)
                {
                    context.graph.Stop();
                }

                UpdateAliveStorage();
            }
            catch (Exception error)
            {
                logger.Error(error);
            }
        }
        public static void RestartLoadedGraph(BlockGraph newGraph,
                                              int walletIdentifier, GraphStateEnum initialState)
        {
            try
            {
                GraphContextWrapper currentlyRunning = _graphs[newGraph.UniqueHash];
                if (currentlyRunning == null)
                {
                    throw new Exception("Invalid graph loaded in memory pool.");
                }
                UpdateStorageStateGraph(currentlyRunning, GraphStateEnum.RESTARTING);
                logger.Info("Reloading Graph hash {0}", newGraph.UniqueHash);
                if (currentlyRunning.graph != null && currentlyRunning.graph.IsRunning)
                {
                    currentlyRunning.graph.Stop();
                }
            }
            catch (Exception error)
            {
                logger.Error(error);
            }
            finally
            {
                Task.Run(() =>
                {
                    // wait for the end of current execution
                    while (_graphs[newGraph.UniqueHash].graph.IsRunning)
                    {
                        Thread.Sleep(1000);
                    }
                    ;

                    newGraph.AppendLog("warn", string.Format("Graph hash {0} stopped successfully, restarting...", newGraph.UniqueHash));
                    logger.Info("Graph hash {0} stopped successfully, restarting...", newGraph.UniqueHash);
                    _graphs.Remove(newGraph.UniqueHash);
                    AddNewGraph(newGraph, walletIdentifier, GraphStateEnum.STARTING);
                });
            }
        }