예제 #1
0
    private async Async.Task <HttpResponseData> Post(HttpRequestData req)
    {
        var request = await RequestHandling.ParseRequest <NodeStateEnvelope>(req);

        if (!request.IsOk)
        {
            return(await _context.RequestHandling.NotOk(req, request.ErrorV, context : "node event"));
        }

        var envelope = request.OkV;

        _log.Info($"node event: machine_id: {envelope.MachineId} event: {EntityConverter.ToJsonString(envelope)}");

        var error = envelope.Event switch {
            NodeStateUpdate updateEvent => await OnStateUpdate(envelope.MachineId, updateEvent),
            WorkerEvent workerEvent => await OnWorkerEvent(envelope.MachineId, workerEvent),
            NodeEvent nodeEvent => await OnNodeEvent(envelope.MachineId, nodeEvent),
            _ => new Error(ErrorCode.INVALID_REQUEST, new string[] { $"invalid node event: {envelope.Event.GetType().Name}" }),
        };

        if (error is Error e)
        {
            return(await _context.RequestHandling.NotOk(req, e, context : "node event"));
        }
        else
        {
            return(await RequestHandling.Ok(req, new BoolResult(true)));
        }
    }
예제 #2
0
    public async Async.Task <HttpResponseData> Run([HttpTrigger] HttpRequestData req)
    {
        var request = await RequestHandling.ParseRequest <CanScheduleRequest>(req);

        if (!request.IsOk || request.OkV == null)
        {
            return(await RequestHandling.NotOk(req, request.ErrorV, typeof(CanScheduleRequest).ToString(), _log));
        }

        var canScheduleRequest = request.OkV;

        var node = await _nodeOperations.GetByMachineId(canScheduleRequest.MachineId);

        if (node == null)
        {
            return(await RequestHandling.NotOk(
                       req,
                       new Error(
                           ErrorCode.UNABLE_TO_FIND,
                           new string[] {
                "unable to find node"
            }
                           ),
                       canScheduleRequest.MachineId.ToString(),
                       _log
                       ));
        }

        var allowed     = true;
        var workStopped = false;

        if (!await _nodeOperations.CanProcessNewWork(node))
        {
            allowed = false;
        }

        var task = await _taskOperations.GetByTaskId(canScheduleRequest.TaskId);

        workStopped = task == null || TaskStateHelper.ShuttingDown.Contains(task.State);

        if (allowed)
        {
            allowed = (await _nodeOperations.AcquireScaleInProtection(node)).IsOk;
        }

        return(await RequestHandling.Ok(
                   req,
                   new BaseResponse[] {
            new CanSchedule(allowed, workStopped)
        }));
    }
예제 #3
0
    private async Async.Task <HttpResponseData> Post(HttpRequestData req)
    {
        var request = await RequestHandling.ParseRequest <CanScheduleRequest>(req);

        if (!request.IsOk)
        {
            return(await _context.RequestHandling.NotOk(req, request.ErrorV, typeof(CanScheduleRequest).ToString()));
        }

        var canScheduleRequest = request.OkV;

        var node = await _context.NodeOperations.GetByMachineId(canScheduleRequest.MachineId);

        if (node == null)
        {
            return(await _context.RequestHandling.NotOk(
                       req,
                       new Error(
                           ErrorCode.UNABLE_TO_FIND,
                           new string[] {
                "unable to find node"
            }),
                       canScheduleRequest.MachineId.ToString()));
        }

        var allowed = true;

        if (!await _context.NodeOperations.CanProcessNewWork(node))
        {
            allowed = false;
        }

        var task = await _context.TaskOperations.GetByTaskId(canScheduleRequest.TaskId);

        var workStopped = task == null || task.State.ShuttingDown();

        if (workStopped)
        {
            allowed = false;
        }

        if (allowed)
        {
            allowed = (await _context.NodeOperations.AcquireScaleInProtection(node)).IsOk;
        }

        return(await RequestHandling.Ok(req, new CanSchedule(Allowed : allowed, WorkStopped : workStopped)));
    }