Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
470054086 committed Oct 19, 2016
1 parent 2fd0af6 commit ffb4540
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 269 deletions.
411 changes: 158 additions & 253 deletions .idea/workspace.xml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions app/Crontab/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/10/18
* Time: 17:54
*/
34 changes: 34 additions & 0 deletions app/config/crontab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/10/18
* Time: 17:50
*/
return [
[
'timeout'=>[
'taskID'=>4,
'name'=>'addPush',//线上的名称
'desc'=>'每秒钟往redis中写入输入',//计划任务的描述
'time'=>'16 * * * *',//根据crontab的计划任务
'url'=>'Index/index',//执行的方法路径
'argv'=>[
'name'=>'xiaobai',
'age'=>10,
]
],
'intval'=>[
'taskID'=>4,
'name'=>'addPush',//线上的名称
'desc'=>'每秒钟往redis中写入输入',//计划任务的描述
'time'=>'16 * * * *',//根据crontab的计划任务
'url'=>'Index/index',//执行的方法路径
'argv'=>[
'name'=>'xiaobai',
'age'=>10,
]
]

]
];
87 changes: 87 additions & 0 deletions core/Lib/Crontab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/10/19
* Time: 10:54
*/
namespace core\Lib;
/* https://github.com/jkonieczny/PHP-Crontab */
class Crontab {
/**
* Finds next execution time(stamp) parsin crontab syntax,
* after given starting timestamp (or current time if ommited)
*
* @param string $_cron_string:
*
* 0 1 2 3 4
* * * * * *
* - - - - -
* | | | | |
* | | | | +----- day of week (0 - 6) (Sunday=0)
* | | | +------- month (1 - 12)
* | | +--------- day of month (1 - 31)
* | +----------- hour (0 - 23)
* +------------- min (0 - 59)
* @param int $_after_timestamp timestamp [default=current timestamp]
* @return int unix timestamp - next execution time will be greater
* than given timestamp (defaults to the current timestamp)
* @throws InvalidArgumentException
*/
public static function parse($_cron_string,$_after_timestamp=null)
{
if(!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i',trim($_cron_string))){
throw new InvalidArgumentException("Invalid cron string: ".$_cron_string);
}
if($_after_timestamp && !is_numeric($_after_timestamp)){
throw new InvalidArgumentException("\$_after_timestamp must be a valid unix timestamp ($_after_timestamp given)");
}
$cron = preg_split("/[\s]+/i",trim($_cron_string));
$start = empty($_after_timestamp)?time():$_after_timestamp;
$date = array( 'minutes' =>self::_parseCronNumbers($cron[0],0,59),
'hours' =>self::_parseCronNumbers($cron[1],0,23),
'dom' =>self::_parseCronNumbers($cron[2],1,31),
'month' =>self::_parseCronNumbers($cron[3],1,12),
'dow' =>self::_parseCronNumbers($cron[4],0,6),
);
// limited to time()+366 - no need to check more than 1year ahead
for($i=0;$i<=60*60*24*366;$i+=60){
if( in_array(intval(date('j',$start+$i)),$date['dom']) &&
in_array(intval(date('n',$start+$i)),$date['month']) &&
in_array(intval(date('w',$start+$i)),$date['dow']) &&
in_array(intval(date('G',$start+$i)),$date['hours']) &&
in_array(intval(date('i',$start+$i)),$date['minutes'])
){
return $start+$i;
}
}
return null;
}
/**
* get a single cron style notation and parse it into numeric value
*
* @param string $s cron string element
* @param int $min minimum possible value
* @param int $max maximum possible value
* @return int parsed number
*/
protected static function _parseCronNumbers($s,$min,$max)
{
$result = array();
$v = explode(',',$s);

foreach($v as $vv){
$vvv = explode('/',$vv);
$step = empty($vvv[1])?1:$vvv[1];
$vvvv = explode('-',$vvv[0]);
$_min = count($vvvv)==2?$vvvv[0]:($vvv[0]=='*'?$min:$vvv[0]);
$_max = count($vvvv)==2?$vvvv[1]:($vvv[0]=='*'?$max:$vvv[0]);
for($i=$_min;$i<=$_max;$i+=$step){
$result[$i]=intval($i);

}
}
ksort($result);
return $result;
}
}
7 changes: 1 addition & 6 deletions core/Swoole/Config/swoole.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ process_rename = 1
keepalive = 1
expire_open = 1
max_request=2000
;daemonize=1
daemonize=1
backlog=128
;log_file => '/home/wwwlogs/swoole',##定义swoole的错误日志路径
heartbeat_idle_time=600
heartbeat_check_interval=60





