ã¬ã³ã¿ã«ãµã¼ãã¼ XREA/CORESERVER 㧠FuelPHP ã使ã [å®è·µç·¨]
ãã®è¨äºã¯ FuelPHP Advent Calendar 2013 ã® 17 æ¥ç®ã®è¨äºã¨ãã¦å ¬éãã¾ãããªããæ¨æ¥ã¯ hosopy ããã®ãFuelPHPã®moduleã使ãããªããã§ããã
å ã æã«ãã¬ã³ã¿ã«ãµã¼ãã¼ XREA/CORESERVER 㧠FuelPHP ãåãããã¨ããè¨äºãæ¸ãã¾ãããä»åã¯ãã®ç¶ãã¨ãã¦ãããå°ãå®è·µçãªå 容ããå±ããã¾ããååã®è¨äºã¨ä½µãã¦ãèªã¿ãã ããã
æ¬ç¨¿ã®å 容㯠s110.coreserver.jp ä¸ã® PHP 5.4.21 㧠FuelPHP 1.7.1 ã使ã£ã¦åä½ç¢ºèªããã¦ããã¾ãã以éãã¦ã¼ã¶ã¼åã hogefugaããµã¼ãã¼ã s1024.coreserver.jp ã¨ä»®å®ãã¦èª¬æãã¾ããã¿ãªããã®ã¢ã«ã¦ã³ãæ å ±ã«é©å®èªã¿æããªãã試ãã¦ã¿ã¦ãã ããã
ãã¼ã«ã«ã®éçºç°å¢ã§ãã¼ã¿ãã¼ã¹ã使ã
XREA/CORESERVER ã§ã¯ MySQL 㨠PostgreSQL ãå©ç¨å¯è½ã§ãããã»ãã¥ãªãã£ã¼ã®çç±ããããåä¸ãµã¼ãã¼ããã®ã¢ã¯ã»ã¹ä»¥å¤ã¯å¼¾ãããããã§ããã¼ã«ã«ã®éçºç°å¢ãããµã¼ãã¼ä¸ã®ãã¼ã¿ãã¼ã¹ãèªã¿æ¸ããããã¨ã¯ã§ãã¾ãããããã§éçºç¨ã®ãã¼ã¿ãã¼ã¹ããã¼ã«ã«ã«ç«ã¦ããã¼ã«ã«ç°å¢ã§ã¯ãã¼ã«ã«ãã¼ã¿ãã¼ã¹ãããµã¼ãã¼ä¸ã®éç¨ç°å¢ã§ã¯ãµã¼ãã¼ã®ãã¼ã¿ãã¼ã¹ãåç §ããããã«ãã¾ãããã
ã¾ãããµã¼ãã¼ä¸ã®ç°å¢ã production (éç¨ç°å¢)ã«è¨å®ãã¾ããããFuelProject/public/.htaccess ã®å é ããAddHandler application/x-httpd-php54cgi .phpãã®ä¸ãããã«ã次ã®è¡ã追å ãã¾ãã
SetEnv FUEL_ENV production
.htaccess ã¯ãã¼ã«ã«ã¨ãµã¼ãã¼ã§ã¯è¨è¿°ãç°ãªãã®ã§ãä¸ç¨æãªä¸æ¸ãã«æ³¨æãã¦ãã ããã
ããã§ãã¼ã«ã«ç°å¢ã¯ developmentããµã¼ãã¼ç°å¢ã¯ production ã¨å¥ã ã® FUEL_ENV ãè¨å®ããã¾ãããã§ã¯ãããããã®ç°å¢ç¨ã« db.php ãä½ãã¾ãã
ç§ã¯ PostgreSQL ã好ããªã®ã§ãåºæ¬çã«ãµã¼ãã¼ã«ã PostgreSQL ã§ãã¼ã¿ãã¼ã¹ãä½æãã¦ãã¾ããããã§ã¯ PostgreSQL ã®ä¾ãæãã¾ãããMySQL ã§ãåæ§ã«è¨å®ã§ãã¾ãã®ã§ãåèã«ãã¦ãã ããã
ã¾ããfuel/app/config/db.php ã«å ±éè¨å®ãæ¸ãã¾ãã
<?phpreturn [
'default' => [
'type' => 'pdo',
'identifier' => '',
'table_prefix' => '',
'charset' => 'utf8',
'enable_cache' => true,
'profiling' => false,
],
];
ç¶ãã¦ãµã¼ãã¼å´ã®è¨å®ã fuel/app/config/production/db.php ã«è¨è¿°ãã¾ãããããã¹ãå㯠s1024.coreserver.jp ã¨ãããlocalhost ã«ãã¾ãã
<?phpreturn [
'default' => [
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=hogefuga',
'username' => 'hogefuga',
'password' => 'your_password',
'persistent' => false,
'compress' => false,
],
],
];
åæ§ã«ããã¼ã«ã«ç°å¢ã®è¨å®ã fuel/app/config/development/db.php ã«è¨è¿°ãã¾ãã
<?phpreturn [
'default' => [
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=hogefuga_local',
'username' => 'hogefuga_local',
'password' => 'your_local_password',
'persistent' => false,
'compress' => false,
],
],
];
ãã¼ã«ã«ã«ç«ã¦ããã¼ã¿ãã¼ã¹ã® DB åã¨ã¦ã¼ã¶ã¼åããã¹ã¯ã¼ãããµã¼ãã¼ã®ãã®ã¨åãã«ããã°è¨å®ãåããã«æ¸ã¿ã¾ããããã¼ã«ã«ç°å¢ã§è¤æ°ã®ãã¼ã¿ãã¼ã¹ãåãæ¿ãã¦éçºã§ããããã«ãã¦ããã»ããããã§ãããã
ã¿ã¹ã¯ã¨ Email ããã±ã¼ã¸ãå©ç¨ãã¦ãã°ãã¡ã¤ã«ã®è¥å¤§åãé²ã
fuel/app/logs ã«æ¥ã èç©ããããã°ãã¡ã¤ã«ããFuelPHP ã使ã£ã¦å®æçã«æé¤ããã¾ãããã
次ã®ãããªä»æ§ãèãã¾ãã
- æ¥ä»ãå¤ãã£ãããåæ¥ã®ãã°ãã¡ã¤ã«ãã¡ã¼ã«ã§éä¿¡ããã
- 1 é±éåã®ãã°ãã¡ã¤ã«ã¯åé¤ããããã®éãã«ã©ã«ãªã£ããã£ã¬ã¯ããªãåé¤ããã
cron ã¸ã§ãã«ãããããå¦çãæ¯æ©èµ°ããã¾ããããFuelPHP ã§ã¯ã¿ã¹ã¯ã¨ããæ©è½ã§ãããå¦çãå®ç¾ã§ãã¾ãã
é©å½ãªããã¸ã§ã¯ããä½æããfuel/app/tasks/logsender.php ã¨ãããã¡ã¤ã«ãä½æãã¾ããããã¸ã§ã¯ãã®è¨ç½®å ´æ㯠~/php/logsender
ã¨ãã¾ãã
<?php/**
* FuelPHP ã®åæ¥åãã°ãã¡ã¤ã«ãã¡ã¼ã«ã§éä¿¡ãã1 é±éåã®ãã°ãã¡ã¤ã«ãåé¤ããã¿ã¹ã¯ã
*/namespace Fuel\Tasks;
\Package::load('email');
/**
* ãã°ã®ã¡ã¼ã«éä¿¡ã¨å¤ããã°ã®åé¤ãè¡ãã¯ã©ã¹ã
*/
class LogSender
{
/**
* ãããæ¬ä½
*/
public static function run()
{
// ã¡ã¼ã«ãããã¼æ å ±
$address_from = '[email protected]';
$name_from = 'FuelPHP Task';
$address_to = 'hogefuga@s1024.coreserver.jp';
$name_to = '俺';$yesterday = date('Y/m/d', time() - 86400);
$one_week_ago = date('Y/m/d', time() - 86400 * 7);$log_dir = '/virtual/hogefuga/php/logsender/fuel/app/logs/';
$yesterday_log_name = $log_dir . $yesterday . '.php';// ã¡ã¼ã«éä¿¡
$subject = "FuelPHP Log - $yesterday.php";$email = \Email::forge();
$email->from($address_from, $name_from);
$email->to($address_to, $name_to);
$email->subject($subject);
if (file_exists($yesterday_log_name)) {
$email->body('');
$email->attach($yesterday_log_name);
} else {
$email->body('æ¨æ¥ã®ãã°ãã¡ã¤ã«ã¯ããã¾ããã');
}
try {
$email->send();
} catch (Exception $ex) {}
// 1 é±éåã®ãã°ãåé¤
$log_to_delete = $log_dir . $one_week_ago . '.php';
if (file_exists($log_to_delete)) {
if (!unlink($log_to_delete)) {
echo 'ãã¡ã¤ã«åé¤ã«å¤±æ' . PHP_EOL;
}
self::remove_directory(dirname($log_to_delete));
self::remove_directory(dirname(dirname($log_to_delete)));
}
}
/**
* ãã£ã¬ã¯ããªãåé¤ããããã£ã¬ã¯ããªå ã«ãã¡ã¤ã«ãããæã¯ä½ãããªãã
* @param string $dir ãã£ã¬ã¯ããªå
*/
private static function remove_directory($dir)
{
$files = scandir($dir);
if (count($files) === 2) {
rmdir($dir);
}
}
}
ãã®ã¿ã¹ã¯ãã³ãã³ãã©ã¤ã³ããå®è¡ããéã以ä¸ã®ã³ãã³ãã§ã¯ã¨ã©ã¼ãè¿ã£ã¦ãã¾ãã
php ~/php/logsender/oil r logsender
æ¬ç¨¿å·çæç¹ã§ã¯ XREA (ä¸é¨ã®ãµã¼ãã¼ãé¤ã)㨠CORESERVER 㯠PHP ã®ãã¼ã¸ã§ã³ã 5.2 ãªã®ã§ãFuelPHP ã¯åããªãã®ã§ãããPHP ããã«ãã¹ã§æå®ãã¾ãããã
ããã§ãµã¼ãã¼ã«ãã°ã¤ã³ã*1ããã¼ã ãã£ã¬ã¯ããªã§ ls /usr/local/bin/php*
ãå®è¡ãã¦ã¿ã¦ãã ããã以ä¸ã®ãããªè¡¨ç¤ºã«ãªãã¨æãã¾ãã
hogefuga@s1024:~> ls /usr/local/bin/php*
/usr/local/bin/php /usr/local/bin/php-5.2.4cli /usr/local/bin/php-5.4.19cli /usr/local/bin/php55-config
/usr/local/bin/php4 /usr/local/bin/php-5.2.5 /usr/local/bin/php-5.4.21 /usr/local/bin/php55ize
/usr/local/bin/php-4.4.7 /usr/local/bin/php-5.2.5cli /usr/local/bin/php-5.4.21cli /usr/local/bin/php5cli
/usr/local/bin/php-4.4.7cli /usr/local/bin/php52cli /usr/local/bin/php-5.4.5 /usr/local/bin/php5-config
/usr/local/bin/php-4.4.8 /usr/local/bin/php53 /usr/local/bin/php-5.4.5cli /usr/local/bin/php5ize
/usr/local/bin/php-4.4.8cli /usr/local/bin/php-5.3.15 /usr/local/bin/php-5.4.7 /usr/local/bin/php6
/usr/local/bin/php4cli /usr/local/bin/php-5.3.15cli /usr/local/bin/php-5.4.7cli /usr/local/bin/php6cli
/usr/local/bin/php4-config /usr/local/bin/php-5.3.17 /usr/local/bin/php54cli /usr/local/bin/php6-config
/usr/local/bin/php4ize /usr/local/bin/php-5.3.17cli /usr/local/bin/php54-config /usr/local/bin/php6ize
/usr/local/bin/php5 /usr/local/bin/php-5.3.27 /usr/local/bin/php54ize /usr/local/bin/php_back
/usr/local/bin/php52 /usr/local/bin/php-5.3.27cli /usr/local/bin/php55 /usr/local/bin/php-cgi
/usr/local/bin/php-5.2.2 /usr/local/bin/php53cli /usr/local/bin/php-5.5.3 /usr/local/bin/php-config
/usr/local/bin/php-5.2.2cli /usr/local/bin/php53-config /usr/local/bin/php-5.5.3cli /usr/local/bin/phpize
/usr/local/bin/php-5.2.3 /usr/local/bin/php53ize /usr/local/bin/php-5.5.5
/usr/local/bin/php-5.2.3cli /usr/local/bin/php54 /usr/local/bin/php-5.5.5cli
/usr/local/bin/php-5.2.4 /usr/local/bin/php-5.4.19 /usr/local/bin/php55cli
hogefuga@s1024:~>
FuelPHP ã®å®è¡ã«ã¯ PHP 5.3 以ä¸ã§ããã°ããã®ã§ã好ããªãã¼ã¸ã§ã³ã® PHP ãæå®ãã¾ãããã
/usr/local/bin/php-5.5.5cli ~/php/logsender/oil r logsender
ãããå®è¡ããã·ã§ã«ã¹ã¯ãªãããé©å½ãªå ´æã«è¨ç½®ãã¾ããããã§ã¯ ~/cron_logsender.sh ã¨ãã¦ç½®ããã¨ã«ãã¾ããæ¹è¡ã³ã¼ãã LF ã«ããã®ãå¿ããªãããã«ãã¦ãã ããã
#!bin/sh
/usr/local/bin/php-5.5.5cli ~/php/logsender/oil r logsender
exit
ããã cron ã¸ã§ãã§èµ·åããããã«è¨å®ãã¾ããCORESERVER ã®ã³ã³ããã¼ã«ããã«ã«ãã°ã¤ã³ãã¦ãå·¦ã¡ãã¥ã¼ã®ãCRON ã¸ã§ãããã¯ãªãã¯ãããCRON ã¸ã§ãã®ç·¨éãç»é¢ãéãã¾ãã
空ãã¦ããè¨å®æ¬ã«ã以ä¸ã®ããã«å ¥åãã¾ãã
- å â 30
- æ â 0
- æ¥ â *
- æ â *
- ææ¥ â *
ãã®ä¸ã«ããã/virtual/hogefuga/ãã«ç¶ãããã¹ãããã¯ã¹ã«ã¯ã
cron_logsender.sh > /dev/null 2>&1
ã¨å ¥åãã¾ããããã§ãæ¯æ¥ 0:30 ã«å ã»ã©ã®ã¿ã¹ã¯ãå®è¡ããã¾ãã
ã¡ã¼ã«éä¿¡æã«ã¨ã©ã¼ãçºçããã¨ãã®å¦çãªã©ã¯ãã¿ãªããã§è¿½å ãã¦ã¿ã¦ãã ããã
ã12/18 追è¨ã
æ·»ä»ãã¡ã¤ã«ã®ãµã¤ãºã®å¶éã«ã¤ãã¦ãFumito Mizuno ãããã質åãããã ãã¾ãããå ¬å¼çºè¡¨ã¯ãªããããªã®ã§ãå®éã«ã©ã®ãããã¾ã§æ·»ä»ã§éããããç°¡åãªã³ã¼ããæ¸ãã¦èª¿ã¹ã¦ã¿ã¾ããã
1MB ãã 1MB å»ã¿ã§æ·»ä»ãã¡ã¤ã«ã®ãµã¤ãºãå¢ããã¦éä¿¡ããã¨ããããããªã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºã¾ããã
Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 28329953 bytes) in /virtual/hogefuga/php/logsender/fuel/packages/email/classes/email/driver.php on line 965
Fatal Error - Allowed memory size of 94371840 bytes exhausted (tried to allocate 28329953 bytes) in PKGPATH/email/classes/email/driver.php on line 965
19MB ã®æ·»ä»ãã¡ã¤ã«ãã¤ããã¡ã¼ã«ã¾ã§ã¯å±ãã¾ãããã20MB ã®æ·»ä»ãã¡ã¤ã«ã¤ãã®ã¡ã¼ã«ã¯å±ãã¾ããã§ããããµã¼ãã¼ã«ãã£ã¦å·®ã¯ããã¨æãã¾ããããã°ã 20MB 以ä¸ã«ãªãã¨å³ããããããã¾ããã
æ¬å½ã¯ãã°ãã¡ã¤ã«ã ZIP å§ç¸®ãã¦ããæ·»ä»ãããã®ã§ãããXREA/CORESERVER ã§ã¯ ZipArchive ã¯ã©ã¹ã使ããªãã®ã§ãéå§ç¸®ã®ã¾ã¾æ·»ä»ãã¦ãã¾ãã
çµããã«
ãã®è¨äºãæåã«æ¸ããã¨ããã¡ã¼ã«éä¿¡é¨åã¯ç´ ã® PHP ã§ããã°ã®å 容ãã¡ã¼ã«æ¬æã¨ã㦠mb_send_mail() ã§éä¿¡ããã³ã¼ãã§ãããæ¸ãçµããå¾ããFuelPHP ã®ã¢ããã³ãã«ã¬ã³ãã¼ãªã®ã« FuelPHP æåãå°ãªãããã¨æãç´ãã使ã£ããã¨ããªã Email ããã±ã¼ã¸ã§éä¿¡ããã³ã¼ããæ¸ãå§ããã¨ããããã®ã® 5 åç¨åº¦ã§æ·»ä»ãã¡ã¤ã«ã¤ãã®ã¡ã¼ã«ãéä¿¡ããã³ã¼ããåãã¦ãã¾ããã¾ãã¾ã FuelPHP ãæ°ã«å ¥ã£ã¦ãã¾ãã¾ããã
次å㯠@mamor ããã®ãFuelPHPã¨MongoDBã¨TraceKitã§JavaScriptã®ã¨ã©ã¼æ å ±ãåéãã¦ã¿ããã§ãã
*1:ãã¬ã³ã¿ã«ãµã¼ãã¼ XREA/CORESERVER 㧠FuelPHP ãåãããã®ã3. ã·ã³ããªãã¯ãªã³ã¯ã®ä½æããåç §ãã¦ãã ããã