private async Task HandleClientImageRequest(OperationHandlerContext ctx, CancellationToken cancellationToken)
        {
            var request = ((ImageTaskRequest)ctx.Message).Payload;
            var image   = await Socket.ReceiveStreamAsync(cancellationToken);

            var task = _taskFactory.Create(image);

            void Callback(object sender, BaseTaskState arg)
            {
                if (!((BaseTask)sender).IsCompleted)
                {
                    return;
                }

                var _ = SendTaskUpdateAsync(task, cancellationToken);
            }

            task.DetectionTask.OnStateChanged += Callback;
            task.OnStateChanged += Callback;

            var _ = _taskRunner.RunTaskAsync(task, cancellationToken);
        }
Beispiel #2
0
        public async Task <IActionResult> DoStandardRequest([FromHeader(Name = "X-WebSocket-Session-Id")]
                                                            Guid sessionId)
        {
            var startTime = DateTime.Now;

            if (!_websockets.TryGetSession(sessionId, out var session))
            {
                return(new BadRequestObjectResult(new ErrorResponse(400, "WebSocket session not found")));
            }

            if (session.SessionState != WebSocketSessionState.SessionEstablished)
            {
                return(new BadRequestObjectResult(new ErrorResponse(400, "Invalid WebSocket session state")));
            }

            var metrics = _metrics.GetInstance(new Dictionary <string, string>
            {
                { "sessionId", sessionId.ToString() }
            });

            // retrieve uploaded image
            if (Request.Body == null)
            {
                return(new BadRequestObjectResult(new ErrorResponse(400, "Image payload cannot be empty")));
            }
            var image = new MemoryStream();
            await Request.Body.CopyToAsync(image);

            // create task
            var task = _taskFactory.Create(image);
            var cancellationToken = CancellationToken.None;

            // subscribe detection task
            var detectionTask = new TaskCompletionSource <bool>();

            task.DetectionTask.OnStateChanged += (sender, arg) => detectionTask.SetResult(true);

            // subscribe entire task
            task.OnStateChanged += (sender, arg) =>
            {
                if (task.State != BaseTaskState.Succeeded && task.State != BaseTaskState.Failed)
                {
                    return;
                }

                session.SendTaskUpdateAsync(task, cancellationToken);

                metrics.Write(VectorTime, task.VectorizationTask.Time);
                metrics.Write(SearchTime, task.SearchTask.Time);

                metrics.Write(Stage2ResponseTime, (int)(DateTime.Now - startTime).TotalMilliseconds);
            };

            // start entire task
            var _ = _taskRunner.RunTaskAsync(task, cancellationToken);

            // await detection task
            await detectionTask.Task;

            metrics.Write(FaceCount, task.FaceCount);
            metrics.Write(DetectionTime, task.DetectionTask.Time);

            // return
            metrics.Write(Stage1ResponseTime, (int)(DateTime.Now - startTime).TotalMilliseconds);
            return(new OkObjectResult(task));
        }