Exemple #1
0
        /// <summary>
        /// Move key from JobQueue to processingQueue, get key value from cache.
        ///
        /// Also set the active field. Indicates when job was retrieved so we can monitor
        /// its time.
        /// </summary>
        /// <returns></returns>
        private async Task <RedisValueDictionary> GetJobAsync()
        {
            var db    = Database;
            var value = new RedisValueDictionary();

            while (!_cancellationToken.IsCancellationRequested)
            {
                string key = await db.ListRightPopLeftPushAsync(_jobQueue, _processingQueue);

                // If key is null, then nothing was there to get, so no value is available
                if (string.IsNullOrEmpty(key))
                {
                    value.Clear();
                    break;
                }

                await db.HashSetAsync(key, "active", (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);

                value = (RedisValueDictionary)(await db.HashGetAllAsync(key)).ToDictionary();

                // if Count is 0, remove it and check for the next job
                if (value.Count == 0)
                {
                    await db.ListRemoveAsync(_processingQueue, key, flags : CommandFlags.FireAndForget);

                    continue;
                }

                value.Key = key;

                break;
            }
            return(value);
        }
Exemple #2
0
        /// <summary>
        /// Add a job to the Queue (async)
        ///
        /// Adds a Dictionary to the message, both values are RedisValue.
        ///
        /// Reserved names for dictionary keys are 'key', 'active', 'failedcount'
        /// </summary>
        /// <param name="parametersDictionary"></param>
        /// <returns></returns>
        public async Task AddJobAsync(RedisValueDictionary parametersDictionary)
        {
            if (parametersDictionary.Count == 0)
            {
                return;
            }
            if (parametersDictionary.ContainsKey("key") ||
                parametersDictionary.ContainsKey("active") ||
                parametersDictionary.ContainsKey("failedcount"))
            {
                Trace.WriteLine("Parameter 'key', 'active' or 'failedcount' are reserved.");
                return;
            }

            var db  = Database;
            var key = await GetNextJobId();

            await db.HashSetAsync(key, parametersDictionary.Select(entries => new HashEntry(entries.Key, entries.Value)).ToArray());

            await db.ListLeftPushAsync(_jobQueue, key, When.Always, CommandFlags.FireAndForget);

            await ConnectionMultiplexer.GetSubscriber().PublishAsync(_subChannel, "", CommandFlags.FireAndForget);
        }