Пример #1
0
        /// <summary>
        /// Starts the task that will makes sure pending messages
        /// are sent.
        /// </summary>
        /// <returns>Returns true if task is running</returns>
        public bool StartWorker()
        {
            var svc = ProxiedService.IsNull() ? "Null" : ProxiedService.GetType().FullName;

            _logger.Debug("Starting worker thread: " + svc);
            try
            {
                if (_workerProcTask == null || _workerProcTask.Status != TaskStatus.Running)
                {
                    Task.Run(async() =>
                    {
                        try
                        {
                            _logger.Debug($"Starting task. Id={ Task.CurrentId.GetValueOrDefault() }, Managed Thread Id {Environment.CurrentManagedThreadId}");

                            _workerToken    = new CancellationTokenSource();
                            _workerProcTask = WorkerProcAsync(_workerToken.Token);
                            await _workerProcTask.ConfigureAwait(false);

                            _logger.Debug($"Task Status: {_workerProcTask.Status}");
                        }
                        catch (OperationCanceledException)
                        {
                            _logger.Debug("WorkProc has been Canceled");
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Failed to start worker: " + ex.Message, ex);
                return(false);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Send all pending messages.
        /// </summary>
        /// <param name="forceSend">If true WiFi only will not be observed.</param>
        /// <param name="token"></param>
        public async Task SendPendingMessages(bool forceSend = false, CancellationToken token = default(CancellationToken))
        {
            var pendingMessages = RetrievePendingMessages();

            if (pendingMessages != null && pendingMessages.Count > 0)
            {
                var svc = ProxiedService.IsNull() ? "Null" : ProxiedService.GetType().FullName;
                _logger.Debug($"Pending message count: {pendingMessages.Count} / ProxiedService Type == {svc} / WiFiConnected == {WifiConnected} / ForceSend == {forceSend}");

                foreach (var call in pendingMessages.Where(call => !call.WifiOnly || WifiConnected || forceSend))
                {
                    if (token.IsCancellationRequested)
                    {
                        token.ThrowIfCancellationRequested();
                    }

                    try
                    {
                        _logger.Debug($"Replaying message {call.MessageId} {call.MethodName}");
                        call.Target = ProxiedService;
                        call.Method = ProxiedService.GetType()
                                      .GetRuntimeMethod(call.MethodName, Util.GetTypes(call.Parameters));
                        CallService(call);

                        await Task.Delay(TimeSpan.FromSeconds(5), token).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                    catch (Exception e)
                    {
                        _logger.Warn(e.Message, e);
                    }
                }
            }
        }