Example #1
0
        private async void ApiQuery(ApiQuery query)
        {
            try
            {
                if (!_apiQueryUpdates.ContainsKey(query.ApiKey))
                {
                    _apiQueryUpdates.TryAdd(query.ApiKey, query);
                }

                if (!_apiQueryBusy)
                {
                    _apiQueryBusy = true;

                    while (_apiQueryUpdates.Count > 0)
                    {
                        var apiQueries = _apiQueryUpdates.Values.ToList();
                        _apiQueryUpdates.Clear();

                        var postQuery = new PostApiQuery()
                        {
                            SecurityToken = _sharedSettings.SecurityToken,
                            ApiQueries    = apiQueries
                        };

                        var start = new Stopwatch();
                        start.Start();
                        var result = await _sharedSettings.PostAsync <PostApiQuery, ReturnValue>("Remote/ApiQuery", postQuery, CancellationToken.None);

                        start.Stop();
                        _logger.LogTrace("Send api query completed in {0}ms.", start.ElapsedMilliseconds);

                        if (result.Success == false)
                        {
                            _logger.LogError(250, result.Exception,
                                             "Query api results failed.  Return message was: {0}." + result.Message);
                        }

                        // wait a little while for more tasks results to arrive.
                        await Task.Delay(500);
                    }

                    _apiQueryBusy = false;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(250, ex,
                                 "Update api query failed with error.  Error was: {0}." + ex.Message);
                _apiQueryBusy = false;
            }
        }
        public async Task <ReturnValue> ApiQuery([FromBody] PostApiQuery apiQuery, CancellationToken cancellationToken)
        {
            try
            {
                if (string.IsNullOrEmpty(apiQuery.SecurityToken))
                {
                    return(new ReturnValue(false, $"Could not update api query as no securityToken was provided by the remote agent.", null));
                }

                //send any update to results to the clients
                foreach (var item in apiQuery.ApiQueries)
                {
                    await _operations.BroadcastHubMessageAsync(item.HubKey, EClientCommand.ApiQuery, item, cancellationToken);
                }

                return(new ReturnValue(true));
            }
            catch (Exception ex)
            {
                return(new ReturnValue(false, "Error occurred in UpdateTasks. " + ex.Message, ex));
            }
        }