title: Trying to understand the delayed job polling query
body: <p>I'm trying to port Delayed Job to Haskell and am unable to understand the <code>WHERE</code> clause in the query that DJ fires to poll the next job:</p>
  
  <pre><code>UPDATE "delayed_jobs"
  SET locked_at = '2017-07-18 03:33:51.729884',
      locked_by = 'delayed_job.0 host:myhostname pid:21995'
  WHERE id IN (
    SELECT id FROM "delayed_jobs"
    WHERE
    (
        (
          run_at &lt;= '2017-07-18 03:33:51.729457'
          AND (locked_at IS NULL OR locked_at &lt; '2017-07-17 23:33:51.729488')
          OR locked_by = 'delayed_job.0 host:myhostname pid:21995'
        )
        AND failed_at IS NULL
    ) ORDER BY priority ASC, run_at ASC LIMIT 1 FOR UPDATE) RETURNING *
  </code></pre>
  
  <p>The structure of the <code>WHERE</code> clause is the following:</p>
  
  <pre><code>(run_at_condition AND locked_at_condition OR locked_by_condition) 
  AND failed_at_condition
  </code></pre>
  
  <ul>
  <li>Is there a set of inner parentheses missing in <code>run_at_condition AND locked_at_condition OR locked_by_condition</code>? In what precedence are the AND/OR clauses evaluated?</li>
  <li>What is the purpose of the <code>locked_by_condition</code> where it <em>seems</em> to be picking up jobs that have already been locked by the current DJ process?!</li>
  </ul>