You are on page 1of 8

TI LIU HNG DN S DNG PEAR V CRON JOB MAGENTO 1.

7 Ti liu k thut 07/2012

Lch s chnh sa
Ngy thng 07/2012 Version 1.7 M t Ngi vit DVMS

Mc lc
1. PEAR: .......................................................................................................................... 3 2. Cron job: ..................................................................................................................... 4 THNG TIN LIN H H TR......................................... Error! Bookmark not defined.

Trang 2 /8

1. PEAR: Pear trong php: PEAR l mt trong nhng t ng c nhc n kh ph bin trong cng ng PHP. mt kha cnh nht nh, vic s dng PEAR s gip cho cc lp trnh vin gii quyt nhanh cng vic vi cht lng sinh m tt nh vo s phong ph v chun ca th vin PEAR. Tham kho thm v th vin pear ti link sau: http://www.pcworld.com.vn/pcworld/printArticle.asp?atcl_id=5f5e5c575f5f58 http://pear.php.net/ Pear trong Magento: dng th vin Pear ci t thm module cho Magento, dng chc nng ny bn phi c phn mm connect vo hosting hay server ca bn bng ssh. Ci cc ng dng Magento bng ssh n gin, v hiu qu cao hn so vi Magento connect, nht l khi ng truyn server ca bn b hn ch (c bit vi cc server ca FPT) v ssh cho php bn kt ni lu di, thi gian c th download package ci t v server ca bn. Ngoi ra, khi kt ni ssh, bn s dng ti khon trn server (ti khon ngi dng Linux) nn vic ghi cc file, gii nn v ci t thng khng b li. Cn vi trnh Magento connect nhiu khi cc kt ni khng thc hin c, do vic qun l cc file, th mc ca Magento khng quyn ghi, gii nn file. Bn cn ci t pear trc khi thc hin cc thao tc khc, vi nhng bn dng shared hosting cc nh cung cp dch v th kim tra PHP c ci t Pear ri hay cha ? nu cha th lin h vi sysadmin nh cung cp dch v h gii quyt vn ny gip bn . Sau khi to c kt ni ssh vi server, bn dng cc lnh ls -a ging nh lnh dir ca dos list cc th mc v tp tin trong th mc hin hnh cd / tr v th mc cao nht cd tn th mc vo th mc Ty theo hosting ca bn, v th mc ni bn ci t Magento, bn dng cc lnh trn n th mc gc ca Magento. Dng lnh: ./pear install EXTENSION_KEY thc hin vic ci t. y trong v d ny, EXTENSION_KEY l key ca module m bn mun ci t, v d: Magento-core/Interface_Frontend_Default_Default_Blue Tip theo l ch i server kt ni vi server Magento, download b ci v ci t. Nu ci t thnh cng, my s bo Installing OK v tr v dng g lnh G tip lnh rm -rf downloader/pearlib/cache/* downloader/pearlib/download/* xa cc file cache to ra trong qu trnh ci t Mt s li xy ra trong qu trnh ci t 1.Error: cannot download "Magento-community/BouncingOrange_TinyMce"

Trang 3 /8

Install Errors Download failed PEAR ERROR: install failed li ny ngha l server khng th download b ci t ng dng. Thng do ng truyn ca server bn qu yu. Hy th ci bng ssh 2. .Error: cannot write folder Li ny ngha l cc th mc trong phn downloader ca bn ang ch chng ghi.Th mc quan trng nht l downloader/pearlib/download/ 3. Error ... status="beta" Li ny l do extension bn mun ci t cn ang trng thi beta (th nghim). tip tc install bn vo phn qun tr Magento connect, vo setting nh trong hnh 2, chn Preferred State: l Beta, hoc Anpha. 2. Cron job: Mt vi tnh nng trong Magento i hi mt kch bn chy theo nh k. V d: app/code/core/Mage/CatalogRule/etc/config.xml <config> ... <crontab> <jobs> <catalogrule_apply_all> <schedule><cron_expr>0 1 * * *</cron_expr></schedule> <run><model>catalogrule/observer::dailyCatalogUpdate</model></run> </catalogrule_apply_all> </jobs> ... </crontab> ...

Trang 4 /8

