-
Notifications
You must be signed in to change notification settings - Fork 4
Scheduler #270
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
base: main-orig
Are you sure you want to change the base?
Scheduler #270
Changes from all commits
3503964
13d8941
31be9be
e73f130
c663de3
d9a4991
b64b06c
7333016
2ebeba3
2bef711
731ce78
63770c9
e0739b5
9001dcb
fcd1a65
2c197c1
112343e
b0b5ef9
80b4f8a
c24ab96
5d821e1
b21d690
66ef257
bd0b034
a684672
6aeffa4
2050d08
d0d22d5
e0053ae
82fb25d
6f427a7
3c2fb5a
16a7c55
8e02de6
36369ab
e9dbdb9
6770f92
1e87dc5
e4c691b
5ce400d
15f3992
21782cc
15667dd
40666f8
f50e24b
8dd273e
806f654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| DROP TABLE IF EXISTS `scheduler`; | ||
| CREATE TABLE `scheduler` ( | ||
| `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, | ||
| `name` varchar(50) DEFAULT NULL, | ||
| `minute` varchar(2) NOT NULL DEFAULT '*', -- 0-59 | ||
| `hour` varchar(2) NOT NULL DEFAULT '*', -- 0-23 | ||
| `dayofmonth` varchar(2) NOT NULL DEFAULT '*', -- 1-31 | ||
| `month` varchar(2) NOT NULL DEFAULT '*', -- 1-12 | ||
| `dayofweek` varchar(1) NOT NULL DEFAULT '*', -- 0-7 (0 or 7 is Sunday, or use names) | ||
| `runnow` tinyint(1) NOT NULL DEFAULT '0', | ||
| `active` tinyint(1) NOT NULL DEFAULT '0', | ||
| `lastrun` int(11) DEFAULT NULL, | ||
| PRIMARY KEY (`ID`) | ||
| ) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=latin1; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| include("../../header.php"); | ||
| include("../../includes/classes/scheduler.php"); | ||
|
|
||
| //Permissions Access | ||
| if(!mfcsPerms::evaluatePageAccess(3)){ | ||
| header('Location: /index.php?permissionFalse'); | ||
| } | ||
|
|
||
| $tableName = "scheduler"; | ||
| $scheduler = new scheduler("../../crons"); | ||
|
|
||
| include("../../includes/formDefinitions/form_scheduler.php"); | ||
|
|
||
| if (isset($engine->cleanPost['MYSQL'][$tableName."_submit"])) { | ||
|
|
||
| log::insert("Admin: Insert New Cron Job"); | ||
|
|
||
| $list = defineList($scheduler, $tableName); | ||
| $list->insert(); | ||
| } | ||
|
|
||
| if (isset($engine->cleanPost['MYSQL'][$tableName."_update"])) { | ||
|
|
||
| log::insert("Admin: Update Cron Job"); | ||
|
|
||
| $list = defineList($scheduler, $tableName); | ||
| $list->update(); | ||
| } | ||
|
|
||
| $list = defineList($scheduler, $tableName); | ||
|
|
||
| localVars::add("results",displayMessages()); | ||
|
|
||
| log::insert("Admin: View Scheduler Page"); | ||
|
|
||
| $engine->eTemplate("include","header"); | ||
| ?> | ||
|
|
||
| <section> | ||
| <header class="page-header"> | ||
| <h1>Manage Cron Jobs</h1> | ||
| </header> | ||
|
|
||
| <ul class="breadcrumbs"> | ||
| <li><a href="{local var="siteRoot"}">Home</a></li> | ||
| <li><a href="{local var="siteRoot"}/admin/">Admin</a></li> | ||
| <li class="pull-right noDivider"><a href="https://github.com/wvulibraries/mfcs/wiki/Scheduler" target="_blank"> <span class="fa fa-book"></span> Documentation</a></li> | ||
| </ul> | ||
|
|
||
| {local var="results"} | ||
|
|
||
| <section> | ||
| <header> | ||
| <h2>Add New Job</h2> | ||
| </header> | ||
| {listObject display="insertForm"} | ||
| </section> | ||
|
|
||
| <hr /> | ||
|
|
||
| <section> | ||
| <header> | ||
| <h2>Edit Scheduled Jobs</h2> | ||
| </header> | ||
| <div class="table-responsive editUsersTable"> | ||
| {listObject display="editTable"} | ||
| </div> | ||
| </section> | ||
|
|
||
| </section> | ||
|
|
||
| <?php | ||
| $engine->eTemplate("include","footer"); | ||
| ?> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| // this file should be scheduled to run every 5 minutes. | ||
|
|
||
| session_save_path('/tmp'); | ||
| ini_set('memory_limit',-1); | ||
| set_time_limit(0); | ||
|
|
||
| require("../header.php"); | ||
| include("../includes/classes/scheduler.php"); | ||
|
|
||
| $scheduler = new scheduler("."); | ||
|
|
||
| if (!isCLI()) { | ||
| print "Must be run from the command line."; | ||
| exit; | ||
| } | ||
|
|
||
| // Turn off EngineAPI template engine | ||
| $engine->obCallback = FALSE; | ||
|
|
||
| $sql = sprintf("SELECT * FROM `scheduler` WHERE `active` = 1 "); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this being sprintf? Your not formatting anything and your not putting any new statements in. A single string should suffice.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed sprintf. |
||
| $sqlResult = $engine->openDB->query($sql); | ||
|
|
||
| if (!$sqlResult['result']) { | ||
| errorHandle::newError(__METHOD__."() - : ".$sqlResult['error'], errorHandle::DEBUG); | ||
| return FALSE; | ||
| } | ||
|
|
||
| if ($sqlResult['numrows'] == "0") die(date("Y-m-d h:i:sa") . " Nothing to do.\n"); | ||
|
|
||
| while($row = mysql_fetch_array($sqlResult['result'], MYSQL_ASSOC)) { | ||
|
|
||
| // verify scheduled job exists | ||
| if (!file_exists($row['name'])) { | ||
| notification::notifyAdmins("MFCS Scheduler Run Failure", $row['name'] , "Scheduled Cron Job is missing"); | ||
| } | ||
| elseif ($row['active'] == '1') { | ||
| if ($row['runnow'] == '1') { | ||
| $scheduler->runjob($row); | ||
| print date("Y-m-d h:i:sa") . " Job " . $row['name'] . "\n"; | ||
| } | ||
| elseif ($scheduler->timetorun($row)) { | ||
| $scheduler->runjob($row); | ||
| print date("Y-m-d h:i:sa") . " Job " . $row['name'] . "\n"; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| class scheduler { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are none of the SQL things namespaced in the scheduler class. Even though they may be done using the command line the classes are still available. |
||
|
|
||
| private $availablecrons = array(); | ||
|
|
||
| public function __construct($dir) { | ||
|
|
||
| if (!is_readable($dir)) { | ||
| return FALSE; | ||
| } | ||
|
|
||
| $dir = opendir($dir); // open the cwd..also do an err check. | ||
| while(false != ($file = readdir($dir))) { | ||
| if(($file != ".") && ($file != "..") && ($file != "runcrons.php")) { | ||
| $this->availablecrons[] = array('value' => $file, 'label' => str_replace('_',' ',basename($file,'.php'))); | ||
| } | ||
| } | ||
|
|
||
| closedir($dir); | ||
|
|
||
| } | ||
|
|
||
| public function getCronsArray() { | ||
| return $this->availablecrons; | ||
| } | ||
|
|
||
| public function createSelect($start, $end) { | ||
| $selectarray = array(); | ||
| $selectarray[] = array('value' => '*', 'label' => '*', 'selected' => TRUE); | ||
| for ($currentpos = $start; $currentpos <= $end; $currentpos++) { | ||
| $selectarray[] = array('value' => (string) $currentpos, 'label' => (string) $currentpos); | ||
| } | ||
|
|
||
| return $selectarray; | ||
|
|
||
| } | ||
|
|
||
| public function minuteSelect() { | ||
| $minutearray = array(); | ||
| $minutearray[] = array('value' => '*', 'label' => '*', 'selected' => TRUE); | ||
| $minutearray[] = array('value' => '0', 'label' => '0'); | ||
| $minutearray[] = array('value' => '15', 'label' => '15'); | ||
| $minutearray[] = array('value' => '30', 'label' => '30'); | ||
| $minutearray[] = array('value' => '45', 'label' => '45'); | ||
|
|
||
| return $minutearray; | ||
|
|
||
| } | ||
|
|
||
| public function monthSelect() { | ||
| $month_names = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); | ||
| $montharray = array(); | ||
| $montharray[] = array('value' => '*', 'label' => '*', 'selected' => TRUE); | ||
| for ($currentpos = 1; $currentpos <= 12; $currentpos++) { | ||
| $montharray[] = array('value' => (string) $currentpos, 'label' => $month_names[$currentpos-1]); | ||
| } | ||
|
|
||
| return $montharray; | ||
|
|
||
| } | ||
|
|
||
| public function weekdaySelect() { | ||
| $weekday_names = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); | ||
| $daysarray = array(); | ||
| $daysarray[] = array('value' => '*', 'label' => '*', 'selected' => TRUE); | ||
| for ($currentpos = 0; $currentpos <= 6; $currentpos++) { | ||
| $daysarray[] = array('value' => (string) $currentpos, 'label' => $weekday_names[$currentpos]); | ||
| } | ||
|
|
||
| return $daysarray; | ||
|
|
||
| } | ||
|
|
||
| public function timetorun($job){ | ||
| $minuteset = ( ($job['minute'] == date("i")) || ($job['minute'] == '*') ); | ||
| $hourset = ( ($job['hour'] == date("G")) || ($job['hour'] == '*') ); | ||
| $dayofmonthset = ( ($job['dayofmonth'] == date("n")) || ($job['dayofmonth'] == '*') ); | ||
| $monthset = ( ($job['month'] == date("n")) || ($job['month'] == '*') ); | ||
| $dayofweekset = ( ($job['dayofweek'] == date("w")) || ($job['dayofweek'] == '*') ); | ||
| return ($minuteset && $hourset && $dayofmonthset && $monthset && $dayofweekset); | ||
| } | ||
|
|
||
| public function runjob($job){ | ||
| shell_exec("/usr/bin/php " . $job['name']); | ||
|
|
||
| // update last run | ||
| $sql = sprintf("UPDATE `scheduler` set `runnow`='%s', `lastrun`='%s' WHERE `ID`='%s' LIMIT 1", | ||
| 0, | ||
| time(), | ||
| $job['ID'] | ||
| ); | ||
| $sqlResult_insert = mfcs::$engine->openDB->query($sql); | ||
|
|
||
| if (!$sqlResult_insert['result']) { | ||
| notification::notifyAdmins("MFCS Database Update Failure", "Failed to set runnow to 0 and lastrun to current time", $job['name']); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this logic be redacted to an include file so that it is not intermixed with the view? If not then it is fine, but thinking about maintainability and future thought we want to keep logic separate from views as much as possible.