Skip to content

Instantly share code, notes, and snippets.

@Petah
Created November 7, 2012 22:25
Show Gist options
  • Save Petah/4034927 to your computer and use it in GitHub Desktop.
Save Petah/4034927 to your computer and use it in GitHub Desktop.
Example background thread script
// Get the current working directory
$current = getcwd();
// An array of real paths of directories already run
$done = array();
// Background processes
$processes = array();
foreach ($directories as $directory) {
// Add directory to the done list, or continue if already done
$directory = realpath($directory);
if (isset($done[$directory])) continue;
$done[$directory] = true;
// Change to the repository directory
chdir($directory);
foreach ($commands as $command) {
if (!$this->execute) {
$this->out->line("Run (in $directory): $this->git $command");
} elseif ($this->background) {
$processes[] = $this->background($directory, "$this->git $command");
} else {
if (!$this->quiet) $this->out->line("Running $this->git $command on $directory");
$this->run("$this->git $command");
}
}
}
// Change back to the old working directory
chdir($current);
// If we did not run in the background, we are done
if (!$this->background) return;
$this->timeout = time() + $this->timeout;
if (!$this->quiet) $this->out->line('Waiting for git, timeout in: '.($this->timeout - time()).'secs ('.sizeof($processes).' to go)');
while (sizeof($processes) > 0 && time() < $this->timeout) {
sleep(2);
if ($this->verbose) $this->out->line('Waiting for git, timeout in: '.($this->timeout - time()).'secs ('.sizeof($processes).' to go)');
foreach ($processes as $i => $process) {
$contents = file_get_contents($process[0]);
$contents = trim($contents);
$length = strlen($contents);
if (substr($contents, $length - strlen($process[1]), $length) == $process[1]) {
if ($this->verbose) $this->out->line("Deleting: $process[0]");
unlink($process[0]);
if ($this->os == 'windows') {
if ($this->verbose) $this->out->line("Deleting: $process[0].bat");
unlink($process[0].'.bat');
} elseif ($this->os == 'linux') {
if ($this->verbose) $this->out->line("Deleting: $process[0].sh");
unlink($process[0].'.sh');
}
if (!$this->quiet) $this->out->border($contents);
unset($processes[$i]);
}
}
}
protected function background($directory, $command) {
$temp_file = tempnam(sys_get_temp_dir(), 'xmod-git-');
$exit_signal = uniqid('xmod-git-exit-');
if ($this->verbose) $this->out->line("Created temp file: $temp_file (exit signal: $exit_signal)");
if ($this->os == 'windows') {
$bat_file = $temp_file.'.bat';
if ($this->verbose) $this->out->line("Creating batch file: $bat_file");
file_put_contents($bat_file, "
cd $directory
cd > $temp_file
$command >> $temp_file
echo $exit_signal >> $temp_file
");
if ($this->verbose) $this->out->line("Running: cmd /C $bat_file");
$com = new COM("WScript.Shell");
$com->Run("cmd /C $bat_file", 0, false);
} elseif ($this->os == 'linux') {
$sh_file = $temp_file.'.sh';
if ($this->verbose) $this->out->line("Creating bash file: $sh_file");
file_put_contents($sh_file, "
pwd >> $temp_file 1>> $temp_file 2>> $temp_file
$command >> $temp_file 1>> $temp_file 2>> $temp_file
echo $exit_signal >> $temp_file 1>> $temp_file 2>> $temp_file
");
chmod($sh_file, 0777);
if ($this->verbose) $this->out->line("Running: $sh_file > /dev/null &");
exec("$sh_file > /dev/null &");
}
return array($temp_file, $exit_signal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment