{"size": 1111, "ext": "js", "max_stars_repo_path": "lib/cron.js", "max_stars_repo_name": "realglobe-Inc/pon-task-basic", "max_stars_repo_stars_event_min_datetime": "2018-01-18T07:32:21.000Z", "max_stars_repo_stars_event_max_datetime": "2018-01-18T07:32:21.000Z", "max_issues_repo_path": "lib/cron.js", "max_issues_repo_name": "realglobe-Inc/pon-task-basic", "max_issues_repo_issues_event_min_datetime": "2021-05-06T17:45:10.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-20T22:02:15.000Z", "max_forks_repo_path": "lib/cron.js", "max_forks_repo_name": "realglobe-Inc/pon-task-basic", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 25.25, "max_line_length": 74, "alphanum_fraction": 0.6075607561}
/**
 * Pon task to define cron
 * @function cron
 * @param {string} when - Cron time
 * @param {string|function} task - Task to run
 * @param {Object} [options={}] - Command
 * @returns {function} Defined task
 */
'use strict'

const {CronJob} = require('cron')
const task = require('pon-task-task')
const {spawn} = require('pon-task-command')

/** @lends cron */
function cron (when, run, options = {}) {
  return function definedTask (ctx) {
    const {logger} = ctx
    if (typeof run === 'string') {
      run = spawn(run)
    }
    const {
      onComplete = null,
      timeZone,
    } = options
    const runWRap = async () => {
      logger.info(`A crone tick started (${new Date().toLocaleString()})`)
      await run(ctx)
      logger.info(`...tick finished (${new Date().toLocaleString()})`)
    }
    const job = new CronJob(when, runWRap, onComplete, true, timeZone)
    const stop = () => job.stop()
    return Object.assign(stop, {
      job,
    })
  }
}

cron.pon = function cornPonTask (when, taskName, options = {}) {
  return cron(when, task.pon(taskName), options)
}

module.exports = cron
