2017年8月20日日曜日

Cakephp : ShellのCron実行

パラメタを自動取得するため、Shellを作成しCronで実行しました。
参考
http://mizunolab.sist.ac.jp/2016/01/cakephp-shell.html
http://mizunolab.sist.ac.jp/2016/01/cakephpcron.html

1.Shellの作成
app]$ vi Console/Command/MapShell.php
<?php
class MapShell extends Shell {
function main () {
App::import('Model','Firedistrict');
$this->Firedistrict = new Firedistrict;
$fires = $this->Firedistrict->find('all',
array(
'conditions' => array('distance' => null),
'limit' => 3,
)
);
foreach($fires as $fire){
$distance = $this->getDirection($fire['Firedistrict']['firelatitude'],$fire['Firedistrict']['firelongitude'],$fire['Firedistrict']['distlatitude'],$fire['Firedistrict']['distlongitude']);
$data = array(
'id' => $fire['Firedistrict']['id'],
'distance' => $distance[0],
'duration' => $distance[1]
);
$this->Firedistrict->save($data);
}
}
public function getDirection($orglat, $orglon, $dstlat, $dstlon){
$datas = file_get_contents("http://maps.googleapis.com/maps/api/directions/json?origin=$orglat,$orglon&destination=$dstlat,$dstlon&sensor=false");
$directions = json_decode($datas);
//print_r ($directions);
$direction = $directions->routes[0]->legs[0];
$distance = $direction->distance->value;
$duration = $direction->duration->value;
$result = array($distance, $duration);
return $result;
}
}
?>
view raw MapShell.php hosted with ❤ by GitHub


2.スクリプトファイルの作成
app]$ vi Vendor/CakeShell.sh
#!/bin/bash
cd ~
cd www/app
Console/cake map
view raw CakeShell.sh hosted with ❤ by GitHub


実行権限を与えます。
app]$ chmod +x Vendor/CakeShell.sh

3.cron設定
$ crontab -e
*/2 * * * * /home/mznqueue/www/app/Vendor/CakeShell.sh
2分に1回実行

0 件のコメント:

コメントを投稿