RestBucks on .Net; the barista-bot

Previous posts

Imagine now that you have another system running in parallel with RestBucks on .Net using the same object domain. This other system is used by the Barista;

A barista (from the Italian for "bartender") is a person, usually a coffee-house employee, who prepares and serves espresso-based coffee drinks.

What I did in RestBucks was to simulate this behavior; our BaristaBot will pick paid orders each 45 seconds and prepare it. For this purpose I have used some ReactiveExtensions magic:

public class Barista
{
    private readonly ISessionFactory sessionFactory;
    private readonly IDisposable subscription;

    public Barista(ISessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
        subscription = Observable.Interval(TimeSpan.FromSeconds(45))
            .Subscribe(PrepareNextOrder);
    }

    public void PrepareNextOrder(long i)
    {
        using(var session = sessionFactory.OpenSession())
        using(var tx = session.BeginTransaction())
        {
            var order = session.Query<Order>()
                            .Where(o => o.Status == OrderStatus.Paid)
                            .OrderBy(o => o.Payment.DateTime)
                            .FirstOrDefault();
            if (order == null) return;
            
            order.Finish();
            
            tx.Commit();
        }
    }

    public void Dispose()
    {
        subscription.Dispose();
    }
}

I didn't use the repository or other abstractions here to keep things simples. This solution depends on NHibernate and Rx.

You can test the behavior in our live instance at http://restbuckson.net.


blog comments powered by Disqus
  • Categories

  • Archives