Пример #1
0
        public async Task <T> GetAllAsync(string entityId)
        {
            var redisKey = PrimaryCacheKeyFormatter(entityId);
            var hash     = (await RedisDatabase.HashGetAllAsync(redisKey).ConfigureAwait(false)).ToList();
            var props    = typeof(T).GetProperties();
            var entity   = new T();

            foreach (var prop in props)
            {
                var propInterfaces = prop.PropertyType.GetInterfaces();

                // If it's a collection property, extract out the values from the hash
                if (CanPutHashItemsIntoCollection(prop, propInterfaces, hash, entity))
                {
                    continue;
                }

                // Else try to get a hash value for the given property.
                var hashItem = hash.FirstOrDefault(h => h.Name == prop.Name);
                if (hashItem == default(HashEntry) || hashItem.Value.IsNull)
                {
                    continue;
                }

                // Deserialize the value
                var val = JsonConvert.DeserializeObject(hashItem.Value, prop.PropertyType);
                prop.SetValue(entity, val);
            }
            return(entity);
        }
Пример #2
0
        public async Task <HttpActionResult <DeploymentRequest, Deployment> > Post([FromBody] DeploymentRequest request) => await this.WithResponseContainer(
            request,
            async info =>
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new InternalHttpException("Deployment package name must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (string.IsNullOrEmpty(request.Version))
            {
                throw new InternalHttpException("Deployment package version must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (request.Agents == null || request.Agents.Count == 0)
            {
                throw new InternalHttpException("Agent list must be specified.", (int)HttpStatusCode.BadRequest, new { request });
            }

            IDeserializer deserializer = new DeserializerBuilder().Build();
            var yaml = deserializer.Deserialize <Dictionary <string, List <string> > >(new StringReader(request.Yaml));
            Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(yaml, StringComparer.InvariantCultureIgnoreCase)
                                                       .ToDictionary(kv => kv.Key, kv => kv.Value ?? new List <string>());
            var deployment = new Deployment
            {
                DeploymentPackage = new DeploymentPackage
                {
                    Name    = request.Name,
                    Version = request.Version
                },
                InstallCommands   = dict.ContainsKey(nameof(Deployment.InstallCommands)) ? dict[nameof(Deployment.InstallCommands)] : new List <string>(),
                StartCommands     = dict.ContainsKey(nameof(Deployment.StartCommands)) ? dict[nameof(Deployment.StartCommands)] : new List <string>(),
                StopCommands      = dict.ContainsKey(nameof(Deployment.StopCommands)) ? dict[nameof(Deployment.StopCommands)] : new List <string>(),
                UninstallCommands = dict.ContainsKey(nameof(Deployment.UninstallCommands)) ? dict[nameof(Deployment.UninstallCommands)] : new List <string>(),
                Timestamp         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                Id = Guid.NewGuid().ToString()
            };

            if (deployment.InstallCommands.Count == 0 && deployment.StartCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of install/start commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            if (deployment.StopCommands.Count == 0 && deployment.UninstallCommands.Count == 0)
            {
                throw new InternalHttpException("Must specify at least one of uninstall/stop commands.", (int)HttpStatusCode.BadRequest, new { request });
            }

            Dictionary <string, AgentInfo> agentsDict   = await RedisDatabase.HashGetAllAsync <AgentInfo>(CacheKeys.AgentInfo);
            Dictionary <string, Deployment> deployments = agentsDict.Values
                                                          .Where(i => i.Status == AgentStatus.Ready)
                                                          .Where(i => request.Agents.Contains(i.Id))
                                                          .Select(i => i.Id)
                                                          .ToDictionary(k => k, v => deployment);

            await RedisDatabase.HashSetAsync(CacheKeys.ActiveDeployments, deployments);
            return(deployment);
        });
Пример #3
0
        public async Task <TFieldValue> GetFieldVaueAsync <TFieldValue>(string entityId, string hashName)
        {
            var redisKey      = PrimaryCacheKeyFormatter(entityId);
            var fieldType     = typeof(TFieldValue);
            var isIDictionary = fieldType.GetInterfaces().Any(pi => pi.IsGenericType && pi.GetGenericTypeDefinition() == typeof(IDictionary <,>));

            if (isIDictionary && FlattenDictionaries)
            {
                var fullHash = await RedisDatabase.HashGetAllAsync(redisKey).ConfigureAwait(false);

                var dictionaryHashes      = fullHash.Where(h => h.Name.ToString().Contains(hashName)).ToList();
                var constructedDictionary = GetConstructedDictionaryPropertyInstance(fieldType, dictionaryHashes, hashName);
                return((TFieldValue)constructedDictionary);
            }
            var hashVal = await RedisDatabase.HashGetAsync(redisKey, hashName).ConfigureAwait(false);

            var val = JsonConvert.DeserializeObject <TFieldValue>(hashVal);

            return(val);
        }
Пример #4
0
 public async Task <HttpActionResult <Dictionary <string, AgentInfo> > > List() => await this.WithResponseContainer(
     async() => await RedisDatabase.HashGetAllAsync <AgentInfo>(CacheKeys.AgentInfo));