Check the queue of executing/submitted jobs and exit when there is
    a free slot.
512110
def poll_queue(job_id, pid, poll_time):
    
    if config.distribution.serialize_jobs:
        first_time = True
        while True:
            jobs = logs.dbcmd(GET_JOBS)
            failed = [job.id for job in jobs if not psutil.pid_exists(job.pid)]
            if failed:
                logs.dbcmd("UPDATE job SET status='failed', is_running=0 "
                           "WHERE id in (?X)", failed)
            elif any(job.id < job_id for job in jobs):
                if first_time:
                    logs.LOG.warn('Waiting for jobs %s', [j.id for j in jobs])
                    logs.dbcmd('update_job', job_id,
                               {'status': 'submitted', 'pid': pid})
                    first_time = False
                time.sleep(poll_time)
            else:
                break
    logs.dbcmd('update_job', job_id, {'status': 'executing', 'pid': _PID})
433009
def _flush_queue(self, q, ignore_priority=False):
        
        assert isinstance(q, PriorityQueue)

        current_timestamp = compute_release_time(lag_in_minutes=0)
        for _ in range(len(q)):
            entry = q.pop()
            assert isinstance(entry, PriorityEntry)

            if ignore_priority or entry.release_time < current_timestamp:
                self._resubmit_uow(entry.entry)
            else:
                q.put(entry)
                break
1030213
private void emptyQueue() {
        if (queue == null) {
            return;
        }
        int startSize = cacheMap.size();
        while (true) {
            @SuppressWarnings("unchecked")
            CacheEntry<K,V> entry = (CacheEntry<K,V>)queue.poll();
            if (entry == null) {
                break;
            }
            K key = entry.getKey();
            if (key == null) {
                // key is null, entry has already been removed
                continue;
            }
            CacheEntry<K,V> currentEntry = cacheMap.remove(key);
            // check if the entry in the map corresponds to the expired
            // entry. If not, readd the entry
            if ((currentEntry != null) && (entry != currentEntry)) {
                cacheMap.put(key, currentEntry);
            }
        }
        if (DEBUG) {
            int endSize = cacheMap.size();
            if (startSize != endSize) {
                System.out.println("*** Expunged " + (startSize - endSize)
                        + " entries, " + endSize + " entries left");
            }
        }
    }
1201180
def enter_into_queue? # Check if enough space in queue
      current_limit = queue_constraints[:capacity].to_i
      current_usage = queue_constraints[:taken].to_i + UserQ::UserQueue.count_unexpired(queue_constraints[:context])

      # Assess whether enough space left into queue
      current_usage < current_limit
    end
71838
function checkIfNoWorkersAndExit() {
            if (!currentWorkersCount) {
                log('Cluster graceful shutdown: done.');
                if (shutdownTimer) clearTimeout(shutdownTimer);
                exitFunction();
            } else {
                log('Cluster graceful shutdown: wait ' + currentWorkersCount + ' worker' + (currentWorkersCount > 1 ? 's' : '') + '.');
            }
        }
855077
private void checkInProgressWork() {
        inProgressWorkQueue.addAll(db().getAllInprogressTasks());
        StatusTrail st = null;        
        while ((st = inProgressWorkQueue.removeFirst()) != null) {
            if (st.getTaskId() == null) {
                 log.info("Found task with null taskId {}.", st);
                 st.setStatus(JobStatus.not_done).setAttemptsDone(0).setGivenUp(0);
                 db().updateStatusTrail(st);
                 log.info("Marked the status as not done to ensure it is resubmitted {}", st);
                 continue;
            }
            switch(druidDriver.pollIndexerTaskStatus(st.getTaskId())) {
                case SUCCESS:
                    newWorkQueue.remove(st);// Make sure.
                    st.setStatus(JobStatus.done);
                    st.setAttemptsDone(st.getAttemptsDone() + 1);// This will also set givenUp.
                    db().updateStatusTrail(st);
                    log.info("Task {} succeeded.", st);                    
                    break;
                case FAILED:
                    newWorkQueue.remove(st);// Make sure.
                    st.setStatus(JobStatus.not_done);
                    st.setAttemptsDone(st.getAttemptsDone() + 1);// This will also set givenUp.
                    db().updateStatusTrail(st);
                    log.info("Task {} failed.", st);
                    break;
                case RUNNING:
                    log.info("Task {} in progress.", st);
                    break;
                case UNKNOWN:
                    log.info("Task {} status unknown.", st);
                    break;
            }            
        }
    }
1223988
def process_queues(self, forced=False):
        """"""

        with self.lock:
            if (not forced and not self.must_process) or not self.queues_enabled:
                return
            self.must_process = False
        ARBITRATOR.awaken()
566242
def _exit(self, status_code):
        """"""
        # If there are active threads still running infinite loops, sys.exit
        # won't kill them but os._exit will. os._exit skips calling cleanup
        # handlers, flushing stdio buffers, etc.
        exit_func = os._exit if threading.active_count() > 1 else sys.exit
        exit_func(status_code)
1062524
def run_queues(self):
        """"""

        if self.exception:
            # pylint: disable=raising-bad-type
            raise self.exception
        listeners = self.__listeners_for_thread
        return sum(l.process() for l in listeners) > 0
56609
function (err) {
		if (this.jobs.length) {
			var job = this.jobs.shift();
			try {
				job(err, this);
			} catch (e) {
				this.next(e);
			}
		} else {
			this.running = false;
		}
	}
