title: Algorithm to find resource-free slot
body: <p>I am designing a project management app for a factory. The app is expected to produce draft project plans. To schedule a task, the app should check three conditions:</p>
  
  <ol>
  <li>task dependency - do not start before,</li>
  <li>machine availability, and</li>
  <li>shift work hours</li>
  </ol>
  
  <p>I keep track of machine engagement in <code>machine_allocations</code> table:</p>
  
  <pre><code>machine_allocations
  +------------+--------------+-----------------+---------------+
  | machine_id | operation_id | start_timestamp | end_timestamp |
  +------------+--------------+-----------------+---------------+
  </code></pre>
  
  <p>Shift hours follow a pattern. </p>
  
  <p>Now, to find the earliest possible date-time for an operation I am thinking of a function:</p>
  
  <pre><code>function earliest_slot($machine_id, $for_duration, $no_sooner_than) {
     // pseudo code
     1. get records for the machine in question for after $no_sooner_than 
     2. put start and end timestamps into $unavailable array
     3. add non-working times as new elements to the array
     4. in a loop find timeslots which are not in the array
     5. if a timeslot is found which is equal to or bigger than $for_duration, return that
  }
  </code></pre>
  
  <p>My question is, is this a good approach? Are there simpler ways to do this?</p>