コード例 #1
0
 /**
  * 初始化
  *
  * @access public static
  * @return void
  */
 public static function init($expire = 60)
 {
     self::$expire = (int) $expire;
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
 }
コード例 #2
0
 public function execute()
 {
     if (!$this->isReady('thedate', 'priority')) {
         return self::IGNO;
     }
     $date1 = date('Ymd', strtotime(Setting::get('last_date')));
     $date2 = date('Ymd', strtotime($this->option('thedate')));
     if ($date1 >= $date2) {
         return self::IGNO;
     }
     $mysql = \Myfox\Lib\Mysql::instance('default');
     $count = (int) $mysql->getOne($mysql->query(sprintf('SELECT COUNT(*) FROM %stask_queque WHERE autokid < %u AND priority <= %u ' . "AND task_flag NOT IN (%d,%d) AND task_type='import'", $mysql->option('prefix'), $this->id, $this->option('priority'), \Myfox\App\Queque::FLAG_IGNO, \Myfox\App\Queque::FLAG_DONE)));
     if ($count > 0) {
         $this->setError(sprintf('Waiting for %d import task(s)', $count));
         return self::FAIL;
     }
     if (!Router::flush()) {
         $this->setError('flush route failed');
         return self::FAIL;
     }
     Setting::set('last_date', $date2, '', '集群数据最新日期');
     // TODO:
     // CALL nodefox to reload route info info cache
     return self::SUCC;
 }
コード例 #3
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::$mysql->query(sprintf('TRUNCATE TABLE %stask_queque', self::$mysql->option('prefix', '')));
 }
コード例 #4
0
 /**
  * 清理失效的路由
  *
  * @access public static
  * @return void
  */
 public static function clean_invalidated_routes()
 {
     $mysql = \Myfox\Lib\Mysql::instance('default');
     $query = sprintf("SHOW TABLES LIKE '%sroute_info%%'", $mysql->option('prefix'));
     foreach ((array) $mysql->getAll($mysql->query($query)) as $table) {
         $table = end($table);
         $query = sprintf('SELECT autokid,hosts_list,real_table,route_text FROM %s WHERE route_flag IN (%d) AND modtime < %d', $table, Router::FLAG_IS_DELETED, time() - 100000);
         $ids = array();
         foreach ((array) $mysql->getAll($mysql->query($query)) as $row) {
             $hosts = trim($row['hosts_list'], '{}$');
             if (!empty($hosts)) {
                 $where = array();
                 foreach (Router::parse($row['route_text']) as $k => $v) {
                     $where[] = sprintf('%s = %s', $k, $v);
                 }
                 $info = array('host' => $hosts, 'path' => $row['real_table'], 'where' => implode(' AND ', $where));
                 if (!Queque::instance()->insert('delete', $info, 0, array('adduser' => 'cleaner'))) {
                     continue;
                 }
             }
             $ids[] = (int) $row['autokid'];
         }
         $mysql->query(sprintf('DELETE FROM %s WHERE (route_flag = %d AND addtime < %d)%s', $table, Router::FLAG_PRE_IMPORT, time() - 10 * 86400, empty($ids) ? '' : sprintf(' OR (autokid IN (%s))', implode(',', $ids))));
     }
 }
コード例 #5
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::removeAllNames();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
 }
コード例 #6
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     Table::cleanAllStatic();
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
 }
コード例 #7
0
 /**
  * 各个对象初始化 
  *
  * @access public static
  * @return void
  */
 public static function init($ini)
 {
     self::setAutoLoad();
     /**
      * @配置文件
      */
     \Myfox\Lib\Config::register('default', $ini);
     $config = \Myfox\Lib\Config::instance('default');
     /**
      * @数据库
      */
     foreach ((array) $config->get('mysql') as $name => $file) {
         \Myfox\Lib\Mysql::register($name, $file);
     }
     /**
      * @日志对象
      */
     foreach ((array) $config->get('log') as $name => $url) {
         \Myfox\Lib\Factory::registerLog($name, $url);
     }
     /**
      * @告警提醒
      */
     \Myfox\Lib\Alert::init(__DIR__ . '/../etc/alert.ini');
 }
