Stores an existing job in the queue.

@return The input job or null if something went wrong (the queue is full for instance)
14327
protected Job post(Job job) {
        if (mQueue.offer(job)) {
            save();
            return job;
        } else {
            return null;
        }
    }
907570
def _insert_job(self, job):
        

        key = self._job_key(job)

        if self._allow_merging:
            if key in self._job_map:
                job_info = self._job_map[key]

                # decide if we want to trigger a widening
                # if not, we'll simply do the merge
                # TODO: save all previous jobs for the sake of widening
                job_added = False
                if self._allow_widening and self._should_widen_jobs(job_info.job, job):
                    try:
                        widened_job = self._widen_jobs(job_info.job, job)
                        # remove the old job since now we have a widened one
                        if job_info in self._job_info_queue:
                            self._job_info_queue.remove(job_info)
                        job_info.add_job(widened_job, widened=True)
                        job_added = True
                    except AngrJobWideningFailureNotice:
                        # widening failed
                        # fall back to merging...
                        pass

                if not job_added:
                    try:
                        merged_job = self._merge_jobs(job_info.job, job)
                        # remove the old job since now we have a merged one
                        if job_info in self._job_info_queue:
                            self._job_info_queue.remove(job_info)
                        job_info.add_job(merged_job, merged=True)
                    except AngrJobMergingFailureNotice:
                        # merging failed
                        job_info = JobInfo(key, job)
                        # update the job map
                        self._job_map[key] = job_info

            else:
                job_info = JobInfo(key, job)
                self._job_map[key] = job_info

        else:
            job_info = JobInfo(key, job)
            self._job_map[key] = job_info

        if self._order_jobs:
            self._binary_insert(self._job_info_queue, job_info, lambda elem: self._job_sorting_key(elem.job))

        else:
            self._job_info_queue.append(job_info)
272124
def store_job(opts, load, event=None, mminion=None):
    '''
    
    '''
    # Generate EndTime
    endtime = salt.utils.jid.jid_to_time(salt.utils.jid.gen_jid(opts))
    # If the return data is invalid, just ignore it
    if any(key not in load for key in ('return', 'jid', 'id')):
        return False
    if not salt.utils.verify.valid_id(opts, load['id']):
        return False
    if mminion is None:
        mminion = salt.minion.MasterMinion(opts, states=False, rend=False)

    job_cache = opts['master_job_cache']
    if load['jid'] == 'req':
        # The minion is returning a standalone job, request a jobid
        load['arg'] = load.get('arg', load.get('fun_args', []))
        load['tgt_type'] = 'glob'
        load['tgt'] = load['id']

        prep_fstr = '{0}.prep_jid'.format(opts['master_job_cache'])
        try:
            load['jid'] = mminion.returners[prep_fstr](nocache=load.get('nocache', False))
        except KeyError:
            emsg = "Returner '{0}' does not support function prep_jid".format(job_cache)
            log.error(emsg)
            raise KeyError(emsg)

        # save the load, since we don't have it
        saveload_fstr = '{0}.save_load'.format(job_cache)
        try:
            mminion.returners[saveload_fstr](load['jid'], load)
        except KeyError:
            emsg = "Returner '{0}' does not support function save_load".format(job_cache)
            log.error(emsg)
            raise KeyError(emsg)
    elif salt.utils.jid.is_jid(load['jid']):
        # Store the jid
        jidstore_fstr = '{0}.prep_jid'.format(job_cache)
        try:
            mminion.returners[jidstore_fstr](False, passed_jid=load['jid'])
        except KeyError:
            emsg = "Returner '{0}' does not support function prep_jid".format(job_cache)
            log.error(emsg)
            raise KeyError(emsg)

    if event:
        # If the return data is invalid, just ignore it
        log.info('Got return from %s for job %s', load['id'], load['jid'])
        event.fire_event(load,
                         salt.utils.event.tagify([load['jid'], 'ret', load['id']], 'job'))
        event.fire_ret_load(load)

    # if you have a job_cache, or an ext_job_cache, don't write to
    # the regular master cache
    if not opts['job_cache'] or opts.get('ext_job_cache'):
        return

    # do not cache job results if explicitly requested
    if load.get('jid') == 'nocache':
        log.debug('Ignoring job return with jid for caching %s from %s',
                  load['jid'], load['id'])
        return

    # otherwise, write to the master cache
    savefstr = '{0}.save_load'.format(job_cache)
    getfstr = '{0}.get_load'.format(job_cache)
    fstr = '{0}.returner'.format(job_cache)
    updateetfstr = '{0}.update_endtime'.format(job_cache)
    if 'fun' not in load and load.get('return', {}):
        ret_ = load.get('return', {})
        if 'fun' in ret_:
            load.update({'fun': ret_['fun']})
        if 'user' in ret_:
            load.update({'user': ret_['user']})

    # Try to reach returner methods
    try:
        savefstr_func = mminion.returners[savefstr]
        getfstr_func = mminion.returners[getfstr]
        fstr_func = mminion.returners[fstr]
    except KeyError as error:
        emsg = "Returner '{0}' does not support function {1}".format(job_cache, error)
        log.error(emsg)
        raise KeyError(emsg)

    if job_cache != 'local_cache':
        try:
            mminion.returners[savefstr](load['jid'], load)
        except KeyError as e:
            log.error("Load does not contain 'jid': %s", e)

    mminion.returners[fstr](load)

    if (opts.get('job_cache_store_endtime')
            and updateetfstr in mminion.returners):
        mminion.returners[updateetfstr](load['jid'], endtime)
