Skip to content

Commit c320c9b

Browse files
committed
process timeouts and some refactoring
1 parent ca248b7 commit c320c9b

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

src/Gitonomy/Git/Admin.php

+26-32
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,7 @@ class Admin
3535
*/
3636
public static function init($path, $bare = true, array $options = array())
3737
{
38-
$command = isset($options['command']) ? $options['command'] : 'git';
39-
40-
// command-line
41-
$builder = ProcessBuilder::create(array($command, 'init', '-q'));
42-
if ($bare) {
43-
$builder->add('--bare');
44-
}
45-
$builder->add($path);
46-
47-
// environment
48-
$builder->inheritEnvironmentVariables(false);
49-
$process = $builder->getProcess();
50-
if (isset($options['environment_variables'])) {
51-
$process->setEnv($options['environment_variables']);
52-
}
38+
$process = static::getProcess('init', array_merge(array('-q'), $bare ? array('--bare') : array(), array($path)), $options);
5339

5440
$process->run();
5541

@@ -74,11 +60,8 @@ public static function init($path, $bare = true, array $options = array())
7460
*/
7561
public static function isValidRepository($url, array $options = array())
7662
{
77-
$command = isset($options['command']) ? $options['command'] : 'git';
78-
$builder = ProcessBuilder::create(array($command, 'ls-remote'));
79-
$builder->add($url);
63+
$process = static::getProcess('ls-remote', array($url), $options);
8064

81-
$process = $builder->getProcess();
8265
$process->run();
8366

8467
return $process->isSuccessFul();
@@ -127,19 +110,7 @@ public static function mirrorTo($path, $url, array $options = array())
127110
*/
128111
private static function cloneRepository($path, $url, array $args = array(), array $options = array())
129112
{
130-
$command = isset($options['command']) ? $options['command'] : 'git';
131-
$builder = ProcessBuilder::create(array($command, 'clone', '-q'));
132-
foreach ($args as $value) {
133-
$builder->add($value);
134-
}
135-
$builder->add($url);
136-
$builder->add($path);
137-
138-
$builder->inheritEnvironmentVariables(false);
139-
$process = $builder->getProcess();
140-
if (isset($options['environment_variables'])) {
141-
$process->setEnv($options['environment_variables']);
142-
}
113+
$process = static::getProcess('clone', array_merge(array('-q'), $args, array($url, $path)), $options);
143114

144115
$process->run();
145116

@@ -149,4 +120,27 @@ private static function cloneRepository($path, $url, array $args = array(), arra
149120

150121
return new Repository($path, $options);
151122
}
123+
124+
/**
125+
* This internal method is used to create a process object.
126+
*/
127+
private static function getProcess($command, array $args = array(), array $options = array())
128+
{
129+
$options = array_merge(array(
130+
'environment_variables' => array(),
131+
'command' => 'git',
132+
'process_timeout' => 3600
133+
), $options);
134+
135+
$builder = ProcessBuilder::create(array_merge(array($options['command'], $command), $args));
136+
$builder->inheritEnvironmentVariables(false);
137+
138+
$process = $builder->getProcess();
139+
$process->setEnv($options['environment_variables']);
140+
$process->setTimeout($options['process_timeout']);
141+
$process->setIdleTimeout($options['process_timeout']);
142+
143+
return $process;
144+
}
145+
152146
}

src/Gitonomy/Git/Repository.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ class Repository
8888
*/
8989
protected $environmentVariables;
9090

91+
/**
92+
* Timeout that should be set for every running process.
93+
*
94+
* @var int
95+
*/
96+
protected $processTimeout;
97+
9198
/**
9299
* Constructs a new repository.
93100
*
@@ -113,7 +120,8 @@ public function __construct($dir, $options = array())
113120
'debug' => true,
114121
'logger' => null,
115122
'environment_variables' => array(),
116-
'command' => 'git'
123+
'command' => 'git',
124+
'process_timeout' => 3600
117125
), $options);
118126

119127
if (null !== $options['logger'] && ! $options['logger'] instanceof LoggerInterface) {
@@ -126,6 +134,7 @@ public function __construct($dir, $options = array())
126134
$this->objects = array();
127135
$this->debug = (bool) $options['debug'];
128136
$this->environmentVariables = $options['environment_variables'];
137+
$this->processTimeout = $options['process_timeout'];
129138
$this->command = $options['command'];
130139

131140
if (true === $this->debug && null !== $this->logger) {
@@ -615,6 +624,8 @@ private function getProcess($command, $args = array())
615624
$builder->inheritEnvironmentVariables(false);
616625
$process = $builder->getProcess();
617626
$process->setEnv($this->environmentVariables);
627+
$process->setTimeout($this->processTimeout);
628+
$process->setIdleTimeout($this->processTimeout);
618629

619630
return $process;
620631
}

tests/Gitonomy/Git/Tests/AbstractTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ protected static function getOptions()
131131
return array(
132132
'command' => $command,
133133
'environment_variables' => $envs,
134+
'process_timeout' => 60
134135
);
135136
}
136137
}

0 commit comments

Comments
 (0)