</config> y tc v Mage_CatalogRule_Model_Observer::dailyCatalogUpdate s c thc hin hng ngy vo lc 01:00am Bn c th to ra cc tc v cho module ca bn trong file cu hnh ca module theo cu trc sau: <config> ... <crontab> <jobs> <namespace_module> <schedule> <cron_expr>0,15,30,45 * * * *</cron_expr> </schedule> <run> <model>module/model::method</model> </run> </namespace_module> </jobs> </crontab> ... </config> Tc v ny c 15 pht li thc thi phng thc ca module mt ln. thc hin tt c cc tc v m bn cu hnh th file cron.php nm trong th mc gc Magento s cn phi c chy nh k, v d mi 15 pht. V c bn n s kim tra cc tc v cn thc hin trong tng lai. Trong mi trng linux th bn cn cu hnh cho server ca bn thc thi file cron.sh nh k. Mt s cron job mc nh ca h thng c cu hnh thi gian thc thi trong: System > Configuration > System.

Trang 5 /8

Nhng tc v bao gm:

Trang 6 /8

Link tham kho:


http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job http://www.novusweb.com/894/e-commerce-technology/understanding-magento-cron-jobs#.UA3_UaD_msp http://fishpig.co.uk/blog/magento-cron-jobs.html

Ngoi ra Cron Schedule Monitior bn c th to file php c ni dung nh sau:


<?php //Magento Cron script. GNU/GPL //oliver.higgins@gmail.com //provided without warranty or support //================================================================ //insert your database info here $server='your.server.address:port'; $user='yourusername'; $pass='yourpassword'; $db='yourMySQLdatabasenane'; //end data input //================================================================ echo "<h1>Magento Cron Schedule</h1><h2>for ".$user."@".$server."</h2>"; //================================================================ //pending jobs mysql_connect($server,$user,$pass); @mysql_select_db($db) or die("Unable to select database"); //$query="SELECT * FROM cron_schedule" ; $query='SELECT * FROM `cron_schedule` WHERE `status` ="pending" ORDER BY `scheduled_at` DESC' ; $result=mysql_query($query); $num=mysql_numrows($result); echo "<h2>".$num." Jobs Pending</h2>"; echo '<table border="1"><tbody>'; echo "<tr><th>schedule_id</th><th>job_code</th><th>status</th><th>created_at</th><th>scheduled_at</th>"; //echo "<th>executed_at</th><th>finished_at</th></tr>"; $i=0; while ($i < $num) { $schedule_id=mysql_result ($result,$i,"schedule_id"); $job_code= mysql_result($result,$i,"job_code"); $status=mysql_result ($result,$i,"status"); $created_at=mysql_result ($result,$i,"created_at"); $scheduled_at=mysql_result ($result,$i,"scheduled_at"); $executed_at=mysql_result ($result,$i,"executed_at"); $finished_at=mysql_result ($result,$i,"finished_at"); //output html echo "<tr>"; echo "<td>".$schedule_id."</td>"; echo '<td>'.$job_code."</td>"; echo '<td style="color: red;">'.$status."</td>"; echo "<td>".$created_at."</td>"; echo "<td>".$scheduled_at."</td>"; //echo "<td>".$executed_at."</td>"; //echo "<td>".$finished_at."</td>"; echo "</tr>"; $i++; }

Trang 7 /8

echo "</tbody></table><hr>"; //================================================================ //Succsessful jobs mysql_connect($server,$user,$pass); @mysql_select_db($db) or die("Unable to select database"); //$query="SELECT * FROM cron_schedule" ; $query='SELECT * FROM `cron_schedule` WHERE `status` ="success" ORDER BY `executed_at` DESC' ; $result=mysql_query($query); $num=mysql_numrows($result); echo "<h2>".$num." Jobs Succsessful</h2>"; echo '<table border="1"><tbody>'; echo "<tr><th>schedule_id</th><th>job_code</th><th>status</th><th>created_at</th><th>scheduled_at</th>"; echo "<th>executed_at</th><th>finished_at</th></tr>"; $i=0; while ($i < $num) { $schedule_id=mysql_result ($result,$i,"schedule_id"); $job_code= mysql_result($result,$i,"job_code"); $status=mysql_result ($result,$i,"status"); $created_at=mysql_result ($result,$i,"created_at"); $scheduled_at=mysql_result ($result,$i,"scheduled_at"); $executed_at=mysql_result ($result,$i,"executed_at"); $finished_at=mysql_result ($result,$i,"finished_at"); //output html echo "<tr>"; echo "<td>".$schedule_id."</td>"; echo "<td>".$job_code."</td>"; echo "<td>".$status."</td>"; echo "<td>".$created_at."</td>"; echo "<td>".$scheduled_at."</td>"; echo "<td>".$executed_at."</td>"; echo "<td>".$finished_at."</td>"; echo "</tr>"; $i++; } echo "</tbody></table>"; //================================================================ ?>

CHC THNH CNG!

THNG TIN LIN H H TR

DVMS (08) 360 289 37 info@dvms.vn www.DVMS.vn

Trang 8 /8