933963
def store_job(self):
        '''
        
        '''
        try:
            class_name = self.__class__.__name__.lower()
        except AttributeError:
            log.warning(
                'Unable to determine class name',
                exc_info_on_loglevel=logging.DEBUG
            )
            return True

        try:
            return self.opts['{0}_returns'.format(class_name)]
        except KeyError:
            # No such option, assume this isn't one we care about gating and
            # just return True.
            return True
65626
@PostConstruct
    public final void init() {
        long timeToKeepAfterAccessInMillis = this.jobQueue.getTimeToKeepAfterAccessInMillis();
        if (timeToKeepAfterAccessInMillis >= 0) {
            if (TimeUnit.SECONDS.toMillis(this.abandonedTimeout) >=
                    this.jobQueue.getTimeToKeepAfterAccessInMillis()) {
                final String msg =
                        String.format("%s abandonTimeout must be smaller than %s timeToKeepAfterAccess",
                                      getClass().getName(), this.jobQueue.getClass().getName());
                throw new IllegalStateException(msg);
            }
            if (TimeUnit.SECONDS.toMillis(this.timeout) >= this.jobQueue.getTimeToKeepAfterAccessInMillis()) {
                final String msg = String.format("%s timeout must be smaller than %s timeToKeepAfterAccess",
                                                 getClass().getName(), this.jobQueue.getClass().getName());
                throw new IllegalStateException(msg);
            }
        }
        CustomizableThreadFactory threadFactory = new CustomizableThreadFactory();
        threadFactory.setDaemon(true);
        threadFactory.setThreadNamePrefix("PrintJobManager-");

        PriorityBlockingQueue<Runnable> queue =
                new PriorityBlockingQueue<>(
                        this.maxNumberOfWaitingJobs,
                        (o1, o2) -> {
                            if (o1 instanceof JobFutureTask<?> &&
                                    o2 instanceof JobFutureTask<?>) {
                                Callable<?> callable1 = ((JobFutureTask<?>) o1).getCallable();
                                Callable<?> callable2 = ((JobFutureTask<?>) o2).getCallable();

                                if (callable1 instanceof PrintJob) {
                                    if (callable2 instanceof PrintJob) {
                                        return ThreadPoolJobManager.this.jobPriorityComparator
                                                .compare((PrintJob) callable1,
                                                         (PrintJob) callable2);
                                    }
                                    return 1;
                                } else if (callable2 instanceof PrintJob) {
                                    return -1;
                                }
                            }
                            return 0;
                        });
        /* The ThreadPoolExecutor uses a unbounded queue (though we are enforcing a limit in `submit()`).
         * Because of that, the executor creates only `corePoolSize` threads. But to use all threads,
         * we set both `corePoolSize` and `maximumPoolSize` to `maxNumberOfRunningPrintJobs`. As a
         * consequence, the `maxIdleTime` will be ignored, idle threads will not be terminated.
         */
        this.executor =
                new ThreadPoolExecutor(this.maxNumberOfRunningPrintJobs, this.maxNumberOfRunningPrintJobs,
                                       this.maxIdleTime, TimeUnit.SECONDS, queue, threadFactory) {
                    @Override
                    protected <T> RunnableFuture<T> newTaskFor(final Callable<T> callable) {
                        return new JobFutureTask<>(callable);
                    }

                    @Override
                    protected void beforeExecute(final Thread t, final Runnable runnable) {
                        if (!ThreadPoolJobManager.this.clustered && runnable instanceof JobFutureTask<?>) {
                            JobFutureTask<?> task = (JobFutureTask<?>) runnable;
                            if (task.getCallable() instanceof PrintJob) {
                                PrintJob printJob = (PrintJob) task.getCallable();
                                try {
                                    ThreadPoolJobManager.this.jobQueue
                                            .start(printJob.getEntry().getReferenceId());
                                } catch (RuntimeException e) {
                                    LOGGER.error("failed to mark job as running", e);
                                } catch (NoSuchReferenceException e) {
                                    LOGGER.error("tried to mark non-existing job as 'running': {}",
                                                 printJob.getEntry().getReferenceId(), e);
                                }
                            }
                        }
                        super.beforeExecute(t, runnable);
                    }
                };

        this.timer = Executors.newScheduledThreadPool(1, timerTask -> {
            final Thread thread = new Thread(timerTask, "Post result to registry");
            thread.setDaemon(true);
            return thread;
        });
        this.timer.scheduleAtFixedRate(new RegistryTask(), RegistryTask.CHECK_INTERVAL,
                                       RegistryTask.CHECK_INTERVAL, TimeUnit.MILLISECONDS);

        if (this.oldFileCleanUp) {
            this.cleanUpTimer = Executors.newScheduledThreadPool(1, timerTask -> {
                final Thread thread = new Thread(timerTask, "Clean up old files");
                thread.setDaemon(true);
                return thread;
            });
            this.cleanUpTimer.scheduleAtFixedRate(
                    this.workingDirectories.getCleanUpTask(), 0,
                    this.oldFileCleanupInterval, TimeUnit.SECONDS);
        }
    }
