public function run() { if (fnmatch('*cli*', php_sapi_name())) { $dir = Config::get('dir.schedules', APPLICATION_PATH . DS . 'schedules'); if (is_dir($dir)) { Timer::start(); Cli::show("Start of execution", 'COMMENT'); $files = glob($dir . DS . '*.php'); foreach ($files as $file) { require_once $file; $object = str_replace('.php', '', Arrays::last(explode(DS, $file))); $class = 'Thin\\' . ucfirst(Inflector::camelize($object . '_schedule')); $instance = lib('app')->make($class); $methods = get_class_methods($instance); Cli::show("Start schedule '{$object}'", 'COMMENT'); foreach ($methods as $method) { $when = $this->getWhen($instance, $method); $isDue = $this->isDue($object, $method, $when); if (true === $isDue) { Cli::show("Execution of {$object}->{$method}", 'INFO'); $instance->{$method}(); } else { Cli::show("No need to execute {$object}->{$method}", 'QUESTION'); } } } Cli::show("Time of execution [" . Timer::get() . " s.]", 'SUCCESS'); Cli::show("end of execution", 'COMMENT'); } } }
public function run($name, $when, $event, $args = []) { Timer::start(); Cli::show("Start of execution", 'SUCCESS'); $db = rdb('cron', 'task'); $dbCron = $db->firstOrCreate(['name' => $name]); $nextDb = $dbCron->next; $cron = CronExpression::factory($when); $next = $cron->getNextRunDate()->format('Y-m-d-H-i-s'); list($y, $m, $d, $h, $i, $s) = explode('-', $next, 6); $timestamp = mktime($h, $i, $s, $m, $d, $y); if ($nextDb) { if ($nextDb < $timestamp) { Cli::show("Execution {$name}", 'COMMENT'); call_user_func_array($event, $args); $dbCron->setNext($timestamp)->save(); } } else { $dbCron->setNext($timestamp)->save(); } Cli::show('Elapsed time: ' . Timer::get() . ' s.', 'INFO'); Cli::show("End of execution", 'SUCCESS'); }
private function authorize($publicKey, $privateKey) { $exists = Raw::ApiUser()->where(['public_key', '=', (string) $publicKey])->where(['private_key', '=', (string) $privateKey])->first(true); if ($exists) { $token = Utils::token(); $row = Raw::ApiAuth()->firstOrCreate(['user_id' => (int) $exists->id]); $row->setToken($token)->setExpiration(time() + 3600)->save(); Api::render(['status' => 200, 'execution_time' => Timer::get(), 'token' => $token]); } Api::forbidden(); }
public static function showStats() { Timer::stop(); $executionTime = Timer::get(); $queries = null === Utils::get('NbQueries') ? 0 : Utils::get('NbQueries'); $valQueries = $queries < 2 ? 'SQL Query executed' : 'SQL Queries executed'; $SQLDuration = null === Utils::get('SQLTotalDuration') ? 0 : Utils::get('SQLTotalDuration'); $queriesNoSQL = \Dbjson\Dbjson::$queries; $valQueriesNoSQL = $queriesNoSQL < 2 ? 'NoSQL Query executed' : 'NoSQL Queries executed'; $SQLDurationNoSQL = number_format(\Dbjson\Dbjson::$duration, 6); $execPHPSQL = $executionTime - $SQLDuration; $execPHPNoSQL = $executionTime - $SQLDurationNoSQL; $execPHP = $executionTime - $SQLDuration - $SQLDurationNoSQL; $PCPhp = round($execPHP / $executionTime * 100, 2); $PCPhpSQL = round($execPHPSQL / $executionTime * 100, 2); $PCPhpNoSQL = round($execPHPNoSQL / $executionTime * 100, 2); $PCSQL = 100 - $PCPhpSQL; $PCNoSQL = 100 - $PCPhpNoSQL; $included = count(get_included_files()); return "\n<!--\n\n\tPage generated in {$executionTime} s. by Thin Framework (C) www.geraldplusquellec.me 1996 - " . date('Y') . "\n\t{$queries} {$valQueries} in {$SQLDuration} s. (" . $PCSQL . " %)\n\t{$queriesNoSQL} {$valQueriesNoSQL} in {$SQLDurationNoSQL} s. (" . $PCNoSQL . " %)\n\tPHP Execution {$execPHP} s. ({$PCPhp} %)\n\n\n\n\t" . $included . " scripts included\n\tUsed Memory : " . convertSize(memory_get_usage()) . "\n\n-->"; }
public function makeBackup($database = null) { set_time_limit(0); $database = is_null($database) ? SITE_NAME : $database; $db = $this->getOdm(); $collections = $db->getCollectionNames(); $key = array_search('zelift.ages', $collections); if (strlen($key)) { unset($collections[$key]); } $key = array_search('zelift.counters', $collections); if (strlen($key)) { unset($collections[$key]); } $key = array_search('zelift.tuples', $collections); if (strlen($key)) { unset($collections[$key]); } $key = array_search('zelift.caching', $collections); if (strlen($key)) { unset($collections[$key]); } $key = array_search('zelift.ages', $collections); if (strlen($key)) { unset($collections[$key]); } $i = 0; foreach ($collections as $coll) { $path = \Thin\Config::get('application.backup_dir'); if (!is_dir($path)) { return false; } list($collDb, $collTable) = explode('.', $coll, 2); if ($collDb != $database) { continue; } $path = $path . DS . $collDb; if (!is_dir($path)) { File::mkdir($path); } $path = $path . DS . $collTable; if (!is_dir($path)) { File::mkdir($path); } $model = self::instance($collDb, $collTable); $cursor = $model->cursor(); while ($row = $cursor->fetch()) { if (isset($row['id'])) { $file = $path . DS . $row['id'] . '.json'; File::put($file, json_encode($row)); $i++; } } } $now = date("d_m_Y_H_i_s"); $path = \Thin\Config::get('application.backup_dir', false); $user = \Thin\Config::get('redis.ftp.backup.user', false); $password = \Thin\Config::get('redis.ftp.backup.password', false); $host = \Thin\Config::get('redis.ftp.backup.host', false); if (false !== $path && false !== $user && false !== $password && false !== $host) { $cmd = "cd {$path} && tar cfvz zelift_{$now}.tar.gz {$path}\nlftp -e 'put {$path}/zelift_{$now}.tar.gz; bye' -u \"{$user}\",{$password} {$host}\nrm zelift_{$now}.tar.gz\necho 'done'"; passthru($cmd); } vd($i); dd(Timer::get()); }