Skip to content

Commit a02846d

Browse files
committed
fix: only retrieve enabled entries, fixes #98
1 parent a419850 commit a02846d

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

resources/jobby-pdo.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
// * * * * * cd /path/to/project && php jobby-pdo.php 1>> /dev/null 2>&1
1111
//
1212

13+
use Jobby\Jobby;
14+
use Opis\Closure\SerializableClosure;
15+
use Jobby\Exception;
16+
1317
require_once __DIR__ . '/../vendor/autoload.php';
1418

1519
// The table, which shall contain the cronjob-configuration(s).
@@ -53,24 +57,21 @@
5357
)
5458
");
5559

56-
$insertCronJobConfiguration = $dbh->prepare("
57-
INSERT INTO `$dbhJobbiesTableName`
58-
(`name`,`command`,`schedule`,`output`)
59-
VALUES
60-
(?,?,?,?)
61-
");
60+
$insertCronJobConfiguration = $dbh->prepare(
61+
"INSERT INTO `$dbhJobbiesTableName` (`name`,`command`,`schedule`,`output`) VALUES (?,?,?,?)"
62+
);
6263
// First demo-job - print "date" to logs/command-pdo.log.
6364
$insertCronJobConfiguration->execute(
6465
['CommandExample', 'date', '* * * * *', 'logs/command-pdo.log']
6566
);
6667
// Second demo-job - a Closure which does some php::echo(). The Closure is saved to PDO-backend, too.
67-
$secondJobFn = function() {
68+
$secondJobFn = static function () {
6869
echo "I'm a function (" . date('Y-m-d H:i:s') . ')!' . PHP_EOL;
6970
return true;
7071
};
71-
$serializer = new SuperClosure\Serializer();
7272

73-
$secondJobFnSerialized = $serializer->serialize($secondJobFn);
73+
$wrapper = new SerializableClosure($secondJobFn);
74+
$secondJobFnSerialized = serialize($wrapper);
7475
$insertCronJobConfiguration->execute(
7576
['ClosureExample', $secondJobFnSerialized, '* * * * *', 'logs/closure-pdo.log']
7677
);
@@ -81,24 +82,22 @@
8182
* Now, fetch all jobbies from PDO-backend and run them.
8283
*/
8384

84-
$jobbiesStmt = $dbh->query("SELECT * FROM `$dbhJobbiesTableName`");
85+
$jobbiesStmt = $dbh->query("SELECT * FROM `$dbhJobbiesTableName` WHERE enabled = 1");
8586
$jobbies = $jobbiesStmt->fetchAll(PDO::FETCH_ASSOC);
8687

87-
$jobby = new \Jobby\Jobby();
88+
$jobby = new Jobby();
8889

8990
foreach ($jobbies as $job) {
9091
// Filter out each value, which is not set (for example, "maxRuntime" is not defined in the job).
9192
$job = array_filter($job);
92-
93-
try {
94-
$job['closure'] = $serializer->unserialize($job['command']);
95-
unset($job['command']);
96-
} catch (SuperClosure\Exception\ClosureUnserializationException $e) {
97-
}
98-
93+
$job['closure'] = unserialize($job['command']);
9994
$jobName = $job['name'];
10095
unset($job['name']);
101-
$jobby->add($jobName, $job);
96+
try {
97+
$jobby->add($jobName, $job);
98+
} catch (Exception $e) {
99+
die($e->getMessage());
100+
}
102101
}
103102

104103
$jobby->run();

0 commit comments

Comments
 (0)