Skip to content

Commit be4e71d

Browse files
committed
Merge pull request #93 from tdt/development
Added more logging when jobs are pushed (or not) to the queue and pro…
2 parents eb78672 + 8a31b22 commit be4e71d

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Tdt\Input\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
9+
class ClearBeanstalk extends Command
10+
{
11+
/**
12+
* The console command name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'input:clearqueue';
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Clear a Beanstalkd queue, by deleting all pending jobs.';
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
/**
33+
* Defines the arguments.
34+
*
35+
* @return array
36+
*/
37+
public function getArguments()
38+
{
39+
return array(
40+
array('queue', InputArgument::OPTIONAL, 'The name of the queue to clear.'),
41+
);
42+
}
43+
/**
44+
* Execute the console command.
45+
*
46+
* @return void
47+
*/
48+
public function fire()
49+
{
50+
$queue = ($this->argument('queue')) ? $this->argument('queue') : \Config::get('queue.connections.beanstalkd.queue');
51+
52+
$this->info(sprintf('Clearing queue: %s', $queue));
53+
54+
$pheanstalk = \Queue::getPheanstalk();
55+
$pheanstalk->useTube($queue);
56+
$pheanstalk->watch($queue);
57+
58+
while ($job = $pheanstalk->reserve(0)) {
59+
$pheanstalk->delete($job);
60+
}
61+
62+
// Set the flag of the jobs
63+
$jobs = \Job::all();
64+
65+
foreach ($jobs as $job) {
66+
$job->added_to_queue = false;
67+
$job->save();
68+
}
69+
70+
$this->info('The Beankstalk queue has been cleared.');
71+
}
72+
}

src/Tdt/Input/Commands/TriggerJobs.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,60 @@ public function fire()
5050
$job_exec_time = new Carbon($exec_time);
5151

5252
$push_to_q = false;
53+
$diff_in_time_string = '';
5354

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

59+
$diff_in_time_string = $diff . ' hours';
60+
5861
if ($diff >= 6) {
5962
$push_to_q = true;
6063
}
6164
break;
6265
case 'daily':
6366
$diff = $now->diffInDays($job_exec_time);
6467

68+
$diff_in_time_string = $diff . ' days';
69+
6570
if ($diff >= 1) {
6671
$push_to_q = true;
6772
}
6873
break;
6974
case 'weekly':
7075
$diff = $now->diffInweeks($job_exec_time);
7176

77+
$diff_in_time_string = $diff . ' weeks';
78+
7279
if ($diff >= 1) {
7380
$push_to_q = true;
7481
}
7582
break;
7683
case 'monthly':
7784
$diff = $now->diffInMonths($job_exec_time);
7885

86+
$diff_in_time_string = $diff . ' months';
87+
7988
if ($diff >= 1) {
8089
$push_to_q = true;
8190
}
8291
break;
8392
}
8493

94+
$job_name = $job->collection_uri . '/' . $job->name;
95+
8596
if ($push_to_q) {
8697
$job->added_to_queue = true;
8798
$job->save();
8899

100+
$this->info("The job ($job_name) has been added to the queue.");
101+
89102
$this->executeCommand($job->collection_uri . '/' . $job->name);
103+
} else {
104+
$this->info("The job $job_name has not been added to the queue, time difference was $diff_in_time_string");
90105
}
106+
91107
}
92108
}
93109
}

src/Tdt/Input/InputServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Tdt\Input\Commands\Export;
88
use Tdt\Input\Commands\ExecuteJob;
99
use Tdt\Input\Commands\TriggerJobs;
10+
use Tdt\Input\Commands\ClearBeanstalk;
1011

1112
class InputServiceProvider extends ServiceProvider
1213
{
@@ -42,10 +43,16 @@ public function boot()
4243
return new TriggerJobs();
4344
});
4445

46+
$this->app['input.clearqueue'] = $this->app->share(function ($app) {
47+
return new ClearBeanstalk();
48+
});
49+
50+
4551
$this->commands('input.export');
4652
$this->commands('input.execute');
4753
$this->commands('input.import');
4854
$this->commands('input.triggerjobs');
55+
$this->commands('input.clearqueue');
4956

5057
include __DIR__ . '/../../routes.php';
5158
}

0 commit comments

Comments
 (0)