private void Collect(object obj, CancellationToken ct, IMessageAcknowledge ack)
 {
     if (_sinks.ContainsKey(obj.GetType()))
     {
         lock (_lock) _collected.Enqueue(Tuple.Create(obj, ack));
     }
 }
        /// <summary>
        ///   Message came in to create a new workflow.
        /// </summary>
        private void NewWorkflowInstance(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            var wfc = (WorkflowCreate)msg;


            Debug.Assert(!string.IsNullOrEmpty(wfc.TemplateName) || !string.IsNullOrEmpty(wfc.TemplateContent));

            try
            {
                _log.InfoFormat("Hosting new workflow instance of type {0}", wfc.TemplateName);

                string templateData = !string.IsNullOrEmpty(wfc.TemplateContent)
                                              ? wfc.TemplateContent
                                              : LoadWorkflowTemplate(wfc.TemplateName);

                var agent = new WorkflowAgent(this, wfc.Id, _ct, wfc.InitialData, templateData, _workflowDataRepositoryKey, _workflowMessagingKey, _workflowWorkspaceKey);
                _agents.TryAdd(wfc.Id, agent);
                agent.Start();
                ack.MessageAcknowledged();
            }
            catch (Exception ex)
            {
                var es = string.Format("Problem while trying to host new workflow instance for type {0}: {1}",
                                       wfc.TemplateName, ex);
                _log.Error(es);
                var on = Catalog.Factory.Resolve <IApplicationAlert>();
                on.RaiseAlert(ApplicationAlertKind.Unknown, es);
                if (_agents.ContainsKey(wfc.Id))
                {
                    WorkflowAgent _;
                    _agents.TryRemove(wfc.Id, out _);
                }
                ack.MessageRejected();
            }
        }
Beispiel #3
0
        private static void ShowCaptain(object data, CancellationToken ct, IMessageAcknowledge ack)
        {
            var c = (Captain)data;

            Debug.WriteLine(c.Name);
            ack.MessageAcknowledged();
        }
 /// <summary>
 ///   Job scheduler says it's time to wake a workflow.
 /// </summary>
 private void WakeWorkflow(object msg, CancellationToken ct, IMessageAcknowledge ack)
 {
     try
     {
         var wwfj = (WakeWorkflowJob)msg;
         _log.InfoFormat("Scheduled wake of sleeping workflow {0}", wwfj.Id);
         TryAcquireWorkflow(wwfj.Id);
         ack.MessageAcknowledged();
     }
     catch (Exception)
     {
         ack.MessageRejected();
         throw;
     }
 }
        private void FireRetryMsg(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            var wfsmr = (WorkflowStateMachineRetry)msg;

            try
            {
                FireRetry(ct, wfsmr);
                ack.MessageAcknowledged();
            }
            catch (Exception)
            {
                ack.MessageRejected();
                throw;
            }
        }
        private void FireTriggerJobMsg(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            var wftj = (FireWorkflowTriggerJob)msg;

            try
            {
                FireTriggerJob(ct, wftj);
                ack.MessageAcknowledged();
            }
            catch (Exception)
            {
                ack.MessageRejected();
                throw;
            }
        }
        /// <summary>
        ///   Message came in saying to trigger a workflow. If we don't host it now, assume it's sleeping.
        /// </summary>
        private void DistributeWorkflowTriggers(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            try
            {
                var trigger = (WorkflowTrigger)msg;

                if (_agents.ContainsKey(trigger.InstanceTarget))
                {
                    InvokeTrigger(_agents[trigger.InstanceTarget], trigger);
                }
                else
                {
                    // try to get a handle on possibly sleeping workflow.

                    var newAgent = TryAcquireWorkflow(trigger.InstanceTarget);
                    if (null != newAgent)
                    {
                        _log.InfoFormat("Waking sleeping workflow {0} with trigger {1}", trigger.InstanceTarget,
                                        trigger.TriggerName ?? "none");
                        InvokeTrigger(_agents[trigger.InstanceTarget], trigger);
                    }
                    else
                    {
                        _dblog.InfoFormat("Ignoring trigger on instance {0} because it is active on another host.",
                                          trigger.InstanceTarget);
                    }
                }

                if (null != ack)
                {
                    ack.MessageAcknowledged();
                }
            }
            catch (Exception)
            {
                if (null != ack)
                {
                    ack.MessageRejected();
                }
                throw;
            }
        }
Beispiel #8
0
        private void Email(object msg, CancellationToken ct, IMessageAcknowledge ack)
        {
            var log = ClassLogger.Create(typeof(SendEmailWorker));

            var command = (SendEmail)msg;

            Debug.Assert(!string.IsNullOrEmpty(command.Sender));
            Debug.Assert(!string.IsNullOrEmpty(command.Subject));
            Debug.Assert(null != command.Content);
            Debug.Assert(command.Recipients.EmptyIfNull().Any());

            if (!command.Recipients.Any())
            {
                return;
            }

            if (ct.IsCancellationRequested)
            {
                return;
            }

            var cred = new NetworkCredential {
                UserName = _account, Password = _password
            };
            var email = new SmtpClient
            {
                Credentials = cred, UseDefaultCredentials = true, EnableSsl = _useSSL, Port = _port, Host = _server
            };

            var message = new MailMessage
            {
                From       = new MailAddress(command.Sender),
                Subject    = command.Subject,
                Body       = command.Content,
                IsBodyHtml = command.HtmlFormat
            };

            foreach (var r in command.Recipients)
            {
                message.To.Add(new MailAddress(r));
            }

            if (!string.IsNullOrEmpty(_replyAddress))
            {
                message.ReplyToList.Add(_replyAddress);
            }

            var sent       = false;
            var retryCount = 0;

            while (!sent)
            {
                if (ct.IsCancellationRequested)
                {
                    return;
                }

                try
                {
                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                    email.Send(message);
                    log.InfoFormat(
                        "Sent email about '{0}' to {1} recipients", command.Subject, command.Recipients.Count());
                    sent = true;
                }
                catch (SmtpFailedRecipientsException)
                {
                    sent = true;
                }
                catch (SmtpFailedRecipientException)
                {
                    sent = true;
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount > 10)
                    {
                        const string es = "Could not send email.";
                        log.Error(es);
                        log.TraceException(ex);
                        var on = Catalog.Factory.Resolve <IApplicationAlert>();
                        on.RaiseAlert(ApplicationAlertKind.Services, es, ex);
                        ack.MessageRejected();
                        break;
                    }

                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                    ct.WaitHandle.WaitOne(3000);
                }
            }

            if (sent)
            {
                ack.MessageAcknowledged();
            }
        }