481515
private boolean tryEnqueueAttempt(DelayedExecution<T> delayedExecution) {
    int rejectionThreshold = queueRejectionThreshold.get();
    if (rejectionThreshold < 0) {
      return requestQueue.offer(delayedExecution);
    }
    long stamp = stampedLock.readLock();
    try {
      while (requestQueue.size() < rejectionThreshold) {
        long writeStamp = stampedLock.tryConvertToWriteLock(stamp);
        if (writeStamp != 0L) {
          stamp = writeStamp;
          return requestQueue.offer(delayedExecution);
        } else {
          stampedLock.unlock(stamp);
          stamp = stampedLock.writeLock();
        }
      }
      return false;
    } finally {
      stampedLock.unlock(stamp);
    }
  }
1289194
def cancel(self):
        """"""
        if self.queue_id is None: return 0
        if self.status >= self.S_DONE: return 0

        exit_status = self.manager.cancel(self.queue_id)
        if exit_status != 0:
            logger.warning("manager.cancel returned exit_status: %s" % exit_status)
            return 0

        # Remove output files and reset the status.
        self.history.info("Job %s cancelled by user" % self.queue_id)
        self.reset()
        return 1
147332
def inspect_queue(queue, state, limit, bucket):
    """"""
    if not HAVE_BIN_LIBS:
        click.echo("missing required binary libs (lz4, msgpack)")
        return

    conn = worker.connection

    def job_row(j):
        if isinstance(j.args[0], basestring):
            account, bucket = j.args[0].split(':', 1)
        elif isinstance(j.args[0], dict):
            account, bucket = j.args[0]['name'], "set %d" % len(j.args[1])

        row = {
            'account': account,
            'bucket': bucket,
            # 'region': j.args[1]['region'],
            # 'size': j.args[1]['keycount'],
            'ttl': j.ttl,
            'enqueued': j.enqueued_at,
            'rtt': j.result_ttl,
            'timeout': j.timeout}

        if queue != "bucket-keyset-scan":
            row['args'] = j.args[2:]
        if state in ('running', 'failed', 'finished'):
            row['started'] = j.started_at
        if state in ('finished', 'failed'):
            row['ended'] = j.ended_at
        return row

    if state == 'running':
        registry_class = StartedJobRegistry
    elif state == 'pending':
        registry_class = Queue
    elif state == 'failed':
        registry_class = FailedQueue
    elif state == 'finished':
        registry_class = FinishedJobRegistry
    else:
        raise ValueError("invalid state: %s" % state)

    registry = registry_class(queue, connection=conn)
    records = []
    for jid in registry.get_job_ids():
        j = Job.fetch(jid, conn)
        if bucket:
            if j.args[1]['name'] != bucket:
                continue
        records.append(job_row(j))
        if len(records) == limit:
            break
    if records:
        click.echo(
            tabulate.tabulate(
                records,
                "keys",
                tablefmt='simple'))
    else:
        click.echo("no queue items found")
565683
def work_once(self, free_pool_slots=1, max_jobs=None):
        

        dequeued_jobs = 0

        available_queues = [
            queue for queue in self.queues
            if queue.root_id not in self.paused_queues and
            queue.id not in self.paused_queues
        ]

        for queue_i in range(len(available_queues)):

            queue = available_queues[(queue_i + self.queue_offset) % len(available_queues)]

            max_jobs_per_queue = free_pool_slots - dequeued_jobs

            if max_jobs_per_queue <= 0:
                queue_i -= 1
                break

            if self.config["dequeue_strategy"] == "parallel":
                max_jobs_per_queue = max(1, int(max_jobs_per_queue / (len(available_queues) - queue_i)))

            for job in queue.dequeue_jobs(
                max_jobs=max_jobs_per_queue,
                job_class=self.job_class,
                worker=self
            ):
                dequeued_jobs += 1

                self.gevent_pool.spawn(self.perform_job, job)

        # At the next pass, start at the next queue to avoid always dequeuing the same one
        if self.config["dequeue_strategy"] == "parallel":
            self.queue_offset = (self.queue_offset + queue_i + 1) % len(self.queues)

        # TODO consider this when dequeuing jobs to have strict limits
        if max_jobs and self.done_jobs >= max_jobs:
            self.log.info("Reached max_jobs=%s" % self.done_jobs)
            return "break", dequeued_jobs

        # We seem to have exhausted available jobs, we can sleep for a
        # while.
        if dequeued_jobs == 0:

            if self.config["dequeue_strategy"] == "burst":
                self.log.info("Burst mode: stopping now because queues were empty")
                return "break", dequeued_jobs

            return "wait", dequeued_jobs

        return None, dequeued_jobs
516310
def has_free_slots(self):
        
        return next(filterfalse(self.numbers.__contains__, count(1))) < self.size
959296
private void checkAccess(JobInProgress job, QueueManager.QueueOperation oper,
                           UserGroupInformation ugi) throws IOException {
    // get the queue
    String queue = job.getProfile().getQueueName();
    if (!queueManager.hasAccess(queue, job, oper, ugi)) {
      throw new AccessControlException("User "
                            + ugi.getUserName()
                            + " cannot perform "
                            + "operation " + oper + " on queue " + queue +
                            ".\n Please run \"hadoop queue -showacls\" " +
                            "command to find the queues you have access" +
                            " to .");
    }
  }
139116
def submit(self, *items):
		""""""
		with self.lock:
			if self.closed:
				raise BrokenPipe('Job submission has been closed.')
			id = self.jobcount
			self._status += ['SUBMITTED'] * len(items)
			self.jobcount += len(items)
			for item in items:
				self.waitqueue.put((id, item))
				id += 1
		if len(items) == 1:
			return id - 1
		else:
			return range(id - len(items), id)