示例#1
0
        static void Main(string[] args)
        {
            //TODO: Get config for the sidecar from the WebAPI it is bridging to.  http://localhost:5000/api/camundaconfig
            var           topicConfigs = FetchCamundaConfigs("http://localhost:5000");
            CamundaClient camunda      = CamundaClient.Create("http://localhost:8080/engine-rest");

            while (true)
            {
                var topicList = new FetchExternalTasks()
                {
                    AsyncResponseTimeout = 10000,
                    MaxTasks             = 50,
                    Topics   = new List <FetchExternalTaskTopic>(),
                    WorkerId = "myworkerId"
                };
                foreach (var config in topicConfigs)
                {
                    topicList.Topics.Add(new FetchExternalTaskTopic(config.Key, 10));
                }

                var allTasks = camunda.ExternalTasks.FetchAndLock(topicList).Result;
                Console.WriteLine($"Number of Tasks:{allTasks.Count}");
                foreach (var task in allTasks)
                {
                    var realtask = camunda.ExternalTasks[task.Id].Get().Result;

                    var response = CallTheSyncMethod(task.Variables["AsyncPostRequest"], topicConfigs[task.TopicName]);

                    CompleteTask(camunda, task, response);
                }

                Thread.Sleep(5000);
            }
        }
        public ExternalTaskWorker(
            CamundaClient camundaClient,
            ExternalTaskWorkerInfo externalTaskWorkerInfo,
            WorkerSettings workerSettings)
        {
            _camundaClient          = camundaClient;
            _externalTaskWorkerInfo = externalTaskWorkerInfo;

            _pollingInterval = workerSettings.ExternalTaskSettings.PollingInterval;
            var lockDuration = workerSettings.ExternalTaskSettings.LockDuration;

            _maxDegreeOfParallelism = workerSettings.ExternalTaskSettings.MaxDegreeOfParallelism;
            var maxTasksToFetchAtOnce = workerSettings.ExternalTaskSettings.MaxTasksToFetchAtOnce;

            var topic = new FetchExternalTaskTopic(_externalTaskWorkerInfo.TopicName, lockDuration)
            {
                Variables = _externalTaskWorkerInfo.VariablesToFetch
            };

            _fetching = new FetchExternalTasks()
            {
                WorkerId    = _workerId,
                MaxTasks    = maxTasksToFetchAtOnce,
                UsePriority = true,
                Topics      = new List <FetchExternalTaskTopic>()
                {
                    topic
                }
            };
        }
示例#3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{WORKER_ID} running at: {DateTimeOffset.Now}");

                // Starting camunda client
                CamundaClient camunda = CamundaClient.Create("http://localhost:8080/engine-rest");

                // Fetching available tasks for that topic
                var fetchExternalTasks = new FetchExternalTasks()
                {
                    MaxTasks = 10,
                    WorkerId = WORKER_ID,
                    Topics   = new List <FetchExternalTaskTopic>()
                    {
                        new FetchExternalTaskTopic("persist_user", 2000)
                    }
                };

                List <LockedExternalTask> lockedExternalTasks = await camunda.ExternalTasks.FetchAndLock(fetchExternalTasks);

                // Processing the tasks
                foreach (LockedExternalTask lockedExternalTask in lockedExternalTasks)
                {
                    // Loading all variables from this task
                    Dictionary <string, VariableValue> taskVariables = lockedExternalTask.Variables;

                    var name     = taskVariables["name"];
                    var password = taskVariables["password"];
                    var userId   = new Random().Next(1, 100);

                    // Process the task as you wish
                    _logger.LogInformation($"Persisting on DB. New user: {name}, password: {password}. Generated ID: {userId}");

                    // Setting output variables
                    Dictionary <string, VariableValue> outputVariables = new Dictionary <string, VariableValue>
                    {
                        { "user_id", VariableValue.FromObject(userId) }
                    };

                    // Completes task
                    var completeExternalTask = new CompleteExternalTask()
                    {
                        Variables = outputVariables,
                        WorkerId  = WORKER_ID,
                    };

                    await camunda.ExternalTasks[lockedExternalTask.Id].Complete(completeExternalTask);
                }

                await Task.Delay(_serviceConfiguration.Interval, stoppingToken);
            }
        }
        private static Task <List <LockedExternalTask> > FetchAndLockTasks(string workerId, FetchExternalTaskTopic topic)
        {
            var fetchingQuery = new FetchExternalTasks
            {
                WorkerId    = workerId,
                MaxTasks    = _camundaSettings.FetchTaskCount,
                UsePriority = false,
                Topics      = new List <FetchExternalTaskTopic> {
                    topic
                }
            };

            return(_engineClient.Client().ExternalTasks.FetchAndLock(fetchingQuery));
        }
