Skip to content

Commit

Permalink
Merge pull request #93 from tdt/development
Browse files Browse the repository at this point in the history
Added more logging when jobs are pushed (or not) to the queue and pro…
  • Loading branch information
coreation committed Jan 9, 2016
2 parents eb78672 + 8a31b22 commit be4e71d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Tdt/Input/Commands/ClearBeanstalk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Tdt\Input\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ClearBeanstalk extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'input:clearqueue';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear a Beanstalkd queue, by deleting all pending jobs.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Defines the arguments.
*
* @return array
*/
public function getArguments()
{
return array(
array('queue', InputArgument::OPTIONAL, 'The name of the queue to clear.'),
);
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$queue = ($this->argument('queue')) ? $this->argument('queue') : \Config::get('queue.connections.beanstalkd.queue');

$this->info(sprintf('Clearing queue: %s', $queue));

$pheanstalk = \Queue::getPheanstalk();
$pheanstalk->useTube($queue);
$pheanstalk->watch($queue);

while ($job = $pheanstalk->reserve(0)) {
$pheanstalk->delete($job);
}

// Set the flag of the jobs
$jobs = \Job::all();

foreach ($jobs as $job) {
$job->added_to_queue = false;
$job->save();
}

$this->info('The Beankstalk queue has been cleared.');
}
}
16 changes: 16 additions & 0 deletions src/Tdt/Input/Commands/TriggerJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,60 @@ public function fire()
$job_exec_time = new Carbon($exec_time);

$push_to_q = false;
$diff_in_time_string = '';

switch ($job->schedule) {
case 'half-daily':
$diff = $now->diffInHours($job_exec_time);

$diff_in_time_string = $diff . ' hours';

if ($diff >= 6) {
$push_to_q = true;
}
break;
case 'daily':
$diff = $now->diffInDays($job_exec_time);

$diff_in_time_string = $diff . ' days';

if ($diff >= 1) {
$push_to_q = true;
}
break;
case 'weekly':
$diff = $now->diffInweeks($job_exec_time);

$diff_in_time_string = $diff . ' weeks';

if ($diff >= 1) {
$push_to_q = true;
}
break;
case 'monthly':
$diff = $now->diffInMonths($job_exec_time);

$diff_in_time_string = $diff . ' months';

if ($diff >= 1) {
$push_to_q = true;
}
break;
}

$job_name = $job->collection_uri . '/' . $job->name;

if ($push_to_q) {
$job->added_to_queue = true;
$job->save();

$this->info("The job ($job_name) has been added to the queue.");

$this->executeCommand($job->collection_uri . '/' . $job->name);
} else {
$this->info("The job $job_name has not been added to the queue, time difference was $diff_in_time_string");
}

}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Tdt/Input/InputServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tdt\Input\Commands\Export;
use Tdt\Input\Commands\ExecuteJob;
use Tdt\Input\Commands\TriggerJobs;
use Tdt\Input\Commands\ClearBeanstalk;

class InputServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -42,10 +43,16 @@ public function boot()
return new TriggerJobs();
});

$this->app['input.clearqueue'] = $this->app->share(function ($app) {
return new ClearBeanstalk();
});


$this->commands('input.export');
$this->commands('input.execute');
$this->commands('input.import');
$this->commands('input.triggerjobs');
$this->commands('input.clearqueue');

include __DIR__ . '/../../routes.php';
}
Expand Down

0 comments on commit be4e71d

Please sign in to comment.