<commit_before><?php
namespace models;

class ScheduledTask extends \Core\Model
{
	protected $guarded = array();
	protected $primaryKey = '_id';

	public function app() {
		return $this->belongsTo('models\App');
	}

	/**
	 * Current app scope
	 * @example
	 *     ScheduledTask::current()->delete()
	 */
	public function scopeCurrent($query) {
		return $query->where('app_id', App::currentId());
	}

	public function getCommand() {
		$shortcuts = array(
			'hourly'  => '0 * * * *',
			'daily'   => '0 0 * * *',
			'monthly' => '0 0 1 * *',
			'weekly'  => '0 0 * * 0'
		);
		$schedule = preg_match('/[a-z]/', $this->schedule) ? $shortcuts[$this->schedule] : $this->schedule;

		$protocol = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http');
		$public_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $_SERVER["SCRIPT_NAME"] . '/' . $this->task;

		// TODO: redirect output to application log file.
		// https://github.com/doubleleft/dl-api/issues/37
		return $schedule . ' ' . "curl -XGET -H 'X-App-Id: {$this->app_id}' -H 'X-App-Key: {$this->app->keys[0]->key}' '{$public_url}' 2>&1 /dev/null";
	}

	public function toArray() {
		$arr = parent::toArray();
		$arr['command'] = $this->getCommand();
		return $arr;
	}

	public static function install() {
		exec('crontab ' . __DIR__ . '/../storage/crontabs/*.cron', $output, $return_code);
		return $return_code === 0;

		// $tasks = array();
		// static::all()->each(function($task) use (&$tasks) {
		// 	array_push($tasks, $task->toString());
		// });
		// file_put_contents(__DIR__ . '/../storage/crontab', join("\n", $tasks));
	}

}


<commit_msg>force server port for scheduled tasks. #46<commit_after><?php
namespace models;

class ScheduledTask extends \Core\Model
{
	protected $guarded = array();
	protected $primaryKey = '_id';

	public function app() {
		return $this->belongsTo('models\App');
	}

	/**
	 * Current app scope
	 * @example
	 *     ScheduledTask::current()->delete()
	 */
	public function scopeCurrent($query) {
		return $query->where('app_id', App::currentId());
	}

	public function getCommand() {
		$shortcuts = array(
			'hourly'  => '0 * * * *',
			'daily'   => '0 0 * * *',
			'monthly' => '0 0 1 * *',
			'weekly'  => '0 0 * * 0'
		);
		$schedule = preg_match('/[a-z]/', $this->schedule) ? $shortcuts[$this->schedule] : $this->schedule;

		$protocol = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http');
		$public_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['SCRIPT_NAME'] . '/' . $this->task;

		// TODO: redirect output to application log file.
		// https://github.com/doubleleft/dl-api/issues/37
		return $schedule . ' ' . "curl -XGET -H 'X-App-Id: {$this->app_id}' -H 'X-App-Key: {$this->app->keys[0]->key}' '{$public_url}' 2>&1 /dev/null";
	}

	public function toArray() {
		$arr = parent::toArray();
		$arr['command'] = $this->getCommand();
		return $arr;
	}

	public static function install() {
		exec('crontab ' . __DIR__ . '/../storage/crontabs/*.cron', $output, $return_code);

		if (!empty($output)) {
			throw new Exception(json_encode($output));
		}

		return $return_code === 0;

		// $tasks = array();
		// static::all()->each(function($task) use (&$tasks) {
		// 	array_push($tasks, $task->toString());
		// });
		// file_put_contents(__DIR__ . '/../storage/crontab', join("\n", $tasks));
	}

}



{'fraction_non_alphanumeric': 0.23040659988214496, 'fraction_numerical': 0.009428403064230996, 'mean_word_length': 3.514627659574468, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '6937367', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1200, 'n_words': 242}