コード例 #8
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::removeAllNames();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::$mysql->query(sprintf("DELETE FROM %shost_list WHERE host_name LIKE 'unittest%%'", self::$mysql->option('prefix', '')));
 }
コード例 #9
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::$mysql->query(sprintf('DELETE FROM %ssettings WHERE ownname LIKE "unittest%%"', self::$mysql->option('prefix')));
     Setting::$queries = 0;
 }
コード例 #10
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::cleanTable('default', 'route_info');
     self::cleanTable('default', 'task_queque');
 }
コード例 #11
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::$inifile = __DIR__ . '/ini/myfox.ini';
     self::cleanTable('default', 'process_locker');
 }
コード例 #12
0
 /**
  * 构造函数
  * @param void
  */
 public function __construct($option = array('m' => 'run'))
 {
     parent::__construct($option);
     $this->mysql = \Myfox\Lib\Mysql::instance('default');
     $hosts = $this->mysql->getAll($this->mysql->query(sprintf('SELECT host_id,host_name FROM %shost_list', $this->mysql->option('prefix'))));
     foreach ($hosts as $host) {
         $this->hosts[$host['host_id']] = $host['host_name'];
     }
 }
コード例 #13
0
ファイル: task.php プロジェクト: ray-dong/Myfox-load-module
 /**
  * 构造函数
  *
  * @access public
  * @param  Integer $id
  * @param  Array $option
  * @return void
  */
 public function __construct($id, $option, $status = '')
 {
     $this->id = (int) $id;
     $this->option = (array) $option;
     $this->status = $status;
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
 }
コード例 #14
0
 public function test_should_simple_query_works_fine()
 {
     $mysql = new Mysql(__DIR__ . '/ini/mysql_test.ini');
     try {
         $mysql->connectToSlave();
     } catch (\Exception $e) {
         $this->assertContains("\tCONNECT_ERROR\t-\t{\"host\":\"127.0.0.1\",\"port\":3306,\"user\":\"user_ro\",\"pass\":\"**\",\"error\":", self::getLogContents($this->logfile, -1));
     }
     $rs = self::$mysql->getAll(self::$mysql->query('SHOW DATABASES'));
     $this->assertContains("\tQUERY_OK\t-\t{\"sql\":\"SHOW DATABASES\"", self::getLogContents($this->logfile, -1));
     $this->assertContains(array('Database' => 'test'), $rs);
     $this->assertFalse(self::$mysql->query('I AM A WRONG QUERY'));
     $this->assertContains("\tQUERY_ERROR\t-\t{\"sql\":\"I AM A WRONG QUERY\",\"async\":false,\"error\":", self::getLogContents($this->logfile, -1));
     $this->assertEquals(1, self::$mysql->query('INSERT INTO only_for_test (content) VALUES ("aabbcc")'));
     $lastId = self::$mysql->lastId();
     $this->assertEquals($lastId, self::$mysql->getOne(self::$mysql->query('SELECT MAX(id) FROM only_for_test')));
     self::$mysql->query('INSERT INTO only_for_test (content) VALUES ("aabbcc2")');
     $this->assertTrue($lastId < self::$mysql->lastId());
 }
コード例 #15
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
     self::$mysql = \Myfox\Lib\Mysql::instance('default');
     self::$mysql->query(sprintf("DELETE FROM %stable_list WHERE table_name = 'i am not exists'", self::$mysql->option('prefix')));
     self::$mysql->query(sprintf("DELETE FROM %ssettings WHERE cfgname IN ('table_route_count', 'table_real_count')", self::$mysql->option('prefix')));
     self::cleanTable('default', 'route_info');
     Setting::set('last_assign_host', 0);
 }
