<commit_before><?php namespace GO;

class Scheduler
{

  /**
   * Timezone
   */
  private $timezone = 'Europe/Dublin';

  /**
   * The jobs that need to run now
   */
  private $jobs = [];

  /**
   * Where to send the output of the job
   */
  private $output = '/dev/null';

  /**
   * Init the datetime
   *
   */
  public function __construct()
  {
    $this->dt = new \DateTime('now');
    $this->dt->setTimezone(new \DateTimeZone($this->timezone));
  }

  /**
   * Run the scheduled commands
   *
   */
  public function run()
  {
    if (count($this->jobs) == 0) {
      echo 'Nothing to do' . PHP_EOL;
      exit(1);
    }

    foreach ($this->jobs as $j) {
      echo 'Executing ' . $j . PHP_EOL;
      $this->exec($j);
    }
  }

  /**
   * Schedule a command at a given time
   *
   * @param [string] $command - command to execute
   * @param [string] $cron - cron schedule
   *
   */
  public function schedule($command, $cron)
  {
    if ($this->isDue($cron)) {
      array_push($this->jobs, $command);
    }
  }

  /**
   * Check if a schedule is due now
   *
   * @param [string] $cron - cron schedule
   *
   * @return [bool]
   */
  private function isDue($cron)
  {
    $time = explode(' ', $cron);

    $due = [
      'minute'        => $time[0],
      'hour'          => $time[1],
      'dayOfTheMonth' => $time[2],
      'month'         => $time[3],
      'dayOfTheWeek'  => $time[4],
    ];

    $now = [
      'minute'        => $this->dt->format('i'),
      'hour'          => $this->dt->format('H'),
      'dayOfTheMonth' => $this->dt->format('d'),
      'month'         => $this->dt->format('m'),
    ];

    if ($due['minute'] != $now['minute'] && $due['minute'] != '*') {
      return false;
    }

    if ($due['hour'] != $now['hour'] && $due['hour'] != '*') {
      return false;
    }

    if ($due['dayOfTheMonth'] != $now['dayOfTheMonth'] && $due['dayOfTheMonth'] != '*') {
      return false;
    }

    if ($due['month'] != $now['month'] && $due['month'] != '*') {
      return false;
    }

    return true;
  }

  /**
   * Execute the command
   *
   * @param [string] $command - command to execute
   *
   */
  private function exec($command)
  {
    $output = is_dir($this->output) ? $this->output.'/'.(str_replace('.', '_', basename($command))).'.log' : $this->output;
    
    $command = PHP_BINARY . ' ' . $command . ' 1>> ' . $output . ' 2>&1 &';
    exec($command);
  }

  /**
   * Set the timezone
   *
   * @param [string] timezone
   *
   */
  public function setTimezone($timezone)
  {
    $this->timezone = $timezone;<commit_msg>Fix script name when running commands with args<commit_after><?php namespace GO;

class Scheduler
{

  /**
   * Timezone
   */
  private $timezone = 'Europe/Dublin';

  /**
   * The jobs that need to run now
   */
  private $jobs = [];

  /**
   * Where to send the output of the job
   */
  private $output = '/dev/null';

  /**
   * Init the datetime
   *
   */
  public function __construct()
  {
    $this->dt = new \DateTime('now');
    $this->dt->setTimezone(new \DateTimeZone($this->timezone));
  }

  /**
   * Run the scheduled commands
   *
   */
  public function run()
  {
    if (count($this->jobs) == 0) {
      echo 'Nothing to do' . PHP_EOL;
      exit(1);
    }

    foreach ($this->jobs as $j) {
      echo 'Executing ' . $j . PHP_EOL;
      $this->exec($j);
    }
  }

  /**
   * Schedule a command at a given time
   *
   * @param [string] $command - command to execute
   * @param [string] $cron - cron schedule
   *
   */
  public function schedule($command, $cron)
  {
    if ($this->isDue($cron)) {
      array_push($this->jobs, $command);
    }
  }

  /**
   * Check if a schedule is due now
   *
   * @param [string] $cron - cron schedule
   *
   * @return [bool]
   */
  private function isDue($cron)
  {
    $time = explode(' ', $cron);

    $due = [
      'minute'        => $time[0],
      'hour'          => $time[1],
      'dayOfTheMonth' => $time[2],
      'month'         => $time[3],
      'dayOfTheWeek'  => $time[4],
    ];

    $now = [
      'minute'        => $this->dt->format('i'),
      'hour'          => $this->dt->format('H'),
      'dayOfTheMonth' => $this->dt->format('d'),
      'month'         => $this->dt->format('m'),
    ];

    if ($due['minute'] != $now['minute'] && $due['minute'] != '*') {
      return false;
    }

    if ($due['hour'] != $now['hour'] && $due['hour'] != '*') {
      return false;
    }

    if ($due['dayOfTheMonth'] != $now['dayOfTheMonth'] && $due['dayOfTheMonth'] != '*') {
      return false;
    }

    if ($due['month'] != $now['month'] && $due['month'] != '*') {
      return false;
    }

    return true;
  }

  /**
   * Execute the command
   *
   * @param [string] $command - command to execute
   *
   */
  private function exec($command)
  {
    $script = explode(' ', basename($command))[0];

    $output = is_dir($this->output) ? $this->output.'/'.(str_replace('.', '_', $script)).'.log' : $this->output;
    
    $command = PHP_BINARY . ' ' . $command . ' 1>> ' . $output . ' 2>&1 &';
    echo 'Exec ' . $command . PHP_EOL;
    exec($command);
  }

  /**
   * Set the timezone
   *
   * @param [string] timezone
   *
   */
  public function setTimezone($timezone)
  {
    $this->timezone = $timezone;
{'fraction_non_alphanumeric': 0.20542488619119878, 'fraction_numerical': 0.003983308042488619, 'mean_word_length': 2.118273211117682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5133177', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1853, 'n_words': 397}