Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from droid-php/develop
Add cron:addjob and cron:deljob commands.
- Loading branch information
Showing
12 changed files
with
1,281 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Symfony\Component\Console\Application; | ||
|
||
use Droid\Plugin\Cron\DroidPlugin; | ||
|
||
$loader = __DIR__ . '/../vendor/autoload.php'; | ||
|
||
if (!file_exists($loader)) { | ||
$loader = __DIR__ . '/../../../autoload.php'; | ||
} | ||
|
||
if (!file_exists($loader)) { | ||
die( | ||
'You must set up the project dependencies, run the following commands:' . PHP_EOL . | ||
'curl -s http://getcomposer.org/installer | php' . PHP_EOL . | ||
'php composer.phar install' . PHP_EOL | ||
); | ||
} | ||
|
||
require $loader; | ||
|
||
$application = new Application('Droid Cron', '1.0.0'); | ||
$application->setCatchExceptions(true); | ||
$registry = new DroidPlugin($application); | ||
foreach ($registry->getCommands() as $command) { | ||
$application->add($command); | ||
} | ||
$application->run(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,8 @@ | |
"Droid\\Plugin\\Cron\\": "src/" | ||
} | ||
}, | ||
"bin": [ | ||
"bin/droid-cron" | ||
], | ||
"license": "MIT" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
namespace Droid\Plugin\Cron\Command; | ||
|
||
use RuntimeException; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use Droid\Lib\Plugin\Command\CheckableTrait; | ||
use Droid\Plugin\Cron\Model\JobFactory; | ||
|
||
class JobCreateCommand extends Command | ||
{ | ||
use CheckableTrait; | ||
|
||
protected $jobFactory; | ||
|
||
public function __construct(JobFactory $jobFactory, $name = null) | ||
{ | ||
$this->jobFactory = $jobFactory; | ||
return parent::__construct($name); | ||
} | ||
|
||
public function configure() | ||
{ | ||
$this | ||
->setName('cron:addjob') | ||
->setDescription('Create a cron job.') | ||
->addArgument( | ||
'name', | ||
InputArgument::REQUIRED, | ||
'The name of the job.' | ||
) | ||
->addArgument( | ||
'schedule', | ||
InputArgument::REQUIRED, | ||
'The job schedule.' | ||
) | ||
->addArgument( | ||
'username', | ||
InputArgument::REQUIRED, | ||
'The name of the user account under which to run the job command.' | ||
) | ||
->addArgument( | ||
'job-command', | ||
InputArgument::REQUIRED, | ||
'The job command.' | ||
) | ||
->addOption( | ||
'force', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Overwrite an existing job of the same name.' | ||
) | ||
->addOption( | ||
'mail', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'Comma seperated list of user names or email addresses to which to email the job output.' | ||
) | ||
->addOption( | ||
'no-mail', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Do not email the job output.' | ||
) | ||
->addOption( | ||
'home', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'A value for the HOME environment setting.' | ||
) | ||
->addOption( | ||
'path', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'A value for the PATH environment setting.' | ||
) | ||
->addOption( | ||
'shell', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'A value for the SHELL environment setting.' | ||
) | ||
; | ||
$this->configureCheckMode(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->activateCheckMode($input); | ||
|
||
$job = $this->jobFactory->create($input->getArgument('name')); | ||
|
||
if (!$input->getOption('force') && file_exists($job->getFilePath())) { | ||
throw new RuntimeException( | ||
sprintf( | ||
'The job named "%s" already exists. Use the --force.', | ||
$job->name | ||
) | ||
); | ||
} | ||
|
||
$job | ||
->setSchedule($input->getArgument('schedule')) | ||
->setUsername($input->getArgument('username')) | ||
->setCommand($input->getArgument('job-command')) | ||
; | ||
|
||
$this->markChange(); | ||
|
||
if ($this->checkMode()) { | ||
$output->writeln( | ||
sprintf('I would create the job "%s".', $job->name) | ||
); | ||
$this->reportChange($output); | ||
return 0; | ||
} | ||
|
||
if ($input->getOption('no-mail')) { | ||
$job->setEnv('MAILTO', ''); | ||
} elseif ($input->getOption('mail')) { | ||
$job->setEnv('MAILTO', $input->getOption('mail')); | ||
} | ||
|
||
if ($input->getOption('home')) { | ||
$job->setEnv('HOME', $input->getOption('home')); | ||
} | ||
if ($input->getOption('path')) { | ||
$job->setEnv('PATH', $input->getOption('path')); | ||
} | ||
if ($input->getOption('shell')) { | ||
$job->setEnv('SHELL', $input->getOption('shell')); | ||
} | ||
|
||
$fh = @fopen($job->getFilePath(), 'wb'); | ||
if ($fh === false) { | ||
throw new RuntimeException( | ||
sprintf( | ||
'I cannot open the file "%s" for writing.', | ||
$job->getFilePath() | ||
) | ||
); | ||
} | ||
$content = (string) $job; | ||
fwrite($fh, $content, strlen($content)); | ||
fclose($fh); | ||
if (!chmod($job->getFilePath(), 0640)) { | ||
throw new RuntimeException( | ||
sprintf( | ||
'I have created the job file "%s" but cannot set the file mode to 0640.', | ||
$job->getFilePath() | ||
) | ||
); | ||
} | ||
|
||
$output->writeln( | ||
sprintf('I have successfully created the job "%s".', $job->name) | ||
); | ||
|
||
$this->reportChange($output); | ||
return 0; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Droid\Plugin\Cron\Command; | ||
|
||
use RuntimeException; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use Droid\Lib\Plugin\Command\CheckableTrait; | ||
use Droid\Plugin\Cron\Model\JobFactory; | ||
|
||
class JobDeleteCommand extends Command | ||
{ | ||
use CheckableTrait; | ||
|
||
protected $jobFactory; | ||
|
||
public function __construct(JobFactory $jobFactory, $name = null) | ||
{ | ||
$this->jobFactory = $jobFactory; | ||
return parent::__construct($name); | ||
} | ||
|
||
public function configure() | ||
{ | ||
$this | ||
->setName('cron:deljob') | ||
->setDescription('Delete a cron job.') | ||
->addArgument( | ||
'name', | ||
InputArgument::REQUIRED, | ||
'The name of the job.' | ||
) | ||
; | ||
$this->configureCheckMode(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->activateCheckMode($input); | ||
|
||
$job = $this->jobFactory->create($input->getArgument('name')); | ||
$filename = $job->getFilePath(); | ||
|
||
if (!file_exists($filename)) { | ||
$output->writeln( | ||
sprintf( | ||
'The job file "%s" does not exist. Nothing to do.', | ||
$filename | ||
) | ||
); | ||
$this->reportChange($output); | ||
return 0; | ||
} | ||
|
||
$this->markChange(); | ||
|
||
if ($this->checkMode()) { | ||
$output->writeln( | ||
sprintf('I would delete the job "%s".', $job->name) | ||
); | ||
$this->reportChange($output); | ||
return 0; | ||
} | ||
|
||
if (!unlink($filename)) { | ||
throw new RuntimeException( | ||
sprintf('I cannot delete the job file "%s".', $filename) | ||
); | ||
} | ||
|
||
$output->writeln( | ||
sprintf('I have successfully deleted the job "%s".', $job->name) | ||
); | ||
|
||
$this->reportChange($output); | ||
return 0; | ||
} | ||
} |
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
Oops, something went wrong.