Skip to content

Commit

Permalink
2.29.0
Browse files Browse the repository at this point in the history
  • Loading branch information
scottjpearson committed Jan 25, 2021
1 parent 55a99b2 commit c942d33
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 26 deletions.
7 changes: 5 additions & 2 deletions CareerDev.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CareerDev {
public static $passedModule = NULL;

public static function getVersion() {
return "2.28.1";
return "2.29.0";
}

public static function getLockFile($pid) {
Expand Down Expand Up @@ -99,7 +99,9 @@ public static function log($mssg, $pid = FALSE) {
if ($pid) {
$params = array("project_id" => $pid);
$module->log($mssg, $params);
// error_log($pid.": ".$mssg);
if (self::isVanderbilt()) {
error_log($pid.": ".$mssg);
}
} else {
error_log($mssg);
}
Expand Down Expand Up @@ -585,6 +587,7 @@ public static function getMenu($menuName) {
];
$ary["Configure an Email"] = self::link("/emailMgmt/configure.php");
$ary["View Email Log"] = self::link("/emailMgmt/log.php");
$ary["View Email Queue"] = self::link("/emailMgmt/viewQueue.php");
$ary["List of Nonrespondents"] = self::link("/emailMgmt/noSurvey.php");
$ary["Survey Responses"] = self::link("/surveyResponses.php");
$ary["Import General Data"] = self::link("/import.php");
Expand Down
2 changes: 1 addition & 1 deletion FlightTrackerExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ function cron() {
} else {
loadCrons($mgr, FALSE, $token, $server);
}
CareerDev::log($this->getName().": Running crons for pid $pid", $pid);
CareerDev::log($this->getName().": Running ".$mgr->getNumberOfCrons()." crons for pid $pid", $pid);
$addlEmailText = in_array($pid, $pidsUpdated) ? "Surveys shared from other Flight Tracker projects" : "";
$mgr->run($adminEmail, $tokenName, $addlEmailText);
CareerDev::log($this->getName().": cron run complete for pid $pid", $pid);
Expand Down
55 changes: 41 additions & 14 deletions classes/Crons.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public function __construct($token, $server, $pid) {
$this->server = $server;
$this->pid = $pid;

$this->crons = array();
$this->crons = [];
$days = self::getDaysOfWeek();
foreach ($days as $day) {
$this->crons[$day] = array();
$this->crons[$day] = [];
}

$this->adminEmail = Application::getSetting("admin_email", $pid);
Expand All @@ -26,7 +26,8 @@ public function __construct($token, $server, $pid) {

# file is relative to career_dev's root
# dayOfWeek is in string format - "Monday", "Tuesday", etc. or a date in form Y-M-D
public function addCron($file, $method, $dayOfWeek) {
# records here, if specified, overrides the records specified in function run
public function addCron($file, $method, $dayOfWeek, $records = []) {
$possibleDays = self::getDaysOfWeek();
$dateTs = strtotime($dayOfWeek);
if (!in_array($dayOfWeek, $possibleDays) && !$dateTs) {
Expand All @@ -39,17 +40,29 @@ public function addCron($file, $method, $dayOfWeek) {
}

$cronjob = new CronJob($absFile, $method);
if (!empty($records)) {
$cronjob->setRecords($records);
}
// Application::log("Has day of week $dayOfWeek and timestamp for ".date("Y-m-d", $dateTs));
if (in_array($dayOfWeek, $possibleDays)) {
# Weekday
array_push($this->crons[$dayOfWeek], $cronjob);
if (!isset($this->crons[$dayOfWeek])) {
$this->crons[$dayOfWeek] = [];
// Application::log("Reset cron list for $dayOfWeek");
}
$this->crons[$dayOfWeek][] = $cronjob;
// Application::log("Assigned cron for $dayOfWeek");
} else if ($dateTs) {
# Y-M-D
$date = date(self::getDateFormat(), $dateTs);
if (!isset($this->crons[$date])) {
$this->crons[$date] = array();
$this->crons[$date] = [];
// Application::log("Reset cron list for $date");
}
array_push($this->crons[$date], $cronjob);
$this->crons[$date][] = $cronjob;
// Application::log("Assigned cron for $date");
}
// Application::log("Added cron $method: ".$this->getNumberOfCrons()." total crons now");
}

private static function getDaysOfWeek() {
Expand Down Expand Up @@ -95,31 +108,36 @@ public static function reportCronErrors() {
}
}

public function run($adminEmail = "", $tokenName = "", $additionalEmailText = "", $records = []) {
public function run($adminEmail = "", $tokenName = "", $additionalEmailText = "", $recordsToRun = []) {
$dayOfWeek = date("l");
$date = date(self::getDateFormat());
$keys = array($date, $dayOfWeek); // in order that they will run

Application::log("CRONS RUN AT ".date("Y-m-d h:i:s")." FOR PID ".$this->pid);
Application::log("adminEmail ".$adminEmail);
$run = array();
$toRun = array();
Application::log("Looking in ".$this->getNumberOfCrons()." cron jobs");
$run = [];
$toRun = [];
foreach ($keys as $key) {
if (isset($this->crons[$key])) {
foreach ($this->crons[$key] as $cronjob) {
array_push($toRun, $cronjob);
$toRun[] = $cronjob;
}
}
}

if (empty($records)) {
$records = Download::recordIds($this->token, $this->server);
}

register_shutdown_function(["CronManager", "reportCronErrors"]);

Application::log("Running ".count($toRun)." crons for pid ".$this->pid." with keys ".json_encode($keys));
foreach ($toRun as $cronjob) {
$records = $cronjob->getRecords();
if (empty($records)) {
if (empty($recordsToRun)) {
$records = Download::recordIds($this->token, $this->server);
} else {
$records = $recordsToRun;
}
}
Application::log("Running ".$cronjob->getTitle());
$run[$cronjob->getTitle()] = array("text" => "Attempted", "ts" => self::getTimestamp());
try {
Expand Down Expand Up @@ -213,6 +231,15 @@ public function run($passedToken, $passedServer, $passedPid, $records) {
}
}

public function setRecords($records) {
$this->records = $records;
}

public function getRecords() {
return $this->records;
}

private $file = "";
private $method = "";
private $records = [];
}
29 changes: 29 additions & 0 deletions classes/EmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ private function transformToTS($datetime) {
return $ts;
}

public function getQueue($thresholdTs) {
$rows = [];
foreach ($this->data as $name => $emailSetting) {
foreach ($emailSetting["when"] as $whenType => $datetime) {
$ts = self::transformToTS($datetime);
if ($ts >= $thresholdTs) {
$processedRows = $this->getRows($emailSetting["who"], $whenType, $this->getForms($emailSetting["what"]));
$emailsByRecord = self::processEmails($processedRows);
$emails = [];
foreach ($emailsByRecord as $recordId => $email) {
if ($email) {
$emails[] = Links::makeRecordHomeLink($this->pid, $recordId, "Record $recordId").": $email";
}
}

$row = [];
$row['name'] = $name;
list($row['date'], $row['time']) = explode(" ", $datetime);
$row['from'] = $emailSetting['what']['from'];
$row['subject'] = self::getSubject($emailSetting["what"]);;
$row['to'] = $emails;
$row['to_count'] = count($emails);
$rows[] = $row;
}
}
}
return $rows;
}

private function enqueueRelevantEmails($to = "", $names = array(), $func = "sendEmail", $currTime = FALSE) {
if (!is_array($names)) {
$names = array($names);
Expand Down
2 changes: 1 addition & 1 deletion classes/Grants.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getGrants($type = "compiled") {
if ($type == "all_pis") {
$grants = $this->dedupedGrants;
$filteredGrants = [];
$piRoles = ["Co-PI", "PI", "Principal Investigator", "Admin", "PI/Co-PI", "Post-doctoral Trainee"];
$piRoles = ["Co-PI", "PI", "Principal Investigator", "Admin", "PI/Co-PI"]; // exclude: "Post-Doctoral Trainee"
foreach ($grants as $grant) {
if (in_array($grant->getVariable("role"), $piRoles)) {
$filteredGrants[] = $grant;
Expand Down
2 changes: 1 addition & 1 deletion classes/REDCapManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public static function getMaxInstanceForEvent($rows, $eventName, $recordId) {

public static function makeHTMLId($id) {
$htmlFriendly = preg_replace("/[\s\-]+/", "_", $id);
$htmlFriendly = preg_replace("/[\"'#<>\~\`\!\@\#\$\%\^\&\*\(\)\=\;]/", "", $htmlFriendly);
$htmlFriendly = preg_replace("/[\+\"\/\[\]'#<>\~\`\!\@\#\$\%\^\&\*\(\)\=\;]/", "", $htmlFriendly);
return $htmlFriendly;
}

Expand Down
21 changes: 19 additions & 2 deletions cronLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vanderbilt\FlightTrackerExternalModule;

use Vanderbilt\CareerDevLibrary\Application;
use \Vanderbilt\CareerDevLibrary\Download;
use \Vanderbilt\CareerDevLibrary\CronManager;

Expand Down Expand Up @@ -44,8 +45,14 @@ function loadCrons(&$manager, $specialOnly = FALSE, $token = "", $server = "") {
$manager->addCron("drivers/13_pullOrcid.php", "pullORCIDs", "Friday");
$manager->addCron("publications/getAllPubs_func.php", "getPubs", "Saturday");

if (date("d") == "18") {
$manager->addCron("publications/updateBibliometrics.php", "updateBibliometrics", date("Y-m-d"));
# limited group because bibliometric updates take a lot of time due to rate limiters
$bibliometricRecordsToUpdate = getRecordsToUpdateBibliometrics($token, $server, date("d"), date("t"));
if (!empty($bibliometricRecordsToUpdate)) {
if ((Application::getPid($token) == "112169") && CareerDev::isVanderbilt() && (date("Y-m-d") == "2021-01-23")) {
$manager->addCron("publications/updateBibliometrics.php", "updateBibliometrics", date("Y-m-d"));
} else {
$manager->addCron("publications/updateBibliometrics.php", "updateBibliometrics", date("Y-m-d"), $bibliometricRecordsToUpdate);
}
}
$manager->addCron("drivers/6d_makeSummary.php", "makeSummary", "Monday");
$manager->addCron("drivers/6d_makeSummary.php", "makeSummary", "Tuesday");
Expand Down Expand Up @@ -128,3 +135,13 @@ function checkMetadataForFields($token, $server) {
return $vars;
}

function getRecordsToUpdateBibliometrics($token, $server, $dayOfMonth, $daysInMonth) {
$records = Download::recordIds($token, $server);
$recordsToRun = [];
foreach ($records as $recordId) {
if (($recordId - 1) % $daysInMonth == $dayOfMonth - 1) {
$recordsToRun[] = $recordId;
}
}
return $recordsToRun;
}
Loading

0 comments on commit c942d33

Please sign in to comment.