public void AddJob(Action jobAction, string key = null, string dependencyKey = null)
        {
            lock (this) {
                if (!tokenSource.Token.IsCancellationRequested && !isOnCleanupState)
                {
                    if (!key.IsNullOrEmpty() && !dependencies.ContainsKey(key))
                    {
                        dependencies[key] = new List <Pair <Action, string> >();
                    }

                    if (!dependencyKey.IsNullOrEmpty() && dependencies.ContainsKey(dependencyKey))
                    {
                        dependencies[dependencyKey].Add(new Pair <Action, string>(jobAction, key));
                        return;
                    }

                    runningTask = runningTask.ContinueWith((previousTask) => {
                        if (!tokenSource.Token.IsCancellationRequested)
                        {
                            if (!pubSub.IsAvailable(out var exception))
                            {
                                connectionFails = ProcessJobFailure(connectionFails, ConnectionRetriesTreshold, "Error executing PubSub jobs, {0} consecutive connections failed.", exception);
                                AddJob(jobAction, key, dependencyKey);
                                return;
                            }
                            connectionFails = 0;

                            try {
                                jobAction();
                                jobFails = 0;
                                lock (this) {
                                    if (!key.IsNullOrEmpty() && dependencies.ContainsKey(key))
                                    {
                                        dependencies[key].Apply((pair) => AddJob(pair.First, pair.Second));
                                        dependencies.Remove(key);
                                    }
                                }
                            } catch (Exception ex) {
                                if (pubSub.IsAvailable())
                                {
                                    jobFails = ProcessJobFailure(jobFails, JobRetriesTreshold, "Error executing PubSub jobs, {0} consecutive Jobs failed.", ex);
                                }
                                else
                                {
                                    jobFails        = 0;
                                    connectionFails = ProcessJobFailure(connectionFails, ConnectionRetriesTreshold, "Error executing PubSub jobs, {0} consecutive connections failed.", ex);
                                }
                                AddJob(jobAction, key, dependencyKey);
                            }
                        }
                    }, tokenSource.Token);
                }
            }
        }
Ejemplo n.º 2
0
 public bool IsAvailable()
 {
     return(pubSub.IsAvailable());
 }