Skip to content

Commit

Permalink
命令行。。。。
Browse files Browse the repository at this point in the history
  • Loading branch information
xsir317 committed Nov 27, 2014
1 parent 58054fc commit 2afdaf0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
70 changes: 67 additions & 3 deletions publisher/app/commands/TaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TaskCommand extends Command {
* @var string
*/
protected $name = 'tasks:run';
private $filelock = '';

/**
* The console command description.
Expand All @@ -29,6 +30,7 @@ class TaskCommand extends Command {
public function __construct()
{
parent::__construct();
$this->filelock = app_path().'/storage/taskrun.pid';
}

/**
Expand All @@ -38,9 +40,71 @@ public function __construct()
*/
public function fire()
{
echo "hello , ".$this->argument('example');
//启动之前先检查进程是否还活着,如果还活着咱就退
if(file_exists($this->filelock) && posix_kill(intval(file_get_contents($this->filelock)),0))
{
die("pid exists,exit");
}
file_put_contents($this->filelock, getmypid());
Tasks::where("status" , "execute")->update(array("status"=>"created"));
while (true) {
//获取任务,执行任务
$tasks = Tasks::where('status' , 'created')->lists('id');
foreach ($tasks as $_id)
{
$this->_run($_id);
}
sleep(1);
}
}

private function _run($id)
{
$task = Tasks::find($id);
//如果task不存在或者状态不对
if(!$task || $task->status != 'created')
{
return;
}
//如果task有前置任务,而且前置任务没完成
if($task->pre_task && $task->pre()->status != 'success')
{
return;
}
$task->status = 'execute';
$task->save();
$_func = sprintf('_run_%s',$task->type);
$result = $this->$_func($task);
$task->execute_time = date('Y-m-d H:i:s');
$task->status = $result['result'] ? 'success':'failed';
$task->output = $result['output'];
$task->save();
}

private function _run_checkout($task)
{
//如果目录非空,失败
//checkout到指定目录
//返回
}

private function _run_update($task)
{
//根据命令,运行update指令
//返回
}

private function _run_delete($task)
{
//清空指定目录
}

private function _run_rsync($task)
{
//运行rsync
}


/**
* Get the console command arguments.
*
Expand All @@ -49,7 +113,7 @@ public function fire()
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
//array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}

Expand All @@ -61,7 +125,7 @@ protected function getArguments()
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
//array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}

Expand Down
4 changes: 4 additions & 0 deletions publisher/app/models/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class Tasks extends Eloquent {
'failed' => '失败'
);

public function pre()
{
return $this->belongsTo('Tasks','pre_task');
}
}

0 comments on commit 2afdaf0

Please sign in to comment.