400919
public static JobId of(String project, String job) {
    return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build();
  }
1294858
private synchronized RunningJobImpl get(final String jobIdentifier) {
    final RunningJobImpl result = this.jobs.get(jobIdentifier);
    if (null == result) {
      throw new RuntimeException("Trying to get a RunningJob that is unknown: " + jobIdentifier);
    }
    return result;
  }
1060036
public static Job[] all() {
    List list = UKV.get(LIST);
    Job[] jobs = new Job[list==null?0:list._jobs.length];
    int j=0;
    for( int i=0; i<jobs.length; i++ ) {
      Job job = UKV.get(list._jobs[i]);
      if( job != null ) jobs[j++] = job;
    }
    if( j<jobs.length ) jobs = Arrays.copyOf(jobs,j);
    return jobs;
  }
747567
public void runJob()
    {
        try {
            this.currentJob = this.jobQueue.take();

            // Create a clean Execution Context
            ExecutionContext context = new ExecutionContext();

            try {
                this.executionContextManager.initialize(context);
            } catch (ExecutionContextException e) {
                throw new RuntimeException("Failed to initialize Job " + this.currentJob + " execution context", e);
            }

            this.currentJob.run();
        } catch (InterruptedException e) {
            // Thread has been stopped
        } finally {
            this.execution.removeContext();
        }
    }
671737
private Job createJob() {
    Job job = new Job();
    JobConfiguration jobConfig = new JobConfiguration();
    JobConfigurationLoad loadConfig = new JobConfigurationLoad();
    jobConfig.setLoad(loadConfig);
    job.setConfiguration(jobConfig);

    loadConfig.setAllowQuotedNewlines(false);
    loadConfig.setSourceFormat("NEWLINE_DELIMITED_JSON");

    List<String> sources = new ArrayList<String>();
    for (GcsFilename file : fileSet) {
      sources.add(getFileUri(file));
    }

    loadConfig.setSourceUris(sources);

    TableReference tableRef = new TableReference();
    tableRef.setDatasetId(dataset);
    tableRef.setTableId(tableName);
    tableRef.setProjectId(projectId);
    loadConfig.setDestinationTable(tableRef);
    loadConfig.setSchema(schema.getValue());
    return job;
  }
1062422
@Override
  public JobID getNewJobId() throws IOException {
    int value = jobCounter.incrementAndGet();
    if (value > 1) {
      throw new RuntimeException(
        "CoronaJobTracker can only run one job! (value=" + value + ")");
    }
    createSession();
    // the jobtracker can run only a single job. it's jobid is fixed based
    // on the sessionId.
    jobId = jobIdFromSessionId(sessionId);
    return jobId;
  }
66498
private Put getJobStatusPut() {
    Put pStatus = new Put(jobKeyBytes);
    byte[] valueBytes = Bytes.toBytes(this.jobStatus);
    byte[] qualifier = Bytes.toBytes(JobHistoryKeys.JOB_STATUS.toString().toLowerCase());
    pStatus.addColumn(Constants.INFO_FAM_BYTES, qualifier, valueBytes);
    return pStatus;
  }
1229037
function () {
                    //Check if any input was added dynamically
                    if (job.input.length > 0) {
                        job.is_complete = false;
                        return false;
                    }

                    //Wait for workers or instances that are still working
                    return worker_count > 0 ? areWorkersComplete() : job.instances <= 0;
                }
144187
def enqueue(self, job):
    
    if job.queue_name():
      raise EnqueueError("job %s already queued!" % job.job_id)
    new_len = self.redis.lpush(self.queue_name, job.serialize())
    job.notify_queued(self)
    return new_len
1254426
public JobReport execute(Job job) {
        try {
            return executorService.submit(job).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Unable to execute job " + job.getName(), e);
        }
    }
16666
@Override
    public void setJobQueue(int idJob, int idQueue)
    {
        DbConn cnx = null;

        try
        {
            cnx = getDbSession();
            QueryResult qr = cnx.runUpdate("jj_update_queue_by_id", idQueue, idJob);

            if (qr.nbUpdated != 1)
            {
                throw new JqmClientException("Job instance does not exist or has already started");
            }
            cnx.commit();
        }
        catch (DatabaseException e)
        {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException)
                throw new JqmClientException("Queue does not exist", e);
            else
                throw new JqmClientException("could not change the queue of a job (internal error)", e);
        }
        catch (Exception e)
        {
            throw new JqmClientException("could not change the queue of a job (internal error)", e);
        }
        finally
        {
            closeQuietly(cnx);
        }
    }
1123480
@Override
    public void setJobQueue(int jobId, Queue queue)
    {
        JqmClientFactory.getClient().setJobQueue(jobId, queue);
    }