jobby-pdo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. //
  3. // This script demonstrates how to use jobby with a PDO-backend, which is used to
  4. // save the jobby-cronjob/jobbies configuration.
  5. //
  6. // Adapt this file to your needs, copy it to your project-root,
  7. // and add this line to your crontab file:
  8. //
  9. // * * * * * cd /path/to/project && php jobby-pdo.php 1>> /dev/null 2>&1
  10. //
  11. require_once __DIR__ . '/../vendor/autoload.php';
  12. // The table, which shall contain the cronjob-configuration(s).
  13. $dbhJobbiesTableName = 'jobbies';
  14. /*
  15. * For demo-purposes, an in-memory SQLite database is used.
  16. *
  17. * !!! REPLACE WITH YOUR OWN DATASOURCE!!!
  18. */
  19. $dbh = new PDO('sqlite::memory:');
  20. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. /*
  22. * Setup a test-fixture, having two jobs, first one is a system-cmd (date), second one is a Closure
  23. * (which is saved to pdo-database).
  24. */
  25. $dbh->exec("
  26. CREATE TABLE IF NOT EXISTS `$dbhJobbiesTableName`
  27. (`name` VARCHAR(255) NOT NULL ,
  28. `command` TEXT NOT NULL ,
  29. `schedule` VARCHAR(255) NOT NULL ,
  30. `mailer` VARCHAR(255) NULL DEFAULT 'sendmail' ,
  31. `maxRuntime` INT UNSIGNED NULL ,
  32. `smtpHost` VARCHAR(255) NULL ,
  33. `smtpPort` SMALLINT UNSIGNED NULL ,
  34. `smtpUsername` VARCHAR(255) NULL ,
  35. `smtpPassword` VARCHAR(255) NULL ,
  36. `smtpSender` VARCHAR(255) NULL DEFAULT 'jobby@localhost' ,
  37. `smtpSenderName` VARCHAR(255) NULL DEFAULT 'Jobby' ,
  38. `smtpSecurity` VARCHAR(20) NULL ,
  39. `runAs` VARCHAR(255) NULL ,
  40. `environment` TEXT NULL ,
  41. `runOnHost` VARCHAR(255) NULL ,
  42. `output` VARCHAR(255) NULL ,
  43. `dateFormat` VARCHAR(100) NULL DEFAULT 'Y-m-d H:i:s' ,
  44. `enabled` BOOLEAN NULL DEFAULT TRUE ,
  45. `haltDir` VARCHAR(255) NULL , `debug` BOOLEAN NULL DEFAULT FALSE ,
  46. PRIMARY KEY (`name`)
  47. )
  48. ");
  49. $insertCronJobConfiguration = $dbh->prepare("
  50. INSERT INTO `$dbhJobbiesTableName`
  51. (`name`,`command`,`schedule`,`output`)
  52. VALUES
  53. (:name,:command,:schedule,:output)
  54. ");
  55. // First demo-job - print "date" to logs/command-pdo.log.
  56. $insertCronJobConfiguration->execute(
  57. ['CommandExample', 'date', '* * * * *', 'logs/command-pdo.log']
  58. );
  59. // Second demo-job - a Closure which does some php::echo(). The Closure is saved to PDO-backend, too.
  60. $secondJobFn = function() {
  61. echo "I'm a function (" . date('Y-m-d H:i:s') . ')!' . PHP_EOL;
  62. return true;
  63. };
  64. $serializer = new SuperClosure\Serializer();
  65. $secondJobFnSerialized = $serializer->serialize($secondJobFn);
  66. $insertCronJobConfiguration->execute(
  67. ['ClosureExample', $secondJobFnSerialized, '* * * * *', 'logs/closure-pdo.log']
  68. );
  69. /*
  70. * Examples are now set up, and saved to PDO-backend.
  71. *
  72. * Now, fetch all jobbies from PDO-backend and run them.
  73. */
  74. $jobbiesStmt = $dbh->query("SELECT * FROM `$dbhJobbiesTableName`");
  75. $jobbies = $jobbiesStmt->fetchAll(PDO::FETCH_ASSOC);
  76. $jobby = new \Jobby\Jobby();
  77. foreach ($jobbies as $job) {
  78. // Filter out each value, which is not set (for example, "maxRuntime" is not defined in the job).
  79. $job = array_filter($job);
  80. try {
  81. $job['closure'] = $serializer->unserialize($job['command']);
  82. unset($job['command']);
  83. } catch (SuperClosure\Exception\ClosureUnserializationException $e) {
  84. }
  85. $jobName = $job['name'];
  86. unset($job['name']);
  87. $jobby->add($jobName, $job);
  88. }
  89. $jobby->run();