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()); }
public static function store($type, $flat, $key = null) { $settings = Arrays::exists($type, static::$_settings) ? static::$_settings[$type] : static::defaultConfig($type); $hook = Arrays::exists('store', $settings) ? $settings['store'] : null; static::_hook($hook, func_get_args(), 'before'); $checkTuple = Arrays::exists('checkTuple', $settings) ? $settings['checkTuple'] : null; $indexes = Arrays::exists('indexes', $settings) ? $settings['indexes'] : null; if (Arrays::exists('beforeStore', $settings)) { $settings['beforeDelete']($type, $flat); } static::event($type); $dir = static::checkDir($type); $object = new $type(); $flat = static::_sanitize($flat); $object->populate($flat); $serialize = static::serialize($object); $edit = !is_null($key); $add = !$edit; if (true === $add) { $key = static::makeKey($type); } $versionFile = STORAGE_PATH . DS . 'data' . DS . $dir . DS . 'versions' . DS . $key . '_' . Timer::getMS() . '.data'; if (true === $edit) { $oldFile = STORAGE_PATH . DS . 'data' . DS . $dir . DS . 'read' . DS . $key . '.data'; $oldObject = static::getIt($type, $oldFile); if (count($indexes)) { foreach ($indexes as $indexField => $indexInfo) { static::indexRemove($indexField, $indexInfo, static::getObject($oldFile)); } } if (File::exists($oldFile)) { File::delete($oldFile); } } if (!empty($checkTuple)) { if (is_string($checkTuple)) { $db = dm($type); $res = $db->where($checkTuple . ' = ' . $flat[$checkTuple])->get(); } if (Arrays::is($checkTuple)) { $query = ''; foreach ($checkTuple as $ct) { $query .= $ct . ' = ' . $flat[$ct] . ' && '; } $query = substr($query, 0, -4); $tabConditions = explode(' && ', $query); $init = true; foreach ($tabConditions as $cond) { $db = dm($type); $res = $db->where($cond)->sub(); if (true === $init) { $init = false; $results = $res; } else { $results = array_intersect($results, $res); } } $db = dm($type); $res = $db->get($results); } if (count($res)) { $row = $db->first($res); static::_hook($hook, array("thin_type" => $type) + $flat, 'after'); return $row; } } $file = repl(DS . 'versions' . DS, DS . 'write' . DS, $versionFile); File::delete($file); File::put($file, $serialize); $fields = Arrays::exists($type, static::$_fields) ? static::$_fields[$type] : static::noConfigFields($type); $versioning = false; if (count($fields)) { foreach ($fields as $field => $info) { $val = $object->{$field}; if (empty($val)) { if (!Arrays::exists('canBeNull', $info)) { if (!Arrays::exists('default', $info)) { static::_hook($hook, func_get_args(), 'after'); throw new Exception('The field ' . $field . ' cannot be null.'); } else { $object->{$field} = $info['default']; } } } else { if (Arrays::exists('sha1', $info)) { if (!preg_match('/^[0-9a-f]{40}$/i', $val) || strlen($val) != 40) { $object->{$field} = sha1($val); } } elseif (Arrays::exists('md5', $info)) { if (!preg_match('/^[0-9a-f]{32}$/i', $val) || strlen($val) != 32) { $object->{$field} = md5($val); } } } } } if (Arrays::exists('versioning', $settings)) { $versioning = $settings['versioning']; } if (false !== $versioning) { File::put($versionFile, $serialize); } if (count($indexes)) { $thisObject = static::getById($type, $object->getId()); foreach ($indexes as $indexField => $indexInfo) { static::indexCreate($indexField, $indexInfo, $thisObject); } static::commit(); } if (Arrays::exists('afterStore', $settings)) { if ($settings['afterStore'] instanceof \Closure) { $settings['afterStore']($type, $flat); } } static::_hook($hook, func_get_args(), 'after'); static::emptyCache($type); return $object; }