// This method gets called by the runtime. // Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddBookStore(Configuration, _loggerFactory); var bookDetailLookup = new BookDetailLookup( Configuration.GetOrThrow("GOOGLE_PROJECT_ID"), _loggerFactory); bookDetailLookup.StartPullLoop( services.BuildServiceProvider().GetService<IBookStore>(), new CancellationTokenSource().Token); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); // Launch a thread that watches the book detail subscription. var container = App_Start.UnityConfig.GetConfiguredContainer(); var bookDetailLookup = new BookDetailLookup(LibUnityConfig.ProjectId); bookDetailLookup.CreateTopicAndSubscription(); var pullTask = bookDetailLookup.StartPullLoop(container.Resolve<IBookStore>(), new CancellationTokenSource().Token); }
public void TestLoop() { var options = new BookDetailLookup.Options(); options.SubscriptionName += "-test"; options.TopicName += "-test"; BookDetailLookup bookDetailLookup = new BookDetailLookup(_projectId, new LoggerFactory(), options); bookDetailLookup.CreateTopicAndSubscription(); var cancel = new CancellationTokenSource(); var pullTask = bookDetailLookup.StartPullLoop(new FakeBookStore(), cancel.Token); cancel.CancelAfter(100); pullTask.Wait(); }
public static void AddBookDetailLookup(this IServiceCollection services, string projectId, Action<BookDetailLookup.Options> optionSetter = null) { services.AddSingleton<BookDetailLookup>(provider => { var options = new BookDetailLookup.Options(); if (optionSetter != null) optionSetter(options); var bookDetailLookup = new BookDetailLookup( projectId, provider.GetService<ILoggerFactory>(), options); bookDetailLookup.CreateTopicAndSubscription(); return bookDetailLookup; }); }
public void TestPubsub() { var options = new BookDetailLookup.Options(); options.SubscriptionName += "-test"; options.TopicName += "-test"; BookDetailLookup bookDetailLookup = new BookDetailLookup(_projectId, new LoggerFactory(), options); bookDetailLookup.CreateTopicAndSubscription(); bookDetailLookup.EnqueueBook(45); var cancel = new CancellationTokenSource(); var pullTask = Task.Factory.StartNew(() => bookDetailLookup.PullLoop((long bookId) => { Assert.Equal(45, bookId); cancel.Cancel(); }, cancel.Token)); pullTask.Wait(); }