title: Database polling with Reactive Extensions
body: <p>I have to query a database in a timely fashion to know the state of a legacy system. I've thought of wrapping the query around an <code>Observable</code>, but I don't know the correct way to do it. </p>
  
  <p><strong>Basically, it will be the same query every 5 seconds.</strong> But I'm afraid I will have to face these problems:</p>
  
  <ul>
  <li>What if the execution of the query takes 10 seconds? I don't want to
  execute any new query if the previous is still being processed.</li>
  <li>Also, there should be a timeout. If the current query doesn't execute
  after, for example, 20 seconds, an informative message should be
  logged and a new attempt (the same query) should be sent.</li>
  </ul>
  
  <p>Extra details:</p>
  
  <ul>
  <li>The query is just a <code>SELECT</code> that returns a dataset with a list of status codes (<em>working</em>, <em>faulted</em>).</li>
  <li>The Observable sequence will always take the latest data received from the query, something like the Switch extension method.</li>
  <li>I would like to wrap the database query (lenghty operation) into a Task, but I'm not sure if it's the best option.</li>
  </ul>
  
  <p>I'm almost sure that the query should be executed in another thread, but I have no idea of how the observable should look like, ever  having read <a href="http://introtorx.com/">Introduction to Rx by Lee Campbell</a>.</p>