Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable entity manager #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Command/CronBaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace ColourStream\Bundle\CronBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

abstract class CronBaseCommand extends ContainerAwareCommand
{
/**
* @return Doctrine\ORM\EntityManager
*/
protected function getEntityManger()
{
return $this->getContainer()->get('doctrine')
->getManager($this->getContainer()
->getParameter('colour_stream_cron.entity_manager'));
}
}
14 changes: 6 additions & 8 deletions Command/CronDisableJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,31 @@

use ColourStream\Bundle\CronBundle\Entity\CronJobResult;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CronDisableJobCommand extends ContainerAwareCommand
class CronDisableJobCommand extends CronBaseCommand
{
protected function configure()
{
$this->setName("cron:disable-job")
->setDescription("Disables a cron job")
->addArgument("job", InputArgument::REQUIRED, "Name of the job to disable");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$jobName = $input->getArgument('job');
$em = $this->getContainer()->get("doctrine.orm.entity_manager");
$em = $this->getEntityManger();
$jobRepo = $em->getRepository('ColourStreamCronBundle:CronJob');

$job = $jobRepo->findOneByCommand($jobName);
if(!$job)
{
$output->writeln("Couldn't find a job by the name of " . $jobName);
return CronJobResult::FAILED;
}

$job->setEnabled(false);
$em->flush();

$output->writeln("Disabled cron job by the name of " . $jobName);
}
}
14 changes: 6 additions & 8 deletions Command/CronEnableJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,31 @@

use Symfony\Component\Console\Input\InputInterface;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CronEnableJobCommand extends ContainerAwareCommand
class CronEnableJobCommand extends CronBaseCommand
{
protected function configure()
{
$this->setName("cron:enable-job")
->setDescription("Enables a cron job")
->addArgument("job", InputArgument::REQUIRED, "Name of the job to enable");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$jobName = $input->getArgument('job');
$em = $this->getContainer()->get("doctrine.orm.entity_manager");
$em = $this->getEntityManger();
$jobRepo = $em->getRepository('ColourStreamCronBundle:CronJob');

$job = $jobRepo->findOneByCommand($jobName);
if(!$job)
{
$output->writeln("Couldn't find a job by the name of " . $jobName);
return CronJobResult::FAILED;
}

$job->setEnabled(true);
$em->flush();

$output->writeln("Enabled cron job by the name of " . $jobName);
}
}
20 changes: 9 additions & 11 deletions Command/CronPruneLogsCommand.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
<?php
namespace ColourStream\Bundle\CronBundle\Command;

use Fusion\Framework\CronBundle\Entity\CronJobResult;
use ColourStream\Bundle\CronBundle\Entity\CronJobResult;

use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Input\InputInterface;

use Symfony\Component\Console\Input\InputArgument;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CronPruneLogsCommand extends ContainerAwareCommand
class CronPruneLogsCommand extends CronBaseCommand
{
protected function configure()
{
$this->setName("cron:pruneLogs")
->setDescription("Prunes the logs for each cron job, leaving only recent failures and the most recent success")
->addArgument('job', InputArgument::OPTIONAL, 'Operate only on this job');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get("doctrine.orm.entity_manager");
$em = $this->getEntityManger();
$job = $input->getArgument('job');

if($job)
{
$output->writeln("Pruning logs for cron job $job");
Expand All @@ -33,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Pruning logs for all cron jobs");
}

if($job)
{
$jobObj = $em->getRepository('ColourStreamCronBundle:CronJob')->findOneByCommand($job);
Expand All @@ -42,17 +40,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("Couldn't find a job by the name of " . $job);
return CronJobResult::FAILED;
}

$em->getRepository('ColourStreamCronBundle:CronJobResult')->deleteOldLogs($jobObj);
}
else
{
$em->getRepository('ColourStreamCronBundle:CronJobResult')->deleteOldLogs();
}

// Flush the EM
$em->flush();

$output->writeln("Logs pruned successfully");
return CronJobResult::SUCCEEDED;
}
Expand Down
41 changes: 20 additions & 21 deletions Command/CronRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@

