/** * Execute the job. * * @return void */ public function handle() { //读取好友和群消息 $login = new LoginController(); $login->getFriends($this->wxuin); do { //循环执行心跳 $user = Login::where('wxuin', $this->wxuin)->where('status', 1)->first(); if (!$user) { \Log::info("wxuin:{$this->wxuin} 冻结状态,结束此次循环"); return; } $cookies = json_decode($user->cookies); $url = "https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?r=" . t() . "&skey=" . urlencode($user->skey) . "&sid=" . urlencode($cookies->wxsid) . "&uin=" . urlencode($user->Uin) . "&deviceid=" . urlencode($user->deviceid) . "&synckey=" . to_url_synckey($user->SyncKey) . "&_=" . t(); $ret = CURL::send($url, ['Cookie' => urldecode(http_build_query($cookies, '', '; '))], ['follow_redirects' => false], ['ret' => 'all']); $html = $ret->body; $cookies2 = toCookies($ret->cookies); $cookies = (object) ((array) $cookies2 + (array) $cookies); $tmp = ['url' => $url, 'cookie' => urldecode(http_build_query($cookies, '', '; ')), 'html' => $html, 'ret_cookies' => (array) $cookies2]; \Log::info('心跳包:', $tmp); $data['retcode'] = str_tiqu($html, 'retcode:\\"', '\\"'); $data['selector'] = str_tiqu($html, 'selector:\\"', '\\"'); //判断消息 $this->retcode($data); \DB::reconnect(); //确保获取了一个新的连接。 } while (1); }
public function testReconnect() { $c1 = DB::connection('mongodb'); $c2 = DB::connection('mongodb'); $this->assertEquals(spl_object_hash($c1), spl_object_hash($c2)); $c1 = DB::connection('mongodb'); $c2 = DB::reconnect('mongodb'); $this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2)); }
/** * Execute the console command. * * @return mixed */ public function fire() { $connectionName = $this->argument('connection-name'); $databaseName = $this->argument('database-name'); \Config::set('database.connections.' . $connectionName . '.database', $databaseName); $connection = \DB::reconnect($connectionName); \DB::setDefaultConnection($connectionName); $this->info('Creating migration table in tenant database "' . $databaseName . '"...'); $this->call('migrate', ['--force' => $this->option('force')]); }
/** * Execute the console command. * * @return mixed */ public function fire() { $connectionName = $this->argument('connection-name'); $databaseName = $this->argument('database-name'); \Config::set('database.connections.' . $connectionName . '.database', $databaseName); $connection = \DB::reconnect($connectionName); \DB::setDefaultConnection($connectionName); $this->info('Seeding tenant database "' . $databaseName . '"...'); $this->call('db:seed'); }
public function saveFieldsMongo($linkKey, $dataForMongo) { try { $module = MongoModule::find($linkKey); } catch (Exception $e) { DB::reconnect('mongodb'); $module = MongoModule::find($linkKey); } //update mongo database $module->title = $dataForMongo['name']; $module->save(); }
/** * Execute the console command. * * @return void */ public function fire() { echo 'Iniciado' . PHP_EOL; $start = microtime(true); $cacheKey = md5(uniqid('')); $totalUsers = User::count(); $pid = pcntl_fork(); if ($pid == -1) { die('could not fork =('); } else { if ($pid) { // we are the parent //É necessário reconectar no banco, os processos vão compartilhar //a conexão, o que irá gerar erro \DB::reconnect('mysql'); //Pega os usuários da primeira metade do total $users = $this->getUsers($totalUsers / 2, 0); //Aguarda o processo filho terminar pcntl_wait($status); //Faz um merge do que o processo pai e filho processaram //Os dados do processo filho estão no cache $users = array_merge($users, \Cache::pull($cacheKey)); } else { // we are the child //É necessário reconectar no banco, os processos vão compartilhar //a conexão, o que irá gerar erro \DB::reconnect('mysql'); //Pega os usuários da segunda metade do total $users = $this->getUsers($totalUsers / 2, $totalUsers / 2); //Armazena os usuários processados no cache \Cache::forever($cacheKey, $users); die; } } echo 'Processados ' . count($users) . ' usuários' . PHP_EOL; echo 'Demorou ' . number_format((microtime(true) - $start) * 1000, 2, ',', '') . 'ms' . PHP_EOL; }
/** * Create a new job instance. * @param Image $image */ public function __construct(Image $image) { \DB::reconnect(); $this->image = $image; }
/** * Execute the console command. * * @return mixed */ public function fire() { // Certain stages of this installer will require variables // to be opulated, which will eventually be used to // write the resultant configuration file. Some // stages require testing too, so lets // prepare some defaults that will // be used. $configuration = array('mysql_hostname' => '127.0.0.1', 'mysql_database' => 'seat', 'mysql_username' => 'root', 'mysql_password' => '', 'redis_host' => '127.0.0.1', 'redis_port' => 6379, 'mail_driver' => 'mail', 'mail_from' => 'seatadmin@localhost', 'mail_from_name' => 'SeAT Administrator', 'smtp_hostname' => '127.0.0.1', 'smtp_username' => null, 'smtp_password' => null, 'smtp_port' => 25, 'smtp_encryption' => null); $this->info('[+] Welcome to the SeAT v' . \Config::get('seat.version') . ' installer!'); $this->line(''); // The very first thing we will be checking is the existence of // the .env.php file which contains the configuration of a // installed system. If this file exists, we will assume // that SeAT is already installed and exit. if (\File::exists(base_path() . '/.installed.lck')) { $this->error('[!] It appears as if SeAT is already installed. Exiting.'); return; } // Next, we will check that we will eventually be able to write // to the .env.php in base_path() /.env.php. if (!\File::isWritable(base_path() . '/.env.php')) { $this->error('[!] The installer needs to be able to write a configuration file to ' . base_path() . '/.env.php, but it appears as though it can not write there.'); return; } // Knowing that we can write the configuration file, we move on to // getting the details for the database. We will try with the // defaults, and if that fails, continue to ask for details // until we can connect $this->info('[+] Database setup...'); $this->info('[+] Please enter the details for the MySQL database to use (enter to use default):'); $error_count = 0; while (true) { // Ask for the MySQL credentials. $configuration['mysql_username'] = $this->ask('[?] Username (' . $configuration['mysql_username'] . '):') ?: $configuration['mysql_username']; $configuration['mysql_password'] = $this->secret('[?] Password:'******'mysql_password']; $configuration['mysql_hostname'] = $this->ask('[?] Hostname (' . $configuration['mysql_hostname'] . '):') ?: $configuration['mysql_hostname']; $configuration['mysql_database'] = $this->ask('[?] Database (' . $configuration['mysql_database'] . '):') ?: $configuration['mysql_database']; // Set the runtime configuration that we have \Config::set('database.connections.mysql.host', $configuration['mysql_hostname']); \Config::set('database.connections.mysql.database', $configuration['mysql_database']); \Config::set('database.connections.mysql.username', $configuration['mysql_username']); \Config::set('database.connections.mysql.password', $configuration['mysql_password']); // Test the database connection try { \DB::reconnect(); \DB::connection()->getDatabaseName(); $this->info('[+] Successfully connected to the MySQL database.'); $this->line(''); // If the connection worked, we don't have to ask for anything // and just move on to the next section break; } catch (\Exception $e) { $error_count++; // Check if we have had more than 3 errors now. if ($error_count >= 3) { $this->error('[!] 3 attempts to connect to the database failed.'); $this->error('[!] Please ensure that you have a MySQL server with a database ready for SeAT to use before installation.'); return; } $this->error('[!] Unable to connect to the database with mysql://' . $configuration['mysql_username'] . '@' . $configuration['mysql_hostname'] . '/' . $configuration['mysql_database']); $this->error('[!] Please re-enter the configuration to try again.'); $this->error('[!] MySQL said: ' . $e->getMessage()); $this->line(''); $this->info('[+] Please re-enter the MySQL details below:'); } } // Now that we have a working database connection, move // on to the Redis configuration. We will follow a // similar path of a infinite loop until it works $this->info('[+] Redis cache setup...'); $this->info('[+] Please enter the details for the Redis cache to use (enter to use default):'); $error_count = 0; while (true) { // Ask for the Redis details. $configuration['redis_host'] = $this->ask('[?] Host (' . $configuration['redis_host'] . '):') ?: $configuration['redis_host']; $configuration['redis_port'] = $this->ask('[?] Port (' . $configuration['redis_port'] . '):') ?: $configuration['redis_port']; // Set the \Config for this one runtime to test the connection \Config::set('database.redis.default.host', $configuration['redis_host']); \Config::set('database.redis.default.port', $configuration['redis_port']); // Test that we can add and remove keys from the cache try { \Cache::put('installer_test', true, 60 * 24); \Cache::forget('installer_test'); $this->info('[+] Successfully connected to the Redis cache.'); $this->line(''); // If the connection worked and we were able to place // and delete a key, move on to the next section break; } catch (\Exception $e) { $error_count++; // Check if we have had more than 3 errors now. if ($error_count >= 3) { $this->error('[!] 3 attempts to connect to redis failed.'); $this->error('[!] Please ensure that you have a Redis server ready for SeAT to use before installation.'); return; } $this->error('[!] Unable to connect to the redis cache at tcp://' . $configuration['redis_host'] . ':' . $configuration['redis_port']); $this->error('[!] Please re-enter the configuration to try again.'); $this->error('[!] Redis said: ' . $e->getMessage()); $this->line(''); $this->info('[+] Please re-enter the Redis details below:'); } } // We now have MySQL + Redis setup and ready. Lets move on the // the email configurations. If we have mail/sendmail as // the config, its easy. However, if we use SMTP, we // need to ask the user for credentials too, incase // those are needed. We will also start another // infinite loop to allow the user to confirm // that the details they entered is correct. $this->info('[+] Mail setup...'); $this->info('[+] Please enter the details for the email configuration to use (enter to use default):'); while (true) { // Ask for the email details $configuration['mail_driver'] = $this->ask('[?] How are emails going to be sent? [mail/sendmail/smtp] (' . $configuration['mail_driver'] . '):') ?: $configuration['mail_driver']; // Check the option we got. If it is not in the array of // known configuration, we return to the question if (!in_array($configuration['mail_driver'], array('mail', 'sendmail', 'smtp'))) { $this->error('[!] The driver you have chosen is not recognized, please try again.'); continue; } // Get the details about where emails will be coming from $configuration['mail_from'] = $this->ask('[?] Where will emails be coming from? (' . $configuration['mail_from'] . '):') ?: $configuration['mail_from']; $configuration['mail_from_name'] = $this->ask('[?] Who will emails be coming from? (' . $configuration['mail_from_name'] . '):') ?: $configuration['mail_from_name']; // If the configuration option is set as smtp, we need to // give the option to set the username and password if ($configuration['mail_driver'] == 'smtp') { $configuration['smtp_hostname'] = $this->ask('[?] SMTP Hostname (' . $configuration['smtp_hostname'] . '):') ?: $configuration['smtp_hostname']; $configuration['smtp_username'] = $this->ask('[?] SMTP Username (' . $configuration['smtp_username'] . '):') ?: $configuration['smtp_username']; $configuration['smtp_password'] = $this->secret('[?] SMTP Password:'******'smtp_password']; $configuration['smtp_port'] = $this->ask('[?] SMTP Port (' . $configuration['smtp_port'] . '):') ?: $configuration['smtp_port']; $configuration['smtp_encryption'] = $this->ask('[?] SMTP Encryption (' . $configuration['smtp_encryption'] . '):') ?: $configuration['smtp_encryption']; } // Print the values and get confirmation that they are correct $this->line(''); $this->line('[+] Mail configuration summary:'); $this->line('[+] Mail Driver: ' . $configuration['mail_driver']); // If we are going to be using the SMTP driver, show the // values for the host/user/pass if ($configuration['mail_driver'] == 'smtp') { $this->line('[+] SMTP Host: ' . $configuration['smtp_hostname']); $this->line('[+] SMTP Username: '******'smtp_username']); $this->line('[+] SMTP Password: '******'*', strlen($configuration['smtp_password']))); $this->line('[+] SMTP Port: ' . $configuration['smtp_port']); $this->line('[+] SMTP Encryption: ' . $configuration['smtp_encryption']); } $this->line(''); if ($this->confirm('[?] Are the above mail settings correct? [yes/no]', true)) { break; } else { continue; } } // With the configuration done, lets attempt to write this to // to disk if (!$this->writeConfig($configuration)) { $this->error('[!] Writing the configuration file failed!'); return; } $this->info('[+] Successfully wrote the configuration file'); // With configuration in place, lets move on to preparing SeAT // for use. We have to do a few things for which most // already have commands. So, lets re-use those // meaning that if they change the intaller // is already up to date. // Run the database migrations $this->info('[+] Running the database migrations...'); $this->call('migrate'); // Run the database seeds $this->info('[+] Running the database seeds...'); $this->call('db:seed'); // Update the SDEs $this->info('[+] Updating to the latest EVE SDE\'s...'); $this->call('seat:update-sde', array('--confirm' => null)); // Configure the admin user $this->info('[+] Configuring the \'admin\' user...'); $this->call('seat:reset'); // Sync the access groups $this->info('[+] Syncing the access groups...'); $this->call('seat:groupsync'); $this->line(''); // Regenerate the Application Encryption key $this->info('[+] Regenerating the Encryption Key'); $this->call('key:generate'); $this->line(''); // Finally, write the installer lock file! $lock_file_write = \File::put(base_path() . '/.installed.lck', 'Installed ' . date('Y-m-d H:i:s')); // Check that we wrote the lock file successfully if (!$lock_file_write) { $this->error('[!] Was not able to write the installation lock file! Please touch \'installed.lck\'.'); } $this->info('[+] Done!'); }
/** * reconnect to the database * * @depends testConnectToTheDatabase */ public function testReconnectToTheDatabase() { DB::reconnect(); }
public function putChatroomFriends($wxuin, $post) { \DB::reconnect(); //确保获取了一个新的连接。 //循环执行心跳 $user = Login::where('wxuin', $wxuin)->where('status', 1)->first(); if (!$user) { $this->death(); } $cookies = json_decode($user->cookies); $url = 'https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=' . t(); $ret = CURL::send($url, ['Cookie' => urldecode(http_build_query($cookies, '', '; '))], [], ['ret' => 'all', 'post' => json_encode($post)]); $html = $ret->body; $cookies2 = toCookies($ret->cookies); $cookies = (object) ((array) $cookies2 + (array) $cookies); $html = iconv('UTF-8', 'UTF-8//IGNORE', $html); $data_arr = json_decode($html, true); foreach ($data_arr['ContactList'] as $k => $v) { //获取好友信息 $data_room = ['EncryChatRoomId' => $v['EncryChatRoomId'], 'Uin' => $v['Uin'], 'UserName' => $v['UserName'], 'NickName' => $v['NickName'], 'HeadImgUrl' => $v['HeadImgUrl'], 'RemarkName' => $v['RemarkName'], 'Sex' => $v['Sex'], 'Signature' => $v['Signature'], 'AttrStatus' => $v['AttrStatus'], 'Province' => $v['Province'], 'City' => $v['City']]; $rm = Chatroom::where('EncryChatRoomId', $v['EncryChatRoomId'])->where('UserName', $v['UserName'])->first(); //如果存在了就更新 if ($rm) { Chatroom::where('EncryChatRoomId', $v['EncryChatRoomId'])->where('UserName', $v['UserName'])->update($data_room); } else { $data_room_batch[] = $data_room; } } if (isset($data_room_batch)) { Chatroom::insert($data_room_batch); } }