135 lines
3.5 KiB
PHP
135 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: linfan
|
|
* Date: 19/01/17
|
|
* Time: 10:09
|
|
*/
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Plan extends CI_Controller
|
|
{
|
|
|
|
private $plan;
|
|
|
|
protected $log_dir;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->log_dir = "plan";
|
|
|
|
$plan = array();
|
|
|
|
//$plan[] = array('url' => base_url(array('plan', 'controller', 'action')), 'interval' => 30);
|
|
|
|
//执行失败的plan重跑
|
|
$plan[] = array('url' => base_url(array('plan', 'plan', 'replan')), 'interval' => 1);
|
|
|
|
$this->plan = $plan;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$plan = $this->plan;
|
|
|
|
if ($plan) {
|
|
$redis = &load_cache('redis');
|
|
|
|
echo "------ mission start " . date("Y-m-d H:i:s") . " ------\n";
|
|
|
|
foreach ($plan as $v) {
|
|
$key = 'hd_plan_job_' . md5($v['url']);
|
|
|
|
if (!$redis->get($key)) {
|
|
$this->benchmark->mark('code_start');
|
|
$res = $this->get_https($v['url']);
|
|
$res && $redis->save($key, 1, $v['interval'] * 60);
|
|
$this->benchmark->mark('code_end');
|
|
|
|
echo $v['url'] . " --- mission" . ($res ? " succ" : " fail") . " spend:" . $this->benchmark->elapsed_time('code_start', 'code_end') . "\n";
|
|
}
|
|
}
|
|
|
|
echo "Finished " . date("Y-m-d H:i:s") . "\n";
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 重置计划脚本
|
|
*/
|
|
public function reset_plan()
|
|
{
|
|
$id = $this->input->get('id');
|
|
|
|
$v = $this->plan[$id];
|
|
|
|
echo json_encode($v, JSON_UNESCAPED_UNICODE) . "\n";
|
|
|
|
if ($v) {
|
|
$key = 'hd_plan_job_' . md5($v['url']);
|
|
|
|
$redis = &load_cache('redis');
|
|
|
|
if ($redis->get($key)) {
|
|
$ret = $redis->delete($key);
|
|
echo "redis del: {$ret}\n";
|
|
} else {
|
|
echo "redis not save\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
//执行异常的脚本重新跑
|
|
public function replan(){
|
|
$redis = &load_cache('redis');
|
|
$redis = $redis->redis();
|
|
|
|
$key_batch = "return_plan_batch";
|
|
|
|
$now = time();
|
|
$rows = $redis->zRangeByScore($key_batch, 0, $now);
|
|
debug_log("[info]# return_plan_batch:" . json_encode($rows), __FUNCTION__, $this->log_dir);
|
|
echo "return_plan_batch:" . json_encode($rows) ." \n";
|
|
|
|
$total = count($rows);
|
|
$done = 0;
|
|
foreach($rows as $k => $v){
|
|
$url = $v;
|
|
|
|
$res = $this->get_https($url);
|
|
debug_log("[info]# url={$url}; res={$res}", __FUNCTION__, $this->log_dir);
|
|
if($res){
|
|
$done++;
|
|
}
|
|
$redis->zRem($key_batch, $v);
|
|
}
|
|
|
|
$msg = "all:{$total}; done:{$done}";
|
|
debug_log("[info]# {$msg}", __FUNCTION__, $this->log_dir);
|
|
echo $msg;
|
|
}
|
|
|
|
private function get_https($url)
|
|
{
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_HEADER, 1);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //这个是重点,规避ssl的证书检查。
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 跳过host验证
|
|
|
|
curl_exec($curl);
|
|
|
|
if (curl_errno($curl)) {
|
|
curl_close($curl);
|
|
return false;
|
|
}
|
|
|
|
curl_close($curl);
|
|
return true;
|
|
}
|
|
|
|
}
|