Beispiel #1
0
 //--- Constructors ---
 internal InMemoryKeyValueCache(ISerializer serializer, int maxSize, TaskTimerFactory timerFactory)
 {
     _serializer = serializer;
     _maxSize    = maxSize;
     _flushTimer = timerFactory.New(TimeSpan.FromSeconds(1), Flush, null, TaskEnv.None);
     _cache      = new ExpiringDictionary <string, Entry>(timerFactory);
 }
        //--- Constructors ---
        public SearchInstanceData(string indexPath, Analyzer analyzer, UpdateDelayQueue queue, TimeSpan commitInterval, TaskTimerFactory taskTimerFactory)
        {
            _analyzer  = analyzer;
            _directory = FSDirectory.GetDirectory(indexPath);

            // Note (arnec): Needed with SimpleFSLock, since a hard shutdown will have left the lock dangling
            IndexWriter.Unlock(_directory);
            try {
                _writer = new IndexWriter(_directory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
            } catch (CorruptIndexException e) {
                _log.WarnFormat("The Search index at {0} is corrupt. You must repair or delete it before restarting the service. If you delete it, you must rebuild your index after service restart.", indexPath);
                if (e.Message.StartsWith("Unknown format version"))
                {
                    _log.Warn("The index is considered corrupt because it's an unknown version. Did you accidentally downgrade your install?");
                }
                throw;
            }
            _reader           = IndexReader.Open(_directory);
            _searcher         = new IndexSearcher(_reader);
            _queue            = queue;
            _commitInterval   = commitInterval;
            _taskTimerFactory = taskTimerFactory;
            if (_commitInterval != TimeSpan.Zero)
            {
                _commitTimer = _taskTimerFactory.New(_commitInterval, Commit, null, TaskEnv.None);
            }
        }
Beispiel #3
0
 //--- Constructors ---
 public Listener(string queuename, Action <AwsSqsMessage> callback, IAwsSqsClient client, TaskTimerFactory timerFactory, TimeSpan interval)
 {
     _queuename  = queuename;
     _callback   = callback;
     _client     = client;
     _cache      = new ExpiringHashSet <string>(timerFactory);
     _cacheTimer = ((interval.TotalSeconds * 2 < 60) ? 60 : interval.TotalSeconds * 2 + 1).Seconds();
     _pollTimer  = timerFactory.New(tt => Coroutine.Invoke(PollSqs, new Result()).WhenDone(r => _pollTimer.Change(interval, TaskEnv.None)), null);
     _pollTimer.Change(0.Seconds(), TaskEnv.None);
 }
        protected DekiInstance CreateWikiInstance(string wikiId, XDoc instanceConfig)
        {
            List <DekiInstance> instances = null;
            DekiInstance        instance;

            lock (_instances) {
                instance = GetWikiInstance(wikiId);
                if (instance == null)
                {
                    var licenseStoragePlug = _dekiService.Storage;
                    _loggerRepositories[wikiId] = new ContextLoggerRepository("[" + wikiId + "] ");
                    var licenseController = GetLicenseController(wikiId, licenseStoragePlug);
                    _instances[wikiId] = instance = new DekiInstance(_dekiService, wikiId, instanceConfig, licenseController);
                }

                // Schedule new instance for shutdown if inactive-instance-timeout enabled.
                if (InactiveInstanceTimeOut != TimeSpan.MaxValue)
                {
                    var timer = _timerFactory.New(OnInstanceExpireTimer, wikiId);
                    _instanceExpireTimers[wikiId] = timer;
                }
                if (_instances.Count > _maxInstances)
                {
                    instances = _instances.Values.ToList();
                }
            }

            // Hit the instance number limit? Look for least recently accessed wiki and shut it down.
            if (instances != null)
            {
                Async.Fork(() => {
                    _log.DebugFormat("looking for excess instances to shut down");
                    var excessInstances = (from candidate in instances
                                           where DateTime.UtcNow - candidate.InstanceLastUpdateTime >= _minInstanceIdletime
                                           orderby candidate.InstanceLastUpdateTime
                                           select candidate.Id)
                                          .Take(instances.Count - _maxInstances);
                    foreach (var shutdownId in excessInstances)
                    {
                        _log.DebugFormat("shutting down instance '{0}'", shutdownId);
                        OutOfContextShutdown(shutdownId);
                    }
                });
            }
            return(instance);
        }
Beispiel #5
0
 //--- Constructors ---
 public AutoFlushContainer(T initialState, FlushDelegate flush, int maxUpdates, TimeSpan autoFlushDelay, TaskTimerFactory timerFactory)
 {
     if (flush == null)
     {
         throw new ArgumentNullException("flush");
     }
     if (maxUpdates <= 0)
     {
         throw new ArgumentException("maxItems must be positive", "maxUpdates");
     }
     if (autoFlushDelay <= TimeSpan.Zero)
     {
         throw new ArgumentException("maxDelay must be positive", "autoFlushDelay");
     }
     _state          = initialState;
     _flush          = flush;
     _maxUpdates     = maxUpdates;
     _autoFlushDelay = autoFlushDelay;
     _autoFlushTimer = timerFactory.New(DateTime.MaxValue, AutoFlushCallback, null, TaskEnv.None);
 }
        public ServiceRepository.IServiceInfo CreateLocalService(ServiceBE service, string servicePath, XDoc config)
        {
            var      deki     = DekiContext.Current.Deki;
            Plug     location = deki.InternalCreateService(servicePath, service.SID, config, new Result <Plug>()).Wait();
            XUri     sid      = XUri.TryParse(service.SID);
            string   license;
            DateTime?expiration;

            if (deki.TryGetServiceLicense(sid, out license, out expiration) && (expiration != null))
            {
                lock (_serviceExpirations) {
                    TaskTimer timer;
                    if (_serviceExpirations.TryGetValue(service.Id, out timer))
                    {
                        timer.Cancel();
                    }
                    _serviceExpirations[service.Id] = TimerFactory.New(expiration.Value, _ => ServiceBL.StopService(service), null, TaskEnv.Clone());
                }
            }
            return(RunningServices.RegisterService(service, location.Uri, true));
        }
 //--- Constructors ---
 public ExpiringSet(TaskTimerFactory taskTimerFactory, bool autoRefresh)
 {
     _taskTimerFactory = taskTimerFactory;
     _autoRefresh      = autoRefresh;
     _expireTimer      = _taskTimerFactory.New(DateTime.MaxValue, OnExpire, null, TaskEnv.None);
 }
Beispiel #8
0
        private void PutFileInternal(string s3Filename, string filename, StreamInfo file)
        {
            var tmpfile = Path.Combine(_tempDirectory, Guid.NewGuid() + ".cache");

            try {
                using (file) {
                    Tuplet <string, TaskTimer, DateTime?> entry = null;

                    // create tmp file
                    try {
                        // copy stream to tmp file
                        using (Stream stream = File.Create(tmpfile)) {
                            file.Stream.CopyTo(stream, file.Length, new Result <long>(TimeSpan.MaxValue)).Wait();
                        }

                        // create cached entry
                        if (_cacheTtl != TimeSpan.Zero)
                        {
                            lock (_cache) {
                                if (_cache.TryGetValue(s3Filename, out entry))
                                {
                                    entry.Item2.Change(_cacheTtl, TaskEnv.None);
                                    entry.Item3 = file.Modified;
                                }
                                else
                                {
                                    var timer = _timerFactory.New(_cacheTtl, OnTimer, s3Filename, TaskEnv.None);
                                    _cache[s3Filename] = entry = new Tuplet <string, TaskTimer, DateTime?>(tmpfile, timer, file.Modified);
                                }
                            }
                        }
                    } catch (Exception e) {
                        try {
                            // delete tmp file and clear out timer and cache, if any exist
                            SafeFileDelete(tmpfile);
                            if (entry != null)
                            {
                                lock (_cache) {
                                    entry.Item2.Cancel();
                                    _cache.Remove(s3Filename);
                                }
                            }
                        } catch (Exception e2) {
                            _log.WarnFormat("Failed cleaned-up post tmp file creation failure for attachment {0}: {1}", s3Filename, e2.Message);
                        }
                        throw new DreamInternalErrorException(string.Format("Unable to cache file attachment to '{0}' ({1})", s3Filename, e.Message));
                    }
                }

                // forward cached file to S3
                Stream filestream = File.Open(tmpfile, FileMode.Open, FileAccess.Read, FileShare.Read);
                file = new StreamInfo(filestream, file.Length, file.Type);
                var s3Msg = DreamMessage.Ok(file.Type, file.Length, file.Stream);
                s3Msg.Headers.ContentDisposition = new ContentDisposition(true, DateTime.UtcNow, null, null, filename, file.Length);

                // Note (arnec): The timeout is just a workaround Plug not having some kind of heartbeat on progress. Ideally 30 seconds of inactivity
                // should be perfectly fine, as long as we track uploads that are proceeding as active
                _s3.AtPath(s3Filename).WithTimeout(TimeSpan.FromMinutes(30)).Put(s3Msg);
            } finally {
                if (_cacheTtl == TimeSpan.Zero)
                {
                    SafeFileDelete(tmpfile);
                }
            }
        }