use Symfony\Component\Console\Input\InputArgument;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CronRunCommand extends ContainerAwareCommand
class CronRunCommand extends CronBaseCommand
{
protected function configure()
{
$this->setName("cron:run")
->setDescription("Runs any currently schedule cron jobs")
->addArgument("job", InputArgument::OPTIONAL, "Run only this job (if enabled)");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$start = microtime(true);
$em = $this->getContainer()->get("doctrine.orm.entity_manager");
$em = $this->getEntityManger();
$jobRepo = $em->getRepository('ColourStreamCronBundle:CronJob');

$jobsToRun = array();
if($jobName = $input->getArgument('job'))
{
Expand All @@ -52,27 +51,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$jobsToRun = $jobRepo->findDueTasks();
}

$jobCount = count($jobsToRun);
$output->writeln("Running $jobCount jobs:");

foreach($jobsToRun as $job)
{
$this->runJob($job, $output, $em);
}
// Flush our results to the DB

// Flush our results to the DB
$em->flush();

$end = microtime(true);
$duration = sprintf("%0.2f", $end-$start);
$output->writeln("Cron run completed in $duration seconds");
}

protected function runJob(CronJob $job, OutputInterface $output, EntityManager $em)
{
$output->write("Running " . $job->getCommand() . ": ");

try
{
$commandToRun = $this->getApplication()->get($job->getCommand());
Expand All @@ -85,10 +84,10 @@ protected function runJob(CronJob $job, OutputInterface $output, EntityManager $
// No need to reschedule non-existant commands
return;
}

$emptyInput = new ArgvInput();
$jobOutput = new MemoryWriter();

$jobStart = microtime(true);
try
{
Expand All @@ -102,13 +101,13 @@ protected function runJob(CronJob $job, OutputInterface $output, EntityManager $
$jobOutput->writeln($ex->__toString());
}
$jobEnd = microtime(true);

// 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)
Expand All @@ -123,19 +122,19 @@ protected function runJob(CronJob $job, OutputInterface $output, EntityManager $
{
$statusStr = "failed";
}

$durationStr = sprintf("%0.2f", $jobEnd-$jobStart);
$output->writeln("$statusStr in $durationStr seconds");

// Record the result
$this->recordJobResult($em, $job, $jobEnd-$jobStart, $jobOutput->getOutput(), $returnCode);

// And update the job with it's next scheduled time
$newTime = new \DateTime();
$newTime = $newTime->add(new \DateInterval($job->getInterval()));
$job->setNextRun($newTime);
}

protected function recordJobResult(EntityManager $em, CronJob $job, $timeTaken, $output, $resultCode)
{
// Create a new CronJobResult
Expand All @@ -144,7 +143,7 @@ protected function recordJobResult(EntityManager $em, CronJob $job, $timeTaken,
$result->setRunTime($timeTaken);
$result->setOutput($output);
$result->setResult($resultCode);

// Then update associations and persist it
$job->setMostRecentRun($result);
$em->persist($result);
Expand Down
25 changes: 12 additions & 13 deletions Command/CronScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

use Symfony\Component\Console\Input\InputOption;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CronScanCommand extends ContainerAwareCommand
class CronScanCommand extends CronBaseCommand
{
protected function configure()
{
Expand All @@ -27,21 +26,21 @@ protected function configure()
->addOption('keep-deleted', 'k', InputOption::VALUE_NONE, 'If set, deleted cron jobs will not be removed')
->addOption('default-disabled', 'd', InputOption::VALUE_NONE, 'If set, new jobs will be disabled by default');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$keepDeleted = $input->getOption("keep-deleted");
$defaultDisabled = $input->getOption("default-disabled");
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$em = $this->getEntityManger();

// Enumerate the known jobs
$jobRepo = $em->getRepository('ColourStreamCronBundle:CronJob');
$knownJobs = $jobRepo->getKnownJobs();
$knownJobs = array_fill_keys($knownJobs, true);

// Enumerate all the jobs currently loaded
$reader = $this->getContainer()->get("annotation_reader");

foreach($this->getApplication()->all() as $command)
{
// Check for an @CronJob annotation
Expand All @@ -55,15 +54,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
// Clear it from the known jobs so that we don't try to delete it
unset($knownJobs[$job]);

// Update the job if necessary
$currentJob = $jobRepo->findOneByCommand($job);
$currentJob->setDescription($command->getDescription());
if($currentJob->getInterval() != $anno->value)
{
$newTime = new \DateTime();
$newTime = $newTime->add(new \DateInterval($anno->value));

$currentJob->setInterval($anno->interval);
$currentJob->setNextRun($newTime);
$output->writeln("Updated interval for $job to {$anno->value}");
Expand All @@ -76,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

// Clear any jobs that weren't found
if(!$keepDeleted)
{
Expand All @@ -87,11 +86,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$em->remove($jobToDelete);
}
}

$em->flush();
$output->writeln("Finished scanning for cron jobs");
}

protected function newJobFound(EntityManager $em, OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false)
{
$newJob = new CronJob();
Expand All @@ -100,7 +99,7 @@ protected function newJobFound(EntityManager $em, OutputInterface $output, Comma
$newJob->setInterval($anno->value);
$newJob->setNextRun(new \DateTime());
$newJob->setEnabled(!$defaultDisabled);

$output->writeln("Added the job " . $newJob->getCommand() . " with interval " . $newJob->getInterval());
$em->persist($newJob);
}
Expand Down
Loading