-
Notifications
You must be signed in to change notification settings - Fork 1
/
DashboardAnalysisPlatformExternalModule.php
127 lines (115 loc) · 5.01 KB
/
DashboardAnalysisPlatformExternalModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Vanderbilt\DashboardAnalysisPlatformExternalModule;
use Exception;
use REDCap;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
class DashboardAnalysisPlatformExternalModule extends AbstractExternalModule
{
/**
* This cron will only perform actions once a day between the hours
* of midnight and 6am, regardless of how often it is scheduled.
* Scheduling it more often is required to ensure it runs at a predictable time.
* For example, scheduling it for once every 10 minutes will guarantee that
* it runs at the first available minute after 12:10am,
* though it may run as soon as 12am.
* This produces behavior similar to timed crons.
* See the API Sync module's per project sync settings for a more advanced
* example of this same concept.
*/
function dashboardCacheCron($cronAttributes){
$hourRange = 6;
if(date('G') > $hourRange){
// Only perform actions between 12am and 6am.
return;
}
$thisCron = $cronAttributes['cron_name'];
## Old way of checking cron status, check to prevent re-sending on upgrade
$lastRunSettingName = 'last-cron-run-time';
$lastRun = (int)$this->getSystemSetting($lastRunSettingName);
$hoursSinceLastRun = (time()-$lastRun)/60/60;
if($hoursSinceLastRun < $hourRange){
// We're already run recently
return;
}
$lastRunCronSettingName = 'last-cron-run-time-'.$thisCron;
$lastRunThisCron = (int)$this->getSystemSetting($lastRunCronSettingName);
$hoursSinceLastRun = (time()-$lastRunThisCron)/60/60;
if($hoursSinceLastRun < $hourRange){
// We're already run this cron recently
return;
}
## Immediately log starting in case a second process spawns for this cron
$this->setSystemSetting($lastRunCronSettingName, time());
// Perform cron actions here
foreach ($this->getProjectsWithModuleEnabled() as $project_id){
try {
$stop_cron = $this->getProjectSetting('stop-cron',$project_id);
if(!$stop_cron) {
#CRONS
$filename = "dashboard_cache_file_" . $project_id . ".txt";
$q = $this->query("SELECT em.stored_name FROM redcap_edocs_metadata em
LEFT JOIN redcap_docs_to_edocs de ON em.doc_id = de.doc_id
LEFT JOIN redcap_docs rd ON rd.docs_id = de.docs_id
WHERE rd.project_id=? AND rd.docs_name=?",
[$project_id, $filename]);
$found = false;
while ($row = db_fetch_assoc($q)) {
$storedName = $row['stored_name'];
$today = strtotime(date("Ymd"));
$file_date = strtotime(date("Ymd", strtotime(explode("_pid" . $project_id . "_", $storedName)[0])));
#We make sure we only do this once a day
if ($today > $file_date) {
$found = true;
error_log("dashboardCacheFile PID " . $project_id . ", " . $cronAttributes['cron_name']);
include("callCron.php");
}
}
if (!$found) {
error_log("dashboardCacheFile PID " . $project_id . ", " . $cronAttributes['cron_name']);
include("callCron.php");
}
}
} catch (Throwable $e) {
\REDCap::email('[email protected]', '[email protected]',"Cron Error", $e->getMessage());
}
}
}
public function redcap_module_link_check_display($project_id, $link) {
$privacy = $this->getProjectSetting('privacy',$project_id);
if($privacy == "public"){
$link['url'] = $this->getUrl("index.php"). "&NOAUTH";
// return parent::redcap_module_link_check_display($project_id,$link);
}
#Let users always see the link/page if they are added into the project
return true;
}
/**
* Function to increase the Maximum Execution Time
* @param $hours
*/
public function increaseProcessingMax($hours) {
require_once(APP_PATH_DOCROOT."Classes/System.php");
\System::increaseMaxExecTime($hours * 3600);
}
public function validateS3Url($url){
$parts = parse_url($url);
#TODO match the URLs prefixes in the array with the framework function once available
foreach([".s3.amazonaws.com"] as $suffix){
if(ends_with($parts['host'], $suffix)){
return $url;
}
}
throw new \Exception('Only certain domains are allowed');
}
public function loadREDCapJS(){
if (method_exists(get_parent_class($this), 'loadREDCapJS')) {
parent::loadREDCapJS();
} else {
?>
<script src='<?=APP_PATH_WEBROOT?>Resources/webpack/js/bundle.js'></script>
<?php
}
}
}
?>