{"size": 2080, "ext": "js", "max_stars_repo_path": "packages/job-utils/src/index.js", "max_stars_repo_name": "launchql/jobs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/job-utils/src/index.js", "max_issues_repo_name": "launchql/jobs", "max_issues_repo_issues_event_min_datetime": "2020-07-28T03:14:03.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-04T08:14:03.000Z", "max_forks_repo_path": "packages/job-utils/src/index.js", "max_forks_repo_name": "launchql/jobs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 26.6666666667, "max_line_length": 78, "alphanum_fraction": 0.6206730769}
import env from './env';
const { JOBS_SCHEMA } = env;

export const failJob = async (client, { workerId, jobId, message }) => {
  console.log(`utils:failJob worker[${workerId}] job[${jobId}] ${message}`);
  await client.query(`SELECT * FROM "${JOBS_SCHEMA}".fail_job($1, $2, $3);`, [
    workerId,
    jobId,
    message
  ]);
};

export const completeJob = async (client, { workerId, jobId }) => {
  console.log(`utils:completeJob worker[${workerId}] job[${jobId}]`);
  await client.query(`SELECT * FROM "${JOBS_SCHEMA}".complete_job($1, $2);`, [
    workerId,
    jobId
  ]);
};

export const getJob = async (client, { workerId, supportedTaskNames }) => {
  console.log(`utils:getJob ${workerId}`);
  const {
    rows: [job]
  } = await client.query(
    `SELECT * FROM "${JOBS_SCHEMA}".get_job($1, $2::text[]);`,
    [workerId, supportedTaskNames]
  );
  return job;
};

export const getScheduledJob = async (
  client,
  { workerId, supportedTaskNames }
) => {
  console.log(`utils:getScheduledJob worker[${workerId}]`);
  const {
    rows: [job]
  } = await client.query(
    `SELECT * FROM "${JOBS_SCHEMA}".get_scheduled_job($1, $2::text[]);`,
    [workerId, supportedTaskNames]
  );
  return job;
};

export const runScheduledJob = async (client, { jobId }) => {
  console.log(`utils:runScheduledJob job[${jobId}]`);
  try {
    const {
      rows: [job]
    } = await client.query(
      `SELECT * FROM "${JOBS_SCHEMA}".run_scheduled_job($1);`,
      [jobId]
    );
    return job;
  } catch (e) {
    if (e.message === 'ALREADY_SCHEDULED') {
      return null;
    }
    throw e;
  }
};

export const releaseScheduledJobs = async (client, { workerId, ids }) => {
  console.log(`utils:releaseScheduledJobs worker[${workerId}]`);
  return await client.query(
    `SELECT "${JOBS_SCHEMA}".release_scheduled_jobs($1, $2::bigint[])`,
    [workerId, ids]
  );
};

export const releaseJobs = async (client, { workerId }) => {
  console.log(`utils:releaseJobs worker[${workerId}]`);
  return await client.query(`SELECT "${JOBS_SCHEMA}".release_jobs($1)`, [
    workerId
  ]);
};
