コード例 #1
0
        public void TestSyncQueueInvoke()
        {
            var syncQueue = new DacTaskResultConsumerQueue(ConsumeType.Sync);

            syncQueue.Enqueue(DacTaskResultConsumerService.GetConsumer("C1"));
            syncQueue.Enqueue(DacTaskResultConsumerService.GetConsumer("C2"));
            syncQueue.Enqueue(DacTaskResultConsumerService.GetConsumer("C3"));

            DacTaskResultConsumerService.AddComsumerQueue(syncQueue);

            var rslt1 = new List <SensorAcqResult>();

            rslt1.Add(new SensorAcqResult()
            {
                Data = new SensorData(new double[] { 65, 45, 87 }, new double[] { 12, 23, 56 }, new double[] { 65, 45, 87 })
            });
            rslt1[0].ErrorCode = 1;
            DacTaskResultConsumerService.OnDacTaskResultProduced(rslt1);
            //Thread.Sleep(8000);
            Console.WriteLine("source: {0}", rslt1[0].ErrorCode);
            Assert.AreNotEqual(7, rslt1[0].ErrorCode);


            //var rslt2 = new DACTaskResult();
            //rslt2.ErrorMsg = "队列2";
            //DacTaskResultConsumerService.OnDacTaskResultProduced(rslt2);
            ////Thread.Sleep(8000);
            //Console.WriteLine("source: {0}", rslt2.ErrorMsg);
            //Assert.AreNotEqual("C---1C---2C---3", rslt2.ErrorMsg);
            Thread.Sleep(5000);
        }
コード例 #2
0
ファイル: EtController.cs プロジェクト: icprog/FS-SMISCloud
        public void DoFileParseWork()
        {
            var files = new DirectoryInfo(GlobalConfig.DataSourcePath).GetFiles();

            if (files.Length == 0)
            {
                return;
            }
            Log.InfoFormat("{0} files is found to parse, start..", files.Length);
            var sw = new Stopwatch();

            sw.Start();
            int count = 0;

            foreach (FileInfo file in files)
            {
                try
                {
                    lock (FileLock)
                    {
                        if (ProcessiongFiles.Exists(f => f == file.FullName))
                        {
                            return;
                        }

                        ProcessiongFiles.Add(file.FullName);
                    }

                    var data     = new List <SensorAcqResult>();
                    var hasError = false;
                    lock (LockObj)
                    {
                        if (IsExiting)
                        {
                            return;
                        }
                    }
                    Log.InfoFormat("parsing file: {0}", file.FullName);
                    // 扫描数据
                    using (var fs = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        using (var sr = new StreamReader(fs))
                        {
                            var line = 1;
                            while (sr.Peek() > -1)
                            {
                                string s      = sr.ReadLine();
                                var    parser = new JsonParser();
                                try
                                {
                                    var rslt = parser.Parse(s);
                                    if (this.dataStatusJudge.JudgeDataStatusIsOk(rslt))
                                    {
                                        var sensorId = rslt.Sensor.SensorID;
                                        rslt.Sensor = DbAccessor.DbConfigAccessor.GetSensorInfo(sensorId);
                                        data.Add(rslt);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log.Error(
                                        string.Format("file: {0},line: {1},json parse error", file.Name, line),
                                        e);
                                    hasError = true;
                                }
                                line++;
                            }
                        }
                        var filePath = file.FullName;
                        var fileName = file.Name;
                        lock (LockObj)
                        {
                            if (IsExiting)
                            {
                                return;
                            }
                            Log.InfoFormat("parsing file: {0} end, send to consumers", file.FullName);
                            var consumeTask = new Task(
                                () =>
                            {
                                var task = DacTaskResultConsumerService.OnDacTaskResultProduced(data);
                                task.ContinueWith(
                                    t =>
                                {
                                    hasError = hasError || t.Exception != null;
                                    ArchiveDataFile(hasError, fileName, filePath);
                                    Log.InfoFormat("consume file: {0} end", filePath);
                                    lock (FileLock)
                                    {
                                        ProcessiongFiles.Remove(filePath);
                                    }
                                }).Wait();
                            });
                            tasks.Add(consumeTask);
                            consumeTask.Start();
                            consumeTask.ContinueWith(
                                t =>
                            {
                                lock (LockObj)
                                {
                                    tasks.Remove(t);
                                }
                            });
                        }
                        count++;
                    }
                }
                catch (IOException e)
                {
                    Log.Warn(string.Format("file:{0} is being opened", file.Name), e);
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("unknow exception when open file:{0}", file.Name), e);
                }
            }

            sw.Stop();
            Log.InfoFormat(
                "{0} files has parsed in {1} seconds, error: {2}",
                count,
                sw.Elapsed.TotalSeconds,
                files.Length - count);
        }