コード例 #16
0
 protected static function cleanTable($mysql, $prefix)
 {
     $table = null;
     $mysql = \Myfox\Lib\Mysql::instance($mysql);
     $query = sprintf("SHOW TABLES LIKE '%s%s%%'", $mysql->option('prefix'), trim($prefix));
     foreach ((array) $mysql->getAll($mysql->query($query)) as $table) {
         $table = end($table);
         $mysql->query(sprintf('TRUNCATE TABLE %s', $table));
     }
     return $table;
 }
コード例 #17
0
 /**
  * 执行
  *
  * @access public
  * @return Boolean true or false (next loop)
  */
 public function execute($loop = true)
 {
     $mysql = \Myfox\Lib\Mysql::instance('default');
     $count = 0;
     $query = sprintf('SELECT host_id AS id, host_name AS name FROM %shost_list', $mysql->option('prefix'));
     foreach ((array) $mysql->getAll($mysql->query($query)) as $host) {
         $db = Server::instance($host['name'])->getlink();
         try {
             $databases = $db->getAll($db->query('SHOW DATABASES'));
         } catch (\Exception $e) {
             $this->log->error('EXCEPTION', $e->getMessage());
             continue;
         }
         foreach ((array) $databases as $dbname) {
             $dbname = reset($dbname);
             if (preg_match('/^(mysql|test|information_schema)$/', $dbname) || !preg_match('/^\\w+_\\d+$/', $dbname)) {
                 continue;
             }
             foreach ((array) $db->getAll($db->query('SHOW TABLE STATUS FROM ' . $dbname)) as $row) {
                 $table = sprintf('%s.%s', $dbname, $row['Name']);
                 $logvar = array('server' => $host['name'], 'table' => $table, 'engine' => $row['Engine'], 'create' => $row['Create_time'], 'update' => $row['Update_time'], 'check' => $row['Check_time']);
                 if (true !== $this->option['full'] && $row['Check_time'] > $row['Update_time'] && !empty($row['Engine'])) {
                     $this->log->debug('CHECK_IGN1', $logvar);
                     continue;
                 }
                 switch (self::realcheck($table, $db)) {
                     case 0:
                         $this->log->debug('CHECK_IGN2', $logvar);
                         break;
                     case 1:
                         $this->log->notice('CHECK_OK', $logvar);
                         break;
                     default:
                         $this->log->debug('CHECK_FAIL', $logvar);
                         break;
                 }
                 if (++$count % 10 == 0) {
                     self::breakup();
                 }
             }
         }
     }
     return false;
 }
コード例 #18
0
    /**
     * 测试表及信息创建
     */
    protected function setUp()
    {
        parent::setUp();
        \Myfox\Lib\Mysql::removeAllNames();
        \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
        $this->mysql = \Myfox\Lib\Mysql::instance('default');
        $config = new \Myfox\Lib\Config(__DIR__ . '/ini/mysql.ini');
        foreach ($config->get('master') as $host) {
            $urlRes = parse_url($host);
            $this->infos['host'] = $urlRes['host'];
            $this->infos['port'] = array_key_exists('port', $urlRes) ? $urlRes['port'] : 3306;
            $this->infos['user'] = array_key_exists('user', $urlRes) ? $urlRes['user'] : '******';
            $this->infos['pass'] = array_key_exists('pass', $urlRes) ? $urlRes['pass'] : '';
        }
        self::cleanTable('default', 'route_info');
        $this->table_bak('table_list');
        $this->table_bak('host_list');
        $this->table_bak('settings');
        //插入测试分片表1
        $this->mysql->query('CREATE TABLE `test_table_real1` (
			`id` int(10) unsigned NOT NULL auto_increment,
			`key1` varchar(64) NOT NULL default "",
			`key2` int(10) NOT NULL default 0,
			PRIMARY KEY (`id`)
		)');
        //插入测试分片表2
        $this->mysql->query('CREATE TABLE `test_table_real2` (
			`id` int(10) unsigned NOT NULL auto_increment,
			`key1` varchar(64) NOT NULL default "",
			`key2` int(10) NOT NULL default 0,
			PRIMARY KEY (`id`)
		)');
        //插入测试分片表3
        $this->mysql->query('CREATE TABLE `test_table_real3` (
			`id` int(10) unsigned NOT NULL auto_increment,
			`key1` varchar(64) NOT NULL default "",
			`key2` int(10) NOT NULL default 0,
			PRIMARY KEY (`id`)
		)');
    }