[MOUDLE]
AUTOLOAD=1 #是否自动加载模块
Cache=Cache
Expand Down
42 changes: 37 additions & 5 deletions core/Swoole/Network/Swoole/BaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Time: 15:56
*/
namespace core\Swoole\Network\Swoole;
use core\Lib\Crontab;
use core\Lib\Route;
use core\Swoole\Network\Server;
use core\Swoole\Params;
Expand All @@ -30,11 +31,6 @@ public function onManagerStart($serv)
swoole_set_process_name("Swoole->Manage pid->{$pid}");
}






/**
* 请求时间
* 这是哪个傻逼写的代码
Expand All @@ -52,8 +48,41 @@ public function OnWorkStart($serv,$worker_id)
//在这里对需要的对象进行实例化 并且存储在sw对象中 让全局都能使用
//达到常驻内存
$this->WorkStartMoudle();
//完成定时器操作
// $this->crontab($serv,$worker_id);

}

// public function crontab($serv,$worker_id)
// {
// $config=$serv->allConfig;
// $crontabConfig=empty($config['crontab'])?'':$config['crontab'];
// if(!empty($crontabConfig)){
// foreach($crontabConfig as $k=>$v)
// {
// //计算出 下一次的执行时间
// if($worker_id==$v['taskID']){
//
// $serv->tick(1000,function() use($serv,$v){
// $this->crontabRun($serv,$v);
// });
// }
// }
// }
// }

// public function crontabRun($serv,$config,$times)
// {
// $time=$config['time'];
// $nextTime=Crontab::parse($time,$times);
// if($times==$nextTime){
// echo
// }
// }




public function WorkStartMoudle()
{
$config=Params::paramsModule(SWOOLE_FRAMWORK.DIRECTORY_SEPARATOR.'Config'.DIRECTORY_SEPARATOR.'swoole.ini');
Expand All @@ -72,6 +101,9 @@ public function WorkStartMoudle()
}





public function onRevicer($request,$response)
{
//过滤掉 request_uri /favicon.ico
Expand Down
7 changes: 4 additions & 3 deletions core/Swoole/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ public static function params()
echo "swoole is reload success\n";
exit();
}elseif($opt['s']=='start'){
// if(!empty($server_pid)){
// exit('Swoole Server is runing');
// }
if(!empty($server_pid)){
exit('Swoole Server is runing');
}
}elseif($opt['s']=='stop'){
if(empty($server_pid)){
exit('Swoole Server is not runing');
}
//杀死进程
posix_kill($server_pid,SIGTERM);
unlink($pid_file);
echo "swoole is stop success\n";
exit;
}else{
Expand Down
8 changes: 8 additions & 0 deletions mark
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
启动命令
/usr/local/php/bin/php /home/wwwroot/default/lphp/swooleServer/Main.php -t http -s start
-t 类型
-s 启动命令
访问:
index index
配置文件
AUTOLOAD=1 #是否自动加载模块 自动加载 需要加载redis 和mysql 如果测试 可以禁止掉
2 changes: 1 addition & 1 deletion swooleServer/pid/http.pid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5506
83800
2 changes: 1 addition & 1 deletion swooleServer/pid/socket.pid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61257
82333

0 comments on commit ffb4540

Please sign in to comment.