示例#5
0
        public void DoPolling()
        {
            // Query External Tasks
            var fetchAndLockBackoff = 0L;

            try
            {
                var fetchExternalTasks = new FetchExternalTasks()
                {
                    WorkerId = workerId,
                    MaxTasks = topicManagerInfo.MaxTasks,
                    Topics   = new List <FetchExternalTaskTopic>()
                    {
                        new FetchExternalTaskTopic(topicManagerInfo.TopicName, topicManagerInfo.LockDurationInMilliseconds)
                        {
                            Variables = topicManagerInfo.VariablesToFetch == null ? topicManagerInfo.VariablesToFetch : null
                        }
                    }
                };
                var tasks = new List <LockedExternalTask>();
                policyManager.fetchAndLockPolicy().Execute(() =>
                {
                    tasks = externalTaskService.FetchAndLock(fetchExternalTasks).Result;
                });
                backoffStrategy.Reconfigure(tasks.Count);
                fetchAndLockBackoff = backoffStrategy.Calculate();
                Console.WriteLine($"Fetch and locked {tasks.Count} tasks in topic {topicManagerInfo.TopicName}. Will try again in {TimeSpan.FromMilliseconds(fetchAndLockBackoff)} seconds");

                // run them in parallel with a max degree of parallelism
                Parallel.ForEach(
                    tasks,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = topicManagerInfo.MaxDegreeOfParallelism
                },
                    externalTask =>
                {
                    Execute(externalTask);
                }
                    );
            }
            finally
            {
                // schedule next run (if not stopped in between)
                if (taskQueryTimer != null)
                {
                    taskQueryTimer.Change(TimeSpan.FromMilliseconds(fetchAndLockBackoff), TimeSpan.FromMilliseconds(Timeout.Infinite));
                }
            }
        }
示例#6
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{WORKER_ID} running at: {DateTimeOffset.Now}");

                // Starting camunda client
                CamundaClient camunda = CamundaClient.Create("http://localhost:8080/engine-rest");

                // Fetching available tasks for that topic
                var fetchExternalTasks = new FetchExternalTasks()
                {
                    MaxTasks = 10,
                    WorkerId = WORKER_ID,
                    Topics   = new List <FetchExternalTaskTopic>()
                    {
                        new FetchExternalTaskTopic("send_email", 2000)
                    }
                };

                List <LockedExternalTask> lockedExternalTasks = await camunda.ExternalTasks.FetchAndLock(fetchExternalTasks);

                // Processing the tasks
                foreach (LockedExternalTask lockedExternalTask in lockedExternalTasks)
                {
                    // Loading all variables from this task
                    Dictionary <string, VariableValue> taskVariables = lockedExternalTask.Variables;

                    var userId = taskVariables["user_id"];

                    // Process the task as you wish
                    _logger.LogInformation($"Sending email to user: {userId}");

                    // Completes task
                    var completeExternalTask = new CompleteExternalTask()
                    {
                        WorkerId = WORKER_ID
                    };

                    await camunda.ExternalTasks[lockedExternalTask.Id].Complete(completeExternalTask);
                }

                await Task.Delay(_serviceConfiguration.Interval, stoppingToken);
            }
        }
        public List <LockedExternalTask> FetchAndLockTasks(string topicName)
        {
            Console.WriteLine($"Executing fetch and lock for topic {topicName}");

            var fetchAndLockRequest = new FetchExternalTasks()
            {
                WorkerId = TestWorkerId,
                MaxTasks = 1,
                Topics   = new List <FetchExternalTaskTopic> {
                    new FetchExternalTaskTopic(topicName, 10000)
                }
            };

            List <LockedExternalTask> lockedExternalTasks = CamundaClient.ExternalTasks
                                                            .FetchAndLock(fetchAndLockRequest).Result;

            Console.WriteLine($"Number of locked external tasks {lockedExternalTasks.Count}");

            return(lockedExternalTasks);
        }
示例#8
0
        private static async Task AwaitNetExternalTaskSync()
        {
            FetchExternalTaskTopic topic = new FetchExternalTaskTopic("address-info", 10000);

            var topics = new System.Collections.Generic.List <FetchExternalTaskTopic>();

            topics.Add(topic);

            FetchExternalTasks t = new FetchExternalTasks()
            {
                WorkerId             = "postman",
                AsyncResponseTimeout = 30000,
                MaxTasks             = 3,
                Topics = topics
            };

            var et = await _client.ExternalTasks.FetchAndLock(t);

            foreach (var task in et)
            {
                Console.WriteLine(task.Id + " " + task.TopicName);
            }
        }