Ejemplo n.º 1
0
        public async Task <IActionResult> Customize([FromBody] RelayBuildRequest request, [FromServices] ICustomizer customizer)
        {
            RelayBuild newBuild = null;

            System.Action cleanupBuild = () =>
            {
                if (newBuild != null)
                {
                    try
                    {
                        context.RelayBuilds.Remove(newBuild);
                        context.SaveChanges();
                    }
                    catch (Exception)
                    {
                    }
                }
            };
            try
            {
                newBuild = Convert(request);
                byte[] output;
                {
                    context.RelayBuilds.Add(newBuild);
                    context.SaveChanges();
                }
                output = await customizer.CustomizeNodeRelay(newBuild);

                var buildName = String.IsNullOrEmpty(newBuild.Name) ? "" : $"_{newBuild.Name}";
                return(File(output, "application/octet-stream", $"Relay_{newBuild.Arch.ToString().ToLower()}_{newBuild.BuildId:x}{buildName}.{newBuild.Type.ToString().ToLower()}"));
            }
            catch (TimeoutException e)
            {
                cleanupBuild();
                return(StatusCode(StatusCodes.Status408RequestTimeout, e.Message));
            }
            catch (CommandQueues.InvalidGateway e)
            {
                cleanupBuild();
                return(StatusCode(StatusCodes.Status410Gone, $"Failed to add build, because {e.Message}. Try restarting gateway."));
            }
            catch (Exception e)
                when(e is CustomizerError || e is GatewayResponseError)
                {
                    cleanupBuild();
                    return(BadRequest(e.Message));
                }
            catch (Exception e)
            {
                cleanupBuild();
                return(StatusCode(StatusCodes.Status500InternalServerError, $"Unknown error. {e.Message}"));
            }
        }
Ejemplo n.º 2
0
        private async Task AddCommand(Command command, HexId gatewayId, HexId relayId = null, HexId deviceId = null, string deviceType = null)
        {
            if (deviceId != null && deviceType is null)
            {
                throw new NullReferenceException("Got DeviceId without device type");
            }

            command.GatewayAgentId = gatewayId.Value;
            command.RelayAgentId   = relayId?.Value;
            command.InterfaceId    = deviceId?.Value;
            command.DeviceType     = deviceType;
            context.Add(command);
            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                // save changes to force command.Id generation
                context.SaveChanges();
                commandQueues.Enqueue(gatewayId.Value, new Models.Action(command));
                // commit only if enqueue doesn't throw
                transaction.Commit();
            }
        }