Created
August 27, 2012 10:51
-
-
Save predakanga/3487417 to your computer and use it in GitHub Desktop.
Code for running commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The actual command which is used to call other commands | |
*/ | |
class CommandRunCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this->setName("cmd:run") | |
->setDescription("Runs another command, recording the result") | |
->addArgument("job", InputArgument::REQUIRED, "Run only this job (if scheduled)"); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$start = microtime(true); | |
$jobToRun = $input->getArgument('job'); | |
$output->writeln("Running $jobToRun"); | |
$this->runJob($jobToRun, $output); | |
$end = microtime(true); | |
$duration = sprintf("%0.2f", $end-$start); | |
$output->writeln("Command completed in $duration seconds"); | |
} | |
protected function runJob($jobName, OutputInterface $output) | |
{ | |
$output->write("Running " . $jobName . ": "); | |
try | |
{ | |
// Get the command from the Symfony application | |
$commandToRun = $this->getApplication()->get($job->getCommand()); | |
} | |
catch(InvalidArgumentException $ex) | |
{ | |
$output->writeln(" skipped (command no longer exists)"); | |
$this->recordJobResult($jobName, 0, "Command no longer exists", CronJobResult::SKIPPED); | |
// No need to reschedule non-existant commands | |
return; | |
} | |
$emptyInput = new ArgvInput(); | |
$jobOutput = new MemoryWriter(); | |
$jobStart = microtime(true); | |
try | |
{ | |
// Actually run the child command - it's really this simple | |
$returnCode = $commandToRun->execute($emptyInput, $jobOutput); | |
} | |
catch(\Exception $ex) | |
{ | |
// This code could probably be made simpler, but it works for my purposes | |
$returnCode = CronJobResult::FAILED; | |
$jobOutput->writeln(""); | |
$jobOutput->writeln("Job execution failed with exception " . get_class($ex) . ":"); | |
$jobOutput->writeln($ex->__toString()); | |
} | |
$jobEnd = microtime(true); | |
// The following code just picks a status string based on what the command returned | |
// Clamp the result to accepted values | |
if($returnCode < CronJobResult::RESULT_MIN || $returnCode > CronJobResult::RESULT_MAX) | |
{ | |
$returnCode = CronJobResult::FAILED; | |
} | |
// Output the result | |
$statusStr = "unknown"; | |
if($returnCode == CronJobResult::SKIPPED) | |
{ | |
$statusStr = "skipped"; | |
} | |
elseif($returnCode == CronJobResult::SUCCEEDED) | |
{ | |
$statusStr = "succeeded"; | |
} | |
elseif($returnCode == CronJobResult::FAILED) | |
{ | |
$statusStr = "failed"; | |
} | |
$durationStr = sprintf("%0.2f", $jobEnd-$jobStart); | |
$output->writeln("$statusStr in $durationStr seconds"); | |
// Record the result | |
$this->recordJobResult($jobName, $jobEnd-$jobStart, $jobOutput->getOutput(), $returnCode); | |
} | |
protected function recordJobResult($jobName, $timeTaken, $output, $resultCode) | |
{ | |
// Store your job result however you want in here - I use the database | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Part of my persistence code - just included so some of the runJob() code makes sense | |
*/ | |
class CronJobResult | |
{ | |
// Constants to be used as return codes from commands - similar to the unix application return code concept | |
const RESULT_MIN = 0; | |
const SUCCEEDED = 0; | |
const FAILED = 1; | |
const SKIPPED = 2; | |
const RESULT_MAX = 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one thing i did not mention and this is the reason for the process is that the command is not a command of the same app but of a different command, is a silex command.