コード例 #19
0
 /**
  * 构造函数
  *
  * @access private
  * @return void
  */
 private function __construct($name)
 {
     $this->name = empty($name) ? '' : '_' . $name;
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
     if (empty(self::$mypos)) {
         self::$mypos = Context::addr(true);
     }
 }
コード例 #20
0
 /**
  * 释放锁
  *
  * @access public
  * @return void
  */
 public function freelock()
 {
     $name = strtolower(trim($this->worker->locker()));
     if (empty($name)) {
         return;
     }
     $mysql = \Myfox\Lib\Mysql::instance('default');
     return $mysql->query(sprintf("UPDATE %sprocess_locker SET wholock='' WHERE lockkey='%s' AND lockenv='%s' AND wholock='%s'", $mysql->option('prefix'), $mysql->escape($name), $mysql->escape(self::$runmode), $mysql->escape(self::$identy)));
 }
コード例 #21
0
 public function test_should_mysql_reconnect_when_gone_away_works_fine()
 {
     $mysql = new Mysql(__DIR__ . '/ini/mysql.ini');
     $result = $mysql->getAll($mysql->query('SHOW DATABASES'));
     $this->assertEquals(0, $mysql->query(sprintf('KILL %d', $mysql->thread_id)));
     $this->assertEquals($result, $mysql->getAll($mysql->query('SHOW DATABASES')));
 }
コード例 #22
0
 /**
  * 构造函数
  *
  * @access private
  * @return void
  */
 private function __construct($tbname)
 {
     $this->tbname = strtolower(trim($tbname));
     $this->option = null;
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
 }
コード例 #23
0
ファイル: router.php プロジェクト: ray-dong/Myfox-load-module
 /**
  * 构造函数
  *
  * @access private
  * @return void
  */
 private function __construct($tbname)
 {
     $this->table = Table::instance($tbname);
     if (!$this->table->get('autokid')) {
         throw new \Myfox\Lib\Exception(sprintf('Undefined table named as "%s"', $tbname));
     }
     $this->tbname = $this->table->get('table_name', '');
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
     if (null === self::$hostall || null === self::$onlines) {
         self::metadata();
     }
 }
コード例 #24
0
ファイル: server.php プロジェクト: ray-dong/Myfox-load-module
 /**
  * 构造函数
  *
  * @access private
  * @return void
  */
 private function __construct($name)
 {
     if (empty(self::$mysql)) {
         self::$mysql = \Myfox\Lib\Mysql::instance('default');
     }
     $this->dbname = $name;
     $this->option = null;
 }
コード例 #25
0
 /**
  * 根据ID获取机器名字
  *
  * @access private static
  * @return Mixture
  */
 private static function hostname($id)
 {
     if (time() - self::$last_ts >= 300) {
         $mysql = \Myfox\Lib\Mysql::instance('default');
         $query = sprintf('SELECT host_id, host_name FROM %shost_list', $mysql->option('prefix'));
         self::$hostmap = array();
         foreach ($mysql->getAll($mysql->query($query)) as $row) {
             self::$hostmap[(int) $row['host_id']] = trim($row['host_name']);
         }
     }
     return isset(self::$hostmap[$id]) ? self::$hostmap[$id] : null;
 }
コード例 #26
0
 protected function setUp()
 {
     parent::setUp();
     \Myfox\Lib\Mysql::register('default', __DIR__ . '/ini/mysql.ini');
 }