コード例 #1
0
        private void InnerSubscribeTopic(string topic)
        {
            if (!watchers.Keys.Contains(topic))
            {
                lock (watchersLocker) {
                    if (!watchers.Keys.Contains(topic))
                    {
                        var filePath = FileBasedPubSubUtils.GetFilePath(basePath, topic);
                        var fileInfo = new FileInfo(filePath);
                        FileSystemWatcher watcher = new FileSystemWatcher();

                        watcher.Path         = fileInfo.DirectoryName;
                        watcher.Filter       = fileInfo.Name;
                        watcher.NotifyFilter = NotifyFilters.LastWrite;

                        watchers[topic] = watcher;

                        var fileEventHandler = new FileSystemEventHandler((source, e) => {
                            try {
                                OnFileEvent(topic, source, e);
                            } catch (Exception ex) {
                                EventLogger.WriteError($"Error handling cache file event for file: {filePath}. Cache invalidation skipped.");
                                EventLogger.WriteError(ex);
                            }
                        });

                        watcher.Changed            += fileEventHandler;
                        watcher.EnableRaisingEvents = true;
                    }
                }
            }
        }
コード例 #2
0
        public void Dispose()
        {
            if (ClearFilesOnDispose)
            {
                subscriptions
                .SelectMany(sub => sub.GetSubscribedTopics())
                .Select(topic => FileBasedPubSubUtils.GetFilePath(basePath, topic))
                .Where(filePath => File.Exists(filePath))
                .Apply(filePath => File.Delete(filePath));
            }

            CancelAllSubscriptions();
            subscriptions.Apply(sub => sub.Dispose());
        }
コード例 #3
0
        public void Publish(string topic, string message)
        {
            var filePath = FileBasedPubSubUtils.GetFilePath(basePath, topic);
            var content  = Guid.NewGuid().ToString() + Constants.NewLineCharacter + message;
            var fileInfo = new FileInfo(filePath);

            // Yes it's nasty, I know...
            Stopwatch s = new Stopwatch();

            s.Start();
            while (s.Elapsed < TimeSpan.FromMilliseconds(500))
            {
                try {
                    using (var writer = new StreamWriter(fileInfo.FullName)) {
                        writer.Write(content);
                        writer.Flush();
                    }
                    break;
                } catch (IOException) {
                }
            }
            s.Stop();
        }