public GraphContextWrapper(BlockGraph _graph, int _walletIdentifier,
                                   GraphStateEnum _initialState)
        {
            this.graph             = _graph;
            this.walletIdentifier  = _walletIdentifier;
            this.currentGraphState = _initialState;

            this.context   = Thread.CurrentThread;
            this.StoppedAt = null;
        }
        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);
                });
            }
        }
示例#5
0
        public static GraphStorage SetGraphStorage(BlockGraph graph, int walletIdentifier, GraphStateEnum stateGraph)
        {
            string hash    = GraphCompression.GetUniqueGraphHash(walletIdentifier, graph.CompressedRaw);
            var    storage = new GraphStorage()
            {
                StoredHash            = hash,
                CompressedBytes       = graph.CompressedRaw,
                WalletIdentifierOwner = walletIdentifier,
                StateGraph            = stateGraph
            };

            var conn = muxer.GetDatabase(0);

            conn.StringSet(string.Format("graphs/{0}", hash), JsonConvert.SerializeObject(storage));
            return(storage);
        }