-
Notifications
You must be signed in to change notification settings - Fork 16
/
file_updates_task.php
211 lines (179 loc) · 6.97 KB
/
file_updates_task.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* File updates task.
*
* @package tool_ally
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_ally\task;
use core\task\scheduled_task;
use tool_ally\local_file;
use tool_ally\push_config;
use tool_ally\push_file_updates;
defined('MOODLE_INTERNAL') || die();
/**
* File updates task.
*
* @package tool_ally
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class file_updates_task extends scheduled_task {
/**
* @var push_config
*/
public $config;
/**
* @var push_file_updates
*/
public $updates;
/**
* @var bool
*/
private $clionly;
public function get_name() {
return get_string('fileupdatestask', 'tool_ally');
}
public function execute() {
$config = $this->config ?: new push_config();
if (!$config->is_valid()) {
return;
}
$time = clean_param(get_config('tool_ally', 'push_timestamp'), PARAM_INT);
if (empty($time)) {
// First time running or reset. Since this pushes file updates and this is first time, then we have no update
// window. So, set time and wait till next task execution.
$this->set_push_timestamp(time());
return;
}
$updates = $this->updates ?: new push_file_updates($config);
$this->clionly = $config->is_cli_only();
// Push file updates.
$pushupdatesok = $this->push_updates($config, $updates, $time);
if (!$pushupdatesok) {
return;
}
// Push deleted files.
$this->push_deletes($config, $updates);
}
/**
* Push file updates to Ally.
*
* @param push_config $config
* @param push_file_updates $updates
* @param int $time Push updates for files that have been modified after this time
* @throws \Exception
* @return bool
*/
private function push_updates(push_config $config, push_file_updates $updates, $time) {
global $CFG;
$files = local_file::iterator()->since($time)->sort_by('timemodified');
$payload = [];
$timetosave = 0;
try {
$files->rewind();
while ($files->valid()) {
$file = $files->current();
$files->next();
$payload[] = local_file::to_crud($file);
// Check to see if we have our batch size or if we are at the last file.
if (count($payload) >= $config->get_batch_size() || !$files->valid()) {
$sendsuccess = $updates->send($payload);
if (!$sendsuccess) {
// Failed to send, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Give up at this point.
// Time stamp is set to last successful batches final file time modified.
$this->set_push_timestamp($timetosave);
return false;
}
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}
// Reset payload and track last successful and latest time modified.
$payload = [];
$timetosave = $file->get_timemodified();
}
}
} catch (\Exception $e) {
// Save current progress so we don't resend files that were successfully sent.
$this->set_push_timestamp($timetosave);
throw $e;
}
// Everything went according to plan, update our timestamp.
$this->set_push_timestamp($timetosave);
return true;
}
/**
* Push file deletions to Ally.
*
* @param push_config $config
* @param push_file_updates $updates
*/
private function push_deletes(push_config $config, push_file_updates $updates) {
global $DB, $CFG;
$ids = [];
$payload = [];
$deletes = $DB->get_recordset('tool_ally_deleted_files', null, 'id');
while ($deletes->valid()) {
$file = $deletes->current();
$deletes->next();
$ids[] = $file->id;
$payload[] = local_file::to_crud($file);
// Check to see if we have our batch size or if we are at the last file.
if (count($payload) >= $config->get_batch_size() || !$deletes->valid()) {
$sendsuccess = $updates->send($payload);
if (!$sendsuccess) {
// Failed to send, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Give up at this point.
return false;
}
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}
// Successfully sent, remove.
$DB->delete_records_list('tool_ally_deleted_files', 'id', $ids);
// Reset arrays for next payload.
$ids = [];
$payload = [];
}
}
$deletes->close();
}
/**
* Save push timestamp. This is our file last modified window.
*
* @param int $timestamp
*/
private function set_push_timestamp($timestamp) {
if (!empty($timestamp)) {
set_config('push_timestamp', $timestamp, 'tool_ally');
}
}
}