Skip to content

Commit

Permalink
Merge pull request #1 from droid-php/develop
Browse files Browse the repository at this point in the history
Add cron:addjob and cron:deljob commands.
  • Loading branch information
joostfaassen committed Jun 21, 2016
2 parents 4498e3f + 75f8e8b commit f1a62d7
Show file tree
Hide file tree
Showing 12 changed files with 1,281 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bin/droid-cron
@@ -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();
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -26,5 +26,8 @@
"Droid\\Plugin\\Cron\\": "src/"
}
},
"bin": [
"bin/droid-cron"
],
"license": "MIT"
}
168 changes: 168 additions & 0 deletions src/Command/JobCreateCommand.php
@@ -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;
}
}
82 changes: 82 additions & 0 deletions src/Command/JobDeleteCommand.php
@@ -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;
}
}
6 changes: 6 additions & 0 deletions src/DroidPlugin.php
Expand Up @@ -2,6 +2,10 @@

namespace Droid\Plugin\Cron;

use Droid\Plugin\Cron\Command\JobCreateCommand;
use Droid\Plugin\Cron\Command\JobDeleteCommand;
use Droid\Plugin\Cron\Model\JobFactory;

class DroidPlugin
{
public function __construct($droid)
Expand All @@ -12,6 +16,8 @@ public function __construct($droid)
public function getCommands()
{
return array(
new JobCreateCommand(new JobFactory($this->droid, '/etc/cron.d')),
new JobDeleteCommand(new JobFactory($this->droid, '/etc/cron.d')),
);
}
}

0 comments on commit f1a62d7

Please sign in to comment.