{"size": 2324, "ext": "js", "max_stars_repo_path": "libs/ParsedSchedule.js", "max_stars_repo_name": "jagermesh/sys-watcher", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "libs/ParsedSchedule.js", "max_issues_repo_name": "jagermesh/sys-watcher", "max_issues_repo_issues_event_min_datetime": "2019-09-18T10:20:56.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-01T18:24:06.000Z", "max_forks_repo_path": "libs/ParsedSchedule.js", "max_forks_repo_name": "jagermesh/sys-watcher", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 355, "max_forks_count": null, "avg_line_length": 24.2083333333, "max_line_length": 132, "alphanum_fraction": 0.6092943201}
const parseDuration = require('parse-duration');
const moment = require('moment');
const md5 = require('md5');
const fs = require('fs');
const os = require('os');

class ParsedSchedule {

  constructor(scheduler, schedule) {
    const re = /every[ ]+([a-z]+)[ ]+at[ ]+([0-9:amp]+)/i;

    this.schedule = schedule;
    this.scheduler = scheduler;
    this.scheduleRe = re.exec(this.schedule);
    this.scheduleDay = null;
    this.scheduleTime = null;
    if (this.scheduleRe) {
      this.scheduleDay = this.scheduleRe[1].toLowerCase();
      this.scheduleTime = moment(this.scheduleRe[2], 'h:ma');
    }
    this.scheduleMs = parseDuration(this.schedule);
    this.scheduleFile = `${os.tmpdir()}/${md5(this.scheduler.getName() + this.schedule)}.txt`;
    this.applicableDays = [ 'day', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ];

    if (!this.isValid()) {
      throw new Error(`Schedule is invalid: "${this.schedule}"`);
    }
  }

  isValid() {
    return !!this.scheduleMs || (!!this.scheduleRe && (this.applicableDays.indexOf(this.scheduleDay) != -1));
  }

  isTimeToRun() {

    if (!this.scheduleRe) {
      return true;
    }

    let currentDay = moment().format('dddd').toLowerCase();

    if (this.scheduleTime.isBefore() && ((this.scheduleDay == 'day') || (this.scheduleDay == currentDay))) {
      let marker;
      try {
        marker = fs.readFileSync(this.scheduleFile, 'utf8');
      } catch (error) {
        fs.writeFileSync(this.scheduleFile, moment().startOf('day').format());
        marker = fs.readFileSync(this.scheduleFile, 'utf8');
      }
      let lastTime = moment(marker);
      if (lastTime.isBefore(this.scheduleTime)) {
        return true;
      }
    }

    return false;

  }

  touchMarker() {

    if (this.scheduleRe) {
      this.scheduler.getApplication().getConsole().log(`Touching marker ${this.scheduleFile}.`, Object.create({ }), this.scheduler);
      fs.writeFileSync(this.scheduleFile, moment().format());
    }

  }

  getMs() {

    if (this.scheduleRe) {
      return 60*1000;
    }

    if (!this.scheduleMs) {
      return 60*1000;
    }

    return this.scheduleMs;

  }

  getRule() {

    if (this.scheduleRe) {
      return this.schedule;
    }

    return `every ${this.schedule}`;

  }

}

module.exports = ParsedSchedule;
