Esempio n. 1
0
        static async Task <BeanstalkConnection> ConnectAsync(ConnectionConfiguration config)
        {
            var connection = new BeanstalkConnection(config);

            connection._connection = await PhysicalConnection.ConnectAsync(config.Hostname, config.Port, config.Logger).ConfigureAwait(false); // Yo dawg

            return(connection);
        }
Esempio n. 2
0
        /// <summary>
        /// Schedules a worker with a dedicated TCP connection to repeatedly reserve jobs
        /// from the specified tubes and process them.
        /// </summary>
        /// <param name="configuration">The configuration for the Beanstalk connection.</param>
        /// <param name="options">The worker options.</param>
        /// <param name="worker">The delegate used to processed reserved jobs.</param>
        /// <returns>A token which stops the worker when disposed.</returns>
        public static async Task <IDisposable> ConnectWorkerAsync(ConnectionConfiguration configuration, WorkerOptions options, WorkerFunc worker)
        {
            // Must capture the context before the first await
            if (options.TaskScheduler == null)
            {
                options.TaskScheduler = SynchronizationContext.Current == null
                    ? TaskScheduler.Default
                    : TaskScheduler.FromCurrentSynchronizationContext();
            }

            var conn = await BeanstalkConnection.ConnectAsync(configuration).ConfigureAwait(false);

            try
            {
                // Just take the default tube if none was given
                if (options.Tubes.Count > 0)
                {
                    foreach (var tube in options.Tubes)
                    {
                        await((IConsumer)conn).WatchAsync(tube).ConfigureAwait(false);
                    }
                    if (!options.Tubes.Contains("default"))
                    {
                        await((IConsumer)conn).IgnoreAsync("default").ConfigureAwait(false);
                    }
                }
            }
            catch
            {
                conn.Dispose();
                throw;
            }

            var cts        = new CancellationTokenSource();
            var disposable = Disposable.Create(() =>
            {
                cts.Cancel();
                cts.Dispose();
                conn.Dispose();
            });

#pragma warning disable 4014
            Task[] workerLoops = new Task[options.NumberOfWorkers];
            for (var i = 0; i < options.NumberOfWorkers; i++)
            {
                workerLoops[i] = conn.WorkerLoop(worker, options, cts.Token);
            }

            // Ensure we handle any thrown exceptions
            Task.WhenAll(workerLoops).ContinueWith(t =>
            {
                disposable.Dispose();
                if (t.Exception != null)
                {
                    t.Exception.Handle(ex => true);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
#pragma warning restore 4014

            return(disposable);
        }