@@ -22,12 +22,12 @@ |
||
| 22 | 22 | class Migration0 implements MigrationInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritDoc} |
|
| 27 | - * @see MigrationInterface::upgrade() |
|
| 28 | - */ |
|
| 29 | - public function upgrade(): void |
|
| 30 | - { |
|
| 31 | - // These migrations have been merged into migration 1. |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritDoc} |
|
| 27 | + * @see MigrationInterface::upgrade() |
|
| 28 | + */ |
|
| 29 | + public function upgrade(): void |
|
| 30 | + { |
|
| 31 | + // These migrations have been merged into migration 1. |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -36,261 +36,261 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class TaskScheduleService |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * Time-out after which the task will be considered not running any more. |
|
| 41 | - * In seconds, default 5 mins. |
|
| 42 | - * @var integer |
|
| 43 | - */ |
|
| 44 | - public const TASK_TIME_OUT = 600; |
|
| 45 | - |
|
| 46 | - private ModuleService $module_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Constructor for TaskScheduleService |
|
| 50 | - * |
|
| 51 | - * @param ModuleService $module_service |
|
| 52 | - */ |
|
| 53 | - public function __construct(ModuleService $module_service) |
|
| 54 | - { |
|
| 55 | - $this->module_service = $module_service; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Returns all Tasks schedules in database. |
|
| 60 | - * Stored records can be synchronised with the tasks actually available to the system. |
|
| 61 | - * |
|
| 62 | - * @param bool $sync_available Should tasks synchronised with available ones |
|
| 63 | - * @param bool $include_disabled Should disabled tasks be returned |
|
| 64 | - * @return Collection<TaskSchedule> Collection of TaskSchedule |
|
| 65 | - */ |
|
| 66 | - public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
| 67 | - { |
|
| 68 | - $tasks_schedules = DB::table('maj_admintasks') |
|
| 69 | - ->select() |
|
| 70 | - ->get() |
|
| 71 | - ->map(self::rowMapper()); |
|
| 72 | - |
|
| 73 | - if ($sync_available) { |
|
| 74 | - $available_tasks = clone $this->available(); |
|
| 75 | - foreach ($tasks_schedules as $task_schedule) { |
|
| 76 | - /** @var TaskSchedule $task_schedule */ |
|
| 77 | - if ($available_tasks->has($task_schedule->taskId())) { |
|
| 78 | - $available_tasks->forget($task_schedule->taskId()); |
|
| 79 | - } else { |
|
| 80 | - $this->delete($task_schedule); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - foreach ($available_tasks as $task_name => $task) { |
|
| 85 | - /** @var TaskInterface $task */ |
|
| 86 | - $this->insertTask($task_name, $task->defaultFrequency()); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return $this->all(false, $include_disabled); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return $tasks_schedules; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
| 97 | - * |
|
| 98 | - * @return Collection<array<string, string>> |
|
| 99 | - */ |
|
| 100 | - public function available(): Collection |
|
| 101 | - { |
|
| 102 | - return Registry::cache()->array()->remember('maj-available-admintasks', function () { |
|
| 103 | - return $this->module_service |
|
| 104 | - ->findByInterface(ModuleTasksProviderInterface::class) |
|
| 105 | - ->flatMap(fn(ModuleTasksProviderInterface $module) => $module->listTasks()); |
|
| 106 | - }); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Find a task schedule by its ID. |
|
| 111 | - * |
|
| 112 | - * @param int $task_schedule_id |
|
| 113 | - * @return TaskSchedule|NULL |
|
| 114 | - */ |
|
| 115 | - public function find(int $task_schedule_id): ?TaskSchedule |
|
| 116 | - { |
|
| 117 | - return DB::table('maj_admintasks') |
|
| 118 | - ->select() |
|
| 119 | - ->where('majat_id', '=', $task_schedule_id) |
|
| 120 | - ->get() |
|
| 121 | - ->map(self::rowMapper()) |
|
| 122 | - ->first(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Add a new task schedule with the specified task ID, and frequency if defined. |
|
| 127 | - * Uses default for other settings. |
|
| 128 | - * |
|
| 129 | - * @param string $task_id |
|
| 130 | - * @param int $frequency |
|
| 131 | - * @return bool |
|
| 132 | - */ |
|
| 133 | - public function insertTask(string $task_id, int $frequency = 0): bool |
|
| 134 | - { |
|
| 135 | - $values = ['majat_task_id' => $task_id]; |
|
| 136 | - if ($frequency > 0) { |
|
| 137 | - $values['majat_frequency'] = $frequency; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return DB::table('maj_admintasks') |
|
| 141 | - ->insert($values); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Update a task schedule. |
|
| 146 | - * Returns the number of tasks schedules updated. |
|
| 147 | - * |
|
| 148 | - * @param TaskSchedule $task_schedule |
|
| 149 | - * @return int |
|
| 150 | - */ |
|
| 151 | - public function update(TaskSchedule $task_schedule): int |
|
| 152 | - { |
|
| 153 | - return DB::table('maj_admintasks') |
|
| 154 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 155 | - ->update([ |
|
| 156 | - 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
| 157 | - 'majat_last_run' => $task_schedule->lastRunTime(), |
|
| 158 | - 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
| 159 | - 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
| 160 | - 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
| 161 | - 'majat_running' => $task_schedule->isRunning() |
|
| 162 | - ]); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Delete a task schedule. |
|
| 167 | - * |
|
| 168 | - * @param TaskSchedule $task_schedule |
|
| 169 | - * @return int |
|
| 170 | - */ |
|
| 171 | - public function delete(TaskSchedule $task_schedule): int |
|
| 172 | - { |
|
| 173 | - return DB::table('maj_admintasks') |
|
| 174 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 175 | - ->delete(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Find a task by its name |
|
| 180 | - * |
|
| 181 | - * @param string $task_id |
|
| 182 | - * @return TaskInterface|NULL |
|
| 183 | - */ |
|
| 184 | - public function findTask(string $task_id): ?TaskInterface |
|
| 185 | - { |
|
| 186 | - if ($this->available()->has($task_id)) { |
|
| 187 | - return app($this->available()->get($task_id)); |
|
| 188 | - } |
|
| 189 | - return null; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Retrieve all tasks that are candidates to be run. |
|
| 194 | - * |
|
| 195 | - * @param bool $force Should the run be forced |
|
| 196 | - * @param string $task_id Specific task ID to be run |
|
| 197 | - * @return Collection<TaskSchedule> |
|
| 198 | - */ |
|
| 199 | - public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
| 200 | - { |
|
| 201 | - $query = DB::table('maj_admintasks') |
|
| 202 | - ->select() |
|
| 203 | - ->where('majat_status', '=', 'enabled') |
|
| 204 | - ->where(function (Builder $query): void { |
|
| 205 | - |
|
| 206 | - $query->where('majat_running', '=', 0) |
|
| 207 | - ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
| 208 | - }); |
|
| 209 | - |
|
| 210 | - if (!$force) { |
|
| 211 | - $query->where(function (Builder $query): void { |
|
| 212 | - |
|
| 213 | - $query->where('majat_running', '=', 0) |
|
| 214 | - ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
| 215 | - }); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if ($task_id !== null) { |
|
| 219 | - $query->where('majat_task_id', '=', $task_id); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $query->get()->map(self::rowMapper()); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Run the task associated with the schedule. |
|
| 227 | - * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
| 228 | - * The last run time is recorded only if the task is successful. |
|
| 229 | - * |
|
| 230 | - * @param TaskSchedule $task_schedule |
|
| 231 | - * @param boolean $force |
|
| 232 | - */ |
|
| 233 | - public function run(TaskSchedule $task_schedule, $force = false): void |
|
| 234 | - { |
|
| 235 | - $task_schedule = DB::table('maj_admintasks') |
|
| 236 | - ->select() |
|
| 237 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 238 | - ->lockForUpdate() |
|
| 239 | - ->get() |
|
| 240 | - ->map(self::rowMapper()) |
|
| 241 | - ->first(); |
|
| 242 | - |
|
| 243 | - if ( |
|
| 244 | - !$task_schedule->isRunning() && |
|
| 245 | - ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
| 246 | - ) { |
|
| 247 | - $task_schedule->setLastResult(false); |
|
| 248 | - |
|
| 249 | - $task = $this->findTask($task_schedule->taskId()); |
|
| 250 | - if ($task !== null) { |
|
| 251 | - $task_schedule->startRunning(); |
|
| 252 | - $this->update($task_schedule); |
|
| 253 | - |
|
| 254 | - $first_error = $task_schedule->wasLastRunSuccess(); |
|
| 255 | - try { |
|
| 256 | - $task_schedule->setLastResult($task->run($task_schedule)); |
|
| 257 | - } catch (Throwable $ex) { |
|
| 258 | - if ($first_error) { // Only record the first error, as this could fill the log. |
|
| 259 | - Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
| 260 | - '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
| 261 | - . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - if ($task_schedule->wasLastRunSuccess()) { |
|
| 266 | - $task_schedule->setLastRunTime(Carbon::now()); |
|
| 267 | - $task_schedule->decrementRemainingOccurences(); |
|
| 268 | - } |
|
| 269 | - $task_schedule->stopRunning(); |
|
| 270 | - } |
|
| 271 | - $this->update($task_schedule); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Mapper to return a TaskSchedule object from an object. |
|
| 277 | - * |
|
| 278 | - * @return Closure(stdClass $row): TaskSchedule |
|
| 279 | - */ |
|
| 280 | - public static function rowMapper(): Closure |
|
| 281 | - { |
|
| 282 | - return static function (stdClass $row): TaskSchedule { |
|
| 283 | - |
|
| 284 | - return new TaskSchedule( |
|
| 285 | - (int) $row->majat_id, |
|
| 286 | - $row->majat_task_id, |
|
| 287 | - $row->majat_status === 'enabled', |
|
| 288 | - Carbon::parse($row->majat_last_run), |
|
| 289 | - (bool) $row->majat_last_result, |
|
| 290 | - CarbonInterval::minutes($row->majat_frequency), |
|
| 291 | - (int) $row->majat_nb_occur, |
|
| 292 | - (bool) $row->majat_running |
|
| 293 | - ); |
|
| 294 | - }; |
|
| 295 | - } |
|
| 39 | + /** |
|
| 40 | + * Time-out after which the task will be considered not running any more. |
|
| 41 | + * In seconds, default 5 mins. |
|
| 42 | + * @var integer |
|
| 43 | + */ |
|
| 44 | + public const TASK_TIME_OUT = 600; |
|
| 45 | + |
|
| 46 | + private ModuleService $module_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Constructor for TaskScheduleService |
|
| 50 | + * |
|
| 51 | + * @param ModuleService $module_service |
|
| 52 | + */ |
|
| 53 | + public function __construct(ModuleService $module_service) |
|
| 54 | + { |
|
| 55 | + $this->module_service = $module_service; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Returns all Tasks schedules in database. |
|
| 60 | + * Stored records can be synchronised with the tasks actually available to the system. |
|
| 61 | + * |
|
| 62 | + * @param bool $sync_available Should tasks synchronised with available ones |
|
| 63 | + * @param bool $include_disabled Should disabled tasks be returned |
|
| 64 | + * @return Collection<TaskSchedule> Collection of TaskSchedule |
|
| 65 | + */ |
|
| 66 | + public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
| 67 | + { |
|
| 68 | + $tasks_schedules = DB::table('maj_admintasks') |
|
| 69 | + ->select() |
|
| 70 | + ->get() |
|
| 71 | + ->map(self::rowMapper()); |
|
| 72 | + |
|
| 73 | + if ($sync_available) { |
|
| 74 | + $available_tasks = clone $this->available(); |
|
| 75 | + foreach ($tasks_schedules as $task_schedule) { |
|
| 76 | + /** @var TaskSchedule $task_schedule */ |
|
| 77 | + if ($available_tasks->has($task_schedule->taskId())) { |
|
| 78 | + $available_tasks->forget($task_schedule->taskId()); |
|
| 79 | + } else { |
|
| 80 | + $this->delete($task_schedule); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + foreach ($available_tasks as $task_name => $task) { |
|
| 85 | + /** @var TaskInterface $task */ |
|
| 86 | + $this->insertTask($task_name, $task->defaultFrequency()); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return $this->all(false, $include_disabled); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return $tasks_schedules; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
| 97 | + * |
|
| 98 | + * @return Collection<array<string, string>> |
|
| 99 | + */ |
|
| 100 | + public function available(): Collection |
|
| 101 | + { |
|
| 102 | + return Registry::cache()->array()->remember('maj-available-admintasks', function () { |
|
| 103 | + return $this->module_service |
|
| 104 | + ->findByInterface(ModuleTasksProviderInterface::class) |
|
| 105 | + ->flatMap(fn(ModuleTasksProviderInterface $module) => $module->listTasks()); |
|
| 106 | + }); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Find a task schedule by its ID. |
|
| 111 | + * |
|
| 112 | + * @param int $task_schedule_id |
|
| 113 | + * @return TaskSchedule|NULL |
|
| 114 | + */ |
|
| 115 | + public function find(int $task_schedule_id): ?TaskSchedule |
|
| 116 | + { |
|
| 117 | + return DB::table('maj_admintasks') |
|
| 118 | + ->select() |
|
| 119 | + ->where('majat_id', '=', $task_schedule_id) |
|
| 120 | + ->get() |
|
| 121 | + ->map(self::rowMapper()) |
|
| 122 | + ->first(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Add a new task schedule with the specified task ID, and frequency if defined. |
|
| 127 | + * Uses default for other settings. |
|
| 128 | + * |
|
| 129 | + * @param string $task_id |
|
| 130 | + * @param int $frequency |
|
| 131 | + * @return bool |
|
| 132 | + */ |
|
| 133 | + public function insertTask(string $task_id, int $frequency = 0): bool |
|
| 134 | + { |
|
| 135 | + $values = ['majat_task_id' => $task_id]; |
|
| 136 | + if ($frequency > 0) { |
|
| 137 | + $values['majat_frequency'] = $frequency; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return DB::table('maj_admintasks') |
|
| 141 | + ->insert($values); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Update a task schedule. |
|
| 146 | + * Returns the number of tasks schedules updated. |
|
| 147 | + * |
|
| 148 | + * @param TaskSchedule $task_schedule |
|
| 149 | + * @return int |
|
| 150 | + */ |
|
| 151 | + public function update(TaskSchedule $task_schedule): int |
|
| 152 | + { |
|
| 153 | + return DB::table('maj_admintasks') |
|
| 154 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 155 | + ->update([ |
|
| 156 | + 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
| 157 | + 'majat_last_run' => $task_schedule->lastRunTime(), |
|
| 158 | + 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
| 159 | + 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
| 160 | + 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
| 161 | + 'majat_running' => $task_schedule->isRunning() |
|
| 162 | + ]); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Delete a task schedule. |
|
| 167 | + * |
|
| 168 | + * @param TaskSchedule $task_schedule |
|
| 169 | + * @return int |
|
| 170 | + */ |
|
| 171 | + public function delete(TaskSchedule $task_schedule): int |
|
| 172 | + { |
|
| 173 | + return DB::table('maj_admintasks') |
|
| 174 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 175 | + ->delete(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Find a task by its name |
|
| 180 | + * |
|
| 181 | + * @param string $task_id |
|
| 182 | + * @return TaskInterface|NULL |
|
| 183 | + */ |
|
| 184 | + public function findTask(string $task_id): ?TaskInterface |
|
| 185 | + { |
|
| 186 | + if ($this->available()->has($task_id)) { |
|
| 187 | + return app($this->available()->get($task_id)); |
|
| 188 | + } |
|
| 189 | + return null; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Retrieve all tasks that are candidates to be run. |
|
| 194 | + * |
|
| 195 | + * @param bool $force Should the run be forced |
|
| 196 | + * @param string $task_id Specific task ID to be run |
|
| 197 | + * @return Collection<TaskSchedule> |
|
| 198 | + */ |
|
| 199 | + public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
| 200 | + { |
|
| 201 | + $query = DB::table('maj_admintasks') |
|
| 202 | + ->select() |
|
| 203 | + ->where('majat_status', '=', 'enabled') |
|
| 204 | + ->where(function (Builder $query): void { |
|
| 205 | + |
|
| 206 | + $query->where('majat_running', '=', 0) |
|
| 207 | + ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
| 208 | + }); |
|
| 209 | + |
|
| 210 | + if (!$force) { |
|
| 211 | + $query->where(function (Builder $query): void { |
|
| 212 | + |
|
| 213 | + $query->where('majat_running', '=', 0) |
|
| 214 | + ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
| 215 | + }); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if ($task_id !== null) { |
|
| 219 | + $query->where('majat_task_id', '=', $task_id); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $query->get()->map(self::rowMapper()); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Run the task associated with the schedule. |
|
| 227 | + * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
| 228 | + * The last run time is recorded only if the task is successful. |
|
| 229 | + * |
|
| 230 | + * @param TaskSchedule $task_schedule |
|
| 231 | + * @param boolean $force |
|
| 232 | + */ |
|
| 233 | + public function run(TaskSchedule $task_schedule, $force = false): void |
|
| 234 | + { |
|
| 235 | + $task_schedule = DB::table('maj_admintasks') |
|
| 236 | + ->select() |
|
| 237 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 238 | + ->lockForUpdate() |
|
| 239 | + ->get() |
|
| 240 | + ->map(self::rowMapper()) |
|
| 241 | + ->first(); |
|
| 242 | + |
|
| 243 | + if ( |
|
| 244 | + !$task_schedule->isRunning() && |
|
| 245 | + ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
| 246 | + ) { |
|
| 247 | + $task_schedule->setLastResult(false); |
|
| 248 | + |
|
| 249 | + $task = $this->findTask($task_schedule->taskId()); |
|
| 250 | + if ($task !== null) { |
|
| 251 | + $task_schedule->startRunning(); |
|
| 252 | + $this->update($task_schedule); |
|
| 253 | + |
|
| 254 | + $first_error = $task_schedule->wasLastRunSuccess(); |
|
| 255 | + try { |
|
| 256 | + $task_schedule->setLastResult($task->run($task_schedule)); |
|
| 257 | + } catch (Throwable $ex) { |
|
| 258 | + if ($first_error) { // Only record the first error, as this could fill the log. |
|
| 259 | + Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
| 260 | + '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
| 261 | + . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + if ($task_schedule->wasLastRunSuccess()) { |
|
| 266 | + $task_schedule->setLastRunTime(Carbon::now()); |
|
| 267 | + $task_schedule->decrementRemainingOccurences(); |
|
| 268 | + } |
|
| 269 | + $task_schedule->stopRunning(); |
|
| 270 | + } |
|
| 271 | + $this->update($task_schedule); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Mapper to return a TaskSchedule object from an object. |
|
| 277 | + * |
|
| 278 | + * @return Closure(stdClass $row): TaskSchedule |
|
| 279 | + */ |
|
| 280 | + public static function rowMapper(): Closure |
|
| 281 | + { |
|
| 282 | + return static function (stdClass $row): TaskSchedule { |
|
| 283 | + |
|
| 284 | + return new TaskSchedule( |
|
| 285 | + (int) $row->majat_id, |
|
| 286 | + $row->majat_task_id, |
|
| 287 | + $row->majat_status === 'enabled', |
|
| 288 | + Carbon::parse($row->majat_last_run), |
|
| 289 | + (bool) $row->majat_last_result, |
|
| 290 | + CarbonInterval::minutes($row->majat_frequency), |
|
| 291 | + (int) $row->majat_nb_occur, |
|
| 292 | + (bool) $row->majat_running |
|
| 293 | + ); |
|
| 294 | + }; |
|
| 295 | + } |
|
| 296 | 296 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | public function available(): Collection |
| 101 | 101 | { |
| 102 | - return Registry::cache()->array()->remember('maj-available-admintasks', function () { |
|
| 102 | + return Registry::cache()->array()->remember('maj-available-admintasks', function() { |
|
| 103 | 103 | return $this->module_service |
| 104 | 104 | ->findByInterface(ModuleTasksProviderInterface::class) |
| 105 | 105 | ->flatMap(fn(ModuleTasksProviderInterface $module) => $module->listTasks()); |
@@ -201,14 +201,14 @@ discard block |
||
| 201 | 201 | $query = DB::table('maj_admintasks') |
| 202 | 202 | ->select() |
| 203 | 203 | ->where('majat_status', '=', 'enabled') |
| 204 | - ->where(function (Builder $query): void { |
|
| 204 | + ->where(function(Builder $query): void { |
|
| 205 | 205 | |
| 206 | 206 | $query->where('majat_running', '=', 0) |
| 207 | 207 | ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
| 208 | 208 | }); |
| 209 | 209 | |
| 210 | 210 | if (!$force) { |
| 211 | - $query->where(function (Builder $query): void { |
|
| 211 | + $query->where(function(Builder $query): void { |
|
| 212 | 212 | |
| 213 | 213 | $query->where('majat_running', '=', 0) |
| 214 | 214 | ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
@@ -256,9 +256,9 @@ discard block |
||
| 256 | 256 | $task_schedule->setLastResult($task->run($task_schedule)); |
| 257 | 257 | } catch (Throwable $ex) { |
| 258 | 258 | if ($first_error) { // Only record the first error, as this could fill the log. |
| 259 | - Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
| 260 | - '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
| 261 | - . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
| 259 | + Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()).' '. |
|
| 260 | + '['.get_class($ex).'] '.$ex->getMessage().' '.$ex->getFile().':' |
|
| 261 | + . $ex->getLine().PHP_EOL.$ex->getTraceAsString()); |
|
| 262 | 262 | } |
| 263 | 263 | } |
| 264 | 264 | |
@@ -279,17 +279,17 @@ discard block |
||
| 279 | 279 | */ |
| 280 | 280 | public static function rowMapper(): Closure |
| 281 | 281 | { |
| 282 | - return static function (stdClass $row): TaskSchedule { |
|
| 282 | + return static function(stdClass $row): TaskSchedule { |
|
| 283 | 283 | |
| 284 | 284 | return new TaskSchedule( |
| 285 | - (int) $row->majat_id, |
|
| 285 | + (int)$row->majat_id, |
|
| 286 | 286 | $row->majat_task_id, |
| 287 | 287 | $row->majat_status === 'enabled', |
| 288 | 288 | Carbon::parse($row->majat_last_run), |
| 289 | - (bool) $row->majat_last_result, |
|
| 289 | + (bool)$row->majat_last_result, |
|
| 290 | 290 | CarbonInterval::minutes($row->majat_frequency), |
| 291 | - (int) $row->majat_nb_occur, |
|
| 292 | - (bool) $row->majat_running |
|
| 291 | + (int)$row->majat_nb_occur, |
|
| 292 | + (bool)$row->majat_running |
|
| 293 | 293 | ); |
| 294 | 294 | }; |
| 295 | 295 | } |
@@ -20,32 +20,32 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class TokenService |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Returns a random-ish generated token of a given size |
|
| 25 | - * |
|
| 26 | - * @param int $length Length of the token, default to 32 |
|
| 27 | - * @return string Random token |
|
| 28 | - */ |
|
| 29 | - public function generateRandomToken(int $length = 32): string |
|
| 30 | - { |
|
| 31 | - $chars = str_split('abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'); |
|
| 32 | - $len_chars = count($chars); |
|
| 33 | - $token = ''; |
|
| 34 | - |
|
| 35 | - for ($i = 0; $i < $length; $i++) { |
|
| 36 | - $token .= $chars[mt_rand(0, $len_chars - 1)]; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - # Number of 32 char chunks |
|
| 40 | - $chunks = ceil(strlen($token) / 32); |
|
| 41 | - $md5token = ''; |
|
| 42 | - |
|
| 43 | - # Run each chunk through md5 |
|
| 44 | - for ($i = 1; $i <= $chunks; $i++) { |
|
| 45 | - $md5token .= md5(substr($token, $i * 32 - 32, 32)); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - # Trim the token to the required length |
|
| 49 | - return substr($md5token, 0, $length); |
|
| 50 | - } |
|
| 23 | + /** |
|
| 24 | + * Returns a random-ish generated token of a given size |
|
| 25 | + * |
|
| 26 | + * @param int $length Length of the token, default to 32 |
|
| 27 | + * @return string Random token |
|
| 28 | + */ |
|
| 29 | + public function generateRandomToken(int $length = 32): string |
|
| 30 | + { |
|
| 31 | + $chars = str_split('abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'); |
|
| 32 | + $len_chars = count($chars); |
|
| 33 | + $token = ''; |
|
| 34 | + |
|
| 35 | + for ($i = 0; $i < $length; $i++) { |
|
| 36 | + $token .= $chars[mt_rand(0, $len_chars - 1)]; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + # Number of 32 char chunks |
|
| 40 | + $chunks = ceil(strlen($token) / 32); |
|
| 41 | + $md5token = ''; |
|
| 42 | + |
|
| 43 | + # Run each chunk through md5 |
|
| 44 | + for ($i = 1; $i <= $chunks; $i++) { |
|
| 45 | + $md5token .= md5(substr($token, $i * 32 - 32, 32)); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + # Trim the token to the required length |
|
| 49 | + return substr($md5token, 0, $length); |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -27,101 +27,101 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class HealthCheckService |
| 29 | 29 | { |
| 30 | - /** |
|
| 31 | - * Returns a query collating all gedcom records, for use in other queries |
|
| 32 | - * |
|
| 33 | - * @param Tree $tree |
|
| 34 | - * @return Builder |
|
| 35 | - */ |
|
| 36 | - private function allGedcomRecords(Tree $tree): Builder |
|
| 37 | - { |
|
| 38 | - return DB::table('individuals') |
|
| 39 | - ->select(DB::raw("'indi' AS ged_type"), 'i_id AS ged_id')->where('i_file', '=', $tree->id()) |
|
| 40 | - ->unionAll(DB::table('families') |
|
| 41 | - ->select(DB::raw("'fam' AS ged_type"), 'f_id AS ged_id')->where('f_file', '=', $tree->id())) |
|
| 42 | - ->unionAll(DB::table('sources') |
|
| 43 | - ->select(DB::raw("'sour' AS ged_type"), 's_id AS ged_id')->where('s_file', '=', $tree->id())) |
|
| 44 | - ->unionAll(DB::table('media') |
|
| 45 | - ->select(DB::raw("'media' AS ged_type"), 'm_id AS ged_id')->where('m_file', '=', $tree->id())) |
|
| 46 | - ->unionAll(DB::table('other') |
|
| 47 | - ->select(DB::raw('LOWER(o_type) AS ged_type'), 'o_id AS ged_id')->where('o_file', '=', $tree->id())); |
|
| 48 | - } |
|
| 30 | + /** |
|
| 31 | + * Returns a query collating all gedcom records, for use in other queries |
|
| 32 | + * |
|
| 33 | + * @param Tree $tree |
|
| 34 | + * @return Builder |
|
| 35 | + */ |
|
| 36 | + private function allGedcomRecords(Tree $tree): Builder |
|
| 37 | + { |
|
| 38 | + return DB::table('individuals') |
|
| 39 | + ->select(DB::raw("'indi' AS ged_type"), 'i_id AS ged_id')->where('i_file', '=', $tree->id()) |
|
| 40 | + ->unionAll(DB::table('families') |
|
| 41 | + ->select(DB::raw("'fam' AS ged_type"), 'f_id AS ged_id')->where('f_file', '=', $tree->id())) |
|
| 42 | + ->unionAll(DB::table('sources') |
|
| 43 | + ->select(DB::raw("'sour' AS ged_type"), 's_id AS ged_id')->where('s_file', '=', $tree->id())) |
|
| 44 | + ->unionAll(DB::table('media') |
|
| 45 | + ->select(DB::raw("'media' AS ged_type"), 'm_id AS ged_id')->where('m_file', '=', $tree->id())) |
|
| 46 | + ->unionAll(DB::table('other') |
|
| 47 | + ->select(DB::raw('LOWER(o_type) AS ged_type'), 'o_id AS ged_id')->where('o_file', '=', $tree->id())); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Returns the count of gedcom records by type in a Tree, as a keyed Collection. |
|
| 52 | - * |
|
| 53 | - * Collection output: |
|
| 54 | - * - Key : gedcom record type |
|
| 55 | - * - Value: count of records |
|
| 56 | - * |
|
| 57 | - * @param Tree $tree |
|
| 58 | - * @return Collection<string, int> |
|
| 59 | - */ |
|
| 60 | - public function countByRecordType(Tree $tree): Collection |
|
| 61 | - { |
|
| 62 | - return DB::query() |
|
| 63 | - ->fromSub($this->allGedcomRecords($tree), 'gedrecords') |
|
| 64 | - ->select('ged_type', new Expression('COUNT(ged_id) AS total')) |
|
| 65 | - ->groupBy('ged_type') |
|
| 66 | - ->pluck('total', 'ged_type'); |
|
| 67 | - } |
|
| 50 | + /** |
|
| 51 | + * Returns the count of gedcom records by type in a Tree, as a keyed Collection. |
|
| 52 | + * |
|
| 53 | + * Collection output: |
|
| 54 | + * - Key : gedcom record type |
|
| 55 | + * - Value: count of records |
|
| 56 | + * |
|
| 57 | + * @param Tree $tree |
|
| 58 | + * @return Collection<string, int> |
|
| 59 | + */ |
|
| 60 | + public function countByRecordType(Tree $tree): Collection |
|
| 61 | + { |
|
| 62 | + return DB::query() |
|
| 63 | + ->fromSub($this->allGedcomRecords($tree), 'gedrecords') |
|
| 64 | + ->select('ged_type', new Expression('COUNT(ged_id) AS total')) |
|
| 65 | + ->groupBy('ged_type') |
|
| 66 | + ->pluck('total', 'ged_type'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Returns the count of gedcom records changes by type in a Tree across a number of days, as a keyed Collection. |
|
| 71 | - * |
|
| 72 | - * Collection output: |
|
| 73 | - * - Key : gedcom record type |
|
| 74 | - * - Value: count of changes |
|
| 75 | - * |
|
| 76 | - * @param Tree $tree |
|
| 77 | - * @return Collection<string, int> |
|
| 78 | - */ |
|
| 79 | - public function changesByRecordType(Tree $tree, int $nb_days): Collection |
|
| 80 | - { |
|
| 81 | - return DB::table('change') |
|
| 82 | - ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 69 | + /** |
|
| 70 | + * Returns the count of gedcom records changes by type in a Tree across a number of days, as a keyed Collection. |
|
| 71 | + * |
|
| 72 | + * Collection output: |
|
| 73 | + * - Key : gedcom record type |
|
| 74 | + * - Value: count of changes |
|
| 75 | + * |
|
| 76 | + * @param Tree $tree |
|
| 77 | + * @return Collection<string, int> |
|
| 78 | + */ |
|
| 79 | + public function changesByRecordType(Tree $tree, int $nb_days): Collection |
|
| 80 | + { |
|
| 81 | + return DB::table('change') |
|
| 82 | + ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 83 | 83 | |
| 84 | - $join->on('change.xref', '=', 'gedrecords.ged_id') |
|
| 85 | - ->where('change.gedcom_id', '=', $tree->id()); |
|
| 86 | - }) |
|
| 87 | - ->select('ged_type AS type', new Expression('COUNT(change_id) AS count')) |
|
| 88 | - ->where('change.status', '', 'accepted') |
|
| 89 | - ->where('change.change_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 90 | - ->groupBy('ged_type') |
|
| 91 | - ->pluck('total', 'ged_type'); |
|
| 92 | - } |
|
| 84 | + $join->on('change.xref', '=', 'gedrecords.ged_id') |
|
| 85 | + ->where('change.gedcom_id', '=', $tree->id()); |
|
| 86 | + }) |
|
| 87 | + ->select('ged_type AS type', new Expression('COUNT(change_id) AS count')) |
|
| 88 | + ->where('change.status', '', 'accepted') |
|
| 89 | + ->where('change.change_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 90 | + ->groupBy('ged_type') |
|
| 91 | + ->pluck('total', 'ged_type'); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Return the error logs associated with a tree across a number of days, grouped by error message, as a Collection. |
|
| 96 | - * |
|
| 97 | - * Collection output: |
|
| 98 | - * - Value: stdClass object |
|
| 99 | - * - log message: Error log message |
|
| 100 | - * - type: 'site' if no associated Tree, the Tree ID otherwise |
|
| 101 | - * - nblogs: The number of occurence of the same error message |
|
| 102 | - * - lastoccurred: Date/time of the last occurence of the error message |
|
| 103 | - * |
|
| 104 | - * @param Tree $tree |
|
| 105 | - * @param int $nb_days |
|
| 106 | - * @return Collection<\stdClass> |
|
| 107 | - */ |
|
| 108 | - public function errorLogs(Tree $tree, int $nb_days): Collection |
|
| 109 | - { |
|
| 110 | - return DB::table('log') |
|
| 111 | - ->select( |
|
| 112 | - 'log_message', |
|
| 113 | - new Expression("IFNULL(gedcom_id, 'site') as type"), |
|
| 114 | - new Expression('COUNT(log_id) AS nblogs'), |
|
| 115 | - new Expression('MAX(log_time) AS lastoccurred') |
|
| 116 | - ) |
|
| 117 | - ->where('log_type', '=', 'error') |
|
| 118 | - ->where(function (Builder $query) use ($tree): void { |
|
| 119 | - $query->where('gedcom_id', '=', $tree->id()) |
|
| 120 | - ->orWhereNull('gedcom_id'); |
|
| 121 | - }) |
|
| 122 | - ->where('log_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 123 | - ->groupBy('log_message', 'gedcom_id') |
|
| 124 | - ->orderByDesc('lastoccurred') |
|
| 125 | - ->get(); |
|
| 126 | - } |
|
| 94 | + /** |
|
| 95 | + * Return the error logs associated with a tree across a number of days, grouped by error message, as a Collection. |
|
| 96 | + * |
|
| 97 | + * Collection output: |
|
| 98 | + * - Value: stdClass object |
|
| 99 | + * - log message: Error log message |
|
| 100 | + * - type: 'site' if no associated Tree, the Tree ID otherwise |
|
| 101 | + * - nblogs: The number of occurence of the same error message |
|
| 102 | + * - lastoccurred: Date/time of the last occurence of the error message |
|
| 103 | + * |
|
| 104 | + * @param Tree $tree |
|
| 105 | + * @param int $nb_days |
|
| 106 | + * @return Collection<\stdClass> |
|
| 107 | + */ |
|
| 108 | + public function errorLogs(Tree $tree, int $nb_days): Collection |
|
| 109 | + { |
|
| 110 | + return DB::table('log') |
|
| 111 | + ->select( |
|
| 112 | + 'log_message', |
|
| 113 | + new Expression("IFNULL(gedcom_id, 'site') as type"), |
|
| 114 | + new Expression('COUNT(log_id) AS nblogs'), |
|
| 115 | + new Expression('MAX(log_time) AS lastoccurred') |
|
| 116 | + ) |
|
| 117 | + ->where('log_type', '=', 'error') |
|
| 118 | + ->where(function (Builder $query) use ($tree): void { |
|
| 119 | + $query->where('gedcom_id', '=', $tree->id()) |
|
| 120 | + ->orWhereNull('gedcom_id'); |
|
| 121 | + }) |
|
| 122 | + ->where('log_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 123 | + ->groupBy('log_message', 'gedcom_id') |
|
| 124 | + ->orderByDesc('lastoccurred') |
|
| 125 | + ->get(); |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | public function changesByRecordType(Tree $tree, int $nb_days): Collection |
| 80 | 80 | { |
| 81 | 81 | return DB::table('change') |
| 82 | - ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 82 | + ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function(JoinClause $join) use ($tree): void { |
|
| 83 | 83 | |
| 84 | 84 | $join->on('change.xref', '=', 'gedrecords.ged_id') |
| 85 | 85 | ->where('change.gedcom_id', '=', $tree->id()); |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | new Expression('MAX(log_time) AS lastoccurred') |
| 116 | 116 | ) |
| 117 | 117 | ->where('log_type', '=', 'error') |
| 118 | - ->where(function (Builder $query) use ($tree): void { |
|
| 118 | + ->where(function(Builder $query) use ($tree): void { |
|
| 119 | 119 | $query->where('gedcom_id', '=', $tree->id()) |
| 120 | 120 | ->orWhereNull('gedcom_id'); |
| 121 | 121 | }) |
@@ -41,127 +41,127 @@ |
||
| 41 | 41 | * Allow for tasks to be run on a (nearly-)regular schedule |
| 42 | 42 | */ |
| 43 | 43 | class AdminTasksModule extends AbstractModule implements |
| 44 | - ModuleMyArtJaubInterface, |
|
| 45 | - ModuleConfigInterface, |
|
| 46 | - ModuleGlobalInterface, |
|
| 47 | - ModuleTasksProviderInterface |
|
| 44 | + ModuleMyArtJaubInterface, |
|
| 45 | + ModuleConfigInterface, |
|
| 46 | + ModuleGlobalInterface, |
|
| 47 | + ModuleTasksProviderInterface |
|
| 48 | 48 | { |
| 49 | - use ModuleMyArtJaubTrait { |
|
| 50 | - boot as traitBoot; |
|
| 51 | - } |
|
| 52 | - use ModuleConfigTrait; |
|
| 53 | - use ModuleGlobalTrait; |
|
| 54 | - |
|
| 55 | - //How to update the database schema for this module |
|
| 56 | - private const SCHEMA_TARGET_VERSION = 2; |
|
| 57 | - private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
|
| 58 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * {@inheritDoc} |
|
| 62 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 63 | - */ |
|
| 64 | - public function title(): string |
|
| 65 | - { |
|
| 66 | - return I18N::translate('Administration Tasks'); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * {@inheritDoc} |
|
| 71 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 72 | - */ |
|
| 73 | - public function description(): string |
|
| 74 | - { |
|
| 75 | - return I18N::translate('Manage and run nearly-scheduled administration tasks.'); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * {@inheritDoc} |
|
| 80 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::boot() |
|
| 81 | - */ |
|
| 82 | - public function boot(): void |
|
| 83 | - { |
|
| 84 | - $this->traitBoot(); |
|
| 85 | - app(MigrationService::class)->updateSchema( |
|
| 86 | - self::SCHEMA_MIGRATION_PREFIX, |
|
| 87 | - self::SCHEMA_SETTING_NAME, |
|
| 88 | - self::SCHEMA_TARGET_VERSION |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritDoc} |
|
| 94 | - * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes() |
|
| 95 | - */ |
|
| 96 | - public function loadRoutes(Map $router): void |
|
| 97 | - { |
|
| 98 | - $router->attach('', '', static function (Map $router): void { |
|
| 99 | - |
|
| 100 | - $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 101 | - $router->tokens(['enable' => '[01]']); |
|
| 102 | - |
|
| 103 | - $router->attach('', '/admin', static function (Map $router): void { |
|
| 104 | - |
|
| 105 | - $router->extras([ |
|
| 106 | - 'middleware' => [ |
|
| 107 | - AuthAdministrator::class, |
|
| 108 | - ], |
|
| 109 | - ]); |
|
| 110 | - $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
|
| 111 | - |
|
| 112 | - $router->attach('', '/tasks', static function (Map $router): void { |
|
| 113 | - |
|
| 114 | - $router->get(TasksList::class, '', TasksList::class); |
|
| 115 | - $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
|
| 116 | - $router->post(TaskEditAction::class, '/{task}', TaskEditAction::class); |
|
| 117 | - $router->get(TaskStatusAction::class, '/{task}/status/{enable}', TaskStatusAction::class); |
|
| 118 | - }); |
|
| 119 | - }); |
|
| 120 | - |
|
| 121 | - $router->get(TaskTrigger::class, '/trigger{/task}', TaskTrigger::class) |
|
| 122 | - ->allows(RequestMethodInterface::METHOD_POST); |
|
| 123 | - |
|
| 124 | - $router->post(TokenGenerate::class, '/token', TokenGenerate::class) |
|
| 125 | - ->extras(['middleware' => [AuthAdministrator::class]]); |
|
| 126 | - }); |
|
| 127 | - }); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * {@inheritDoc} |
|
| 132 | - * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersion() |
|
| 133 | - */ |
|
| 134 | - public function customModuleVersion(): string |
|
| 135 | - { |
|
| 136 | - return '2.1.0-v.1'; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * {@inheritDoc} |
|
| 141 | - * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink() |
|
| 142 | - */ |
|
| 143 | - public function getConfigLink(): string |
|
| 144 | - { |
|
| 145 | - return route(AdminConfigPage::class); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * {@inheritDoc} |
|
| 150 | - * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::bodyContent() |
|
| 151 | - */ |
|
| 152 | - public function bodyContent(): string |
|
| 153 | - { |
|
| 154 | - return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * {@inheritDoc} |
|
| 159 | - * @see ModuleTasksProviderInterface::listTasks() |
|
| 160 | - */ |
|
| 161 | - public function listTasks(): array |
|
| 162 | - { |
|
| 163 | - return [ |
|
| 164 | - 'maj-healthcheck' => HealthCheckEmailTask::class |
|
| 165 | - ]; |
|
| 166 | - } |
|
| 49 | + use ModuleMyArtJaubTrait { |
|
| 50 | + boot as traitBoot; |
|
| 51 | + } |
|
| 52 | + use ModuleConfigTrait; |
|
| 53 | + use ModuleGlobalTrait; |
|
| 54 | + |
|
| 55 | + //How to update the database schema for this module |
|
| 56 | + private const SCHEMA_TARGET_VERSION = 2; |
|
| 57 | + private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
|
| 58 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * {@inheritDoc} |
|
| 62 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 63 | + */ |
|
| 64 | + public function title(): string |
|
| 65 | + { |
|
| 66 | + return I18N::translate('Administration Tasks'); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * {@inheritDoc} |
|
| 71 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 72 | + */ |
|
| 73 | + public function description(): string |
|
| 74 | + { |
|
| 75 | + return I18N::translate('Manage and run nearly-scheduled administration tasks.'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * {@inheritDoc} |
|
| 80 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::boot() |
|
| 81 | + */ |
|
| 82 | + public function boot(): void |
|
| 83 | + { |
|
| 84 | + $this->traitBoot(); |
|
| 85 | + app(MigrationService::class)->updateSchema( |
|
| 86 | + self::SCHEMA_MIGRATION_PREFIX, |
|
| 87 | + self::SCHEMA_SETTING_NAME, |
|
| 88 | + self::SCHEMA_TARGET_VERSION |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritDoc} |
|
| 94 | + * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes() |
|
| 95 | + */ |
|
| 96 | + public function loadRoutes(Map $router): void |
|
| 97 | + { |
|
| 98 | + $router->attach('', '', static function (Map $router): void { |
|
| 99 | + |
|
| 100 | + $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 101 | + $router->tokens(['enable' => '[01]']); |
|
| 102 | + |
|
| 103 | + $router->attach('', '/admin', static function (Map $router): void { |
|
| 104 | + |
|
| 105 | + $router->extras([ |
|
| 106 | + 'middleware' => [ |
|
| 107 | + AuthAdministrator::class, |
|
| 108 | + ], |
|
| 109 | + ]); |
|
| 110 | + $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
|
| 111 | + |
|
| 112 | + $router->attach('', '/tasks', static function (Map $router): void { |
|
| 113 | + |
|
| 114 | + $router->get(TasksList::class, '', TasksList::class); |
|
| 115 | + $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
|
| 116 | + $router->post(TaskEditAction::class, '/{task}', TaskEditAction::class); |
|
| 117 | + $router->get(TaskStatusAction::class, '/{task}/status/{enable}', TaskStatusAction::class); |
|
| 118 | + }); |
|
| 119 | + }); |
|
| 120 | + |
|
| 121 | + $router->get(TaskTrigger::class, '/trigger{/task}', TaskTrigger::class) |
|
| 122 | + ->allows(RequestMethodInterface::METHOD_POST); |
|
| 123 | + |
|
| 124 | + $router->post(TokenGenerate::class, '/token', TokenGenerate::class) |
|
| 125 | + ->extras(['middleware' => [AuthAdministrator::class]]); |
|
| 126 | + }); |
|
| 127 | + }); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * {@inheritDoc} |
|
| 132 | + * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersion() |
|
| 133 | + */ |
|
| 134 | + public function customModuleVersion(): string |
|
| 135 | + { |
|
| 136 | + return '2.1.0-v.1'; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * {@inheritDoc} |
|
| 141 | + * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink() |
|
| 142 | + */ |
|
| 143 | + public function getConfigLink(): string |
|
| 144 | + { |
|
| 145 | + return route(AdminConfigPage::class); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * {@inheritDoc} |
|
| 150 | + * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::bodyContent() |
|
| 151 | + */ |
|
| 152 | + public function bodyContent(): string |
|
| 153 | + { |
|
| 154 | + return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * {@inheritDoc} |
|
| 159 | + * @see ModuleTasksProviderInterface::listTasks() |
|
| 160 | + */ |
|
| 161 | + public function listTasks(): array |
|
| 162 | + { |
|
| 163 | + return [ |
|
| 164 | + 'maj-healthcheck' => HealthCheckEmailTask::class |
|
| 165 | + ]; |
|
| 166 | + } |
|
| 167 | 167 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | //How to update the database schema for this module |
| 56 | 56 | private const SCHEMA_TARGET_VERSION = 2; |
| 57 | 57 | private const SCHEMA_SETTING_NAME = 'MAJ_ADMTASKS_SCHEMA_VERSION'; |
| 58 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 58 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__.'\Schema'; |
|
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * {@inheritDoc} |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function loadRoutes(Map $router): void |
| 97 | 97 | { |
| 98 | - $router->attach('', '', static function (Map $router): void { |
|
| 98 | + $router->attach('', '', static function(Map $router): void { |
|
| 99 | 99 | |
| 100 | - $router->attach('', '/module-maj/admintasks', static function (Map $router): void { |
|
| 100 | + $router->attach('', '/module-maj/admintasks', static function(Map $router): void { |
|
| 101 | 101 | $router->tokens(['enable' => '[01]']); |
| 102 | 102 | |
| 103 | - $router->attach('', '/admin', static function (Map $router): void { |
|
| 103 | + $router->attach('', '/admin', static function(Map $router): void { |
|
| 104 | 104 | |
| 105 | 105 | $router->extras([ |
| 106 | 106 | 'middleware' => [ |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | ]); |
| 110 | 110 | $router->get(AdminConfigPage::class, '/config', AdminConfigPage::class); |
| 111 | 111 | |
| 112 | - $router->attach('', '/tasks', static function (Map $router): void { |
|
| 112 | + $router->attach('', '/tasks', static function(Map $router): void { |
|
| 113 | 113 | |
| 114 | 114 | $router->get(TasksList::class, '', TasksList::class); |
| 115 | 115 | $router->get(TaskEditPage::class, '/{task}', TaskEditPage::class); |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function bodyContent(): string |
| 153 | 153 | { |
| 154 | - return view($this->name() . '::snippet', [ 'url' => route(TaskTrigger::class) ]); |
|
| 154 | + return view($this->name().'::snippet', ['url' => route(TaskTrigger::class)]); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -39,165 +39,165 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class HealthCheckEmailTask implements TaskInterface, ConfigurableTaskInterface |
| 41 | 41 | { |
| 42 | - /** |
|
| 43 | - * Name of the Tree preference to check if the task is enabled for that tree |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
| 47 | - |
|
| 48 | - private ?AdminTasksModule $module; |
|
| 49 | - private HealthCheckService $healthcheck_service; |
|
| 50 | - private EmailService $email_service; |
|
| 51 | - private UserService $user_service; |
|
| 52 | - private TreeService $tree_service; |
|
| 53 | - private UpgradeService $upgrade_service; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Constructor for HealthCheckTask |
|
| 57 | - * |
|
| 58 | - * @param ModuleService $module_service |
|
| 59 | - * @param HealthCheckService $healthcheck_service |
|
| 60 | - * @param EmailService $email_service |
|
| 61 | - * @param UserService $user_service |
|
| 62 | - * @param TreeService $tree_service |
|
| 63 | - * @param UpgradeService $upgrade_service |
|
| 64 | - */ |
|
| 65 | - public function __construct( |
|
| 66 | - ModuleService $module_service, |
|
| 67 | - HealthCheckService $healthcheck_service, |
|
| 68 | - EmailService $email_service, |
|
| 69 | - UserService $user_service, |
|
| 70 | - TreeService $tree_service, |
|
| 71 | - UpgradeService $upgrade_service |
|
| 72 | - ) { |
|
| 73 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 74 | - $this->healthcheck_service = $healthcheck_service; |
|
| 75 | - $this->email_service = $email_service; |
|
| 76 | - $this->user_service = $user_service; |
|
| 77 | - $this->tree_service = $tree_service; |
|
| 78 | - $this->upgrade_service = $upgrade_service; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * {@inheritDoc} |
|
| 84 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
| 85 | - */ |
|
| 86 | - public function name(): string |
|
| 87 | - { |
|
| 88 | - return I18N::translate('Healthcheck Email'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * {@inheritDoc} |
|
| 93 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
| 94 | - */ |
|
| 95 | - public function defaultFrequency(): int |
|
| 96 | - { |
|
| 97 | - return 10080; // = 1 week = 7 * 24 * 60 min |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * {@inheritDoc} |
|
| 102 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
| 103 | - */ |
|
| 104 | - public function run(TaskSchedule $task_schedule): bool |
|
| 105 | - { |
|
| 106 | - if ($this->module === null) { |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $res = true; |
|
| 111 | - |
|
| 112 | - // Compute the number of days to compute |
|
| 113 | - $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
| 114 | - //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 115 | - $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
| 116 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 117 | - |
|
| 118 | - $view_params_site = [ |
|
| 119 | - 'nb_days' => $nb_days, |
|
| 120 | - 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
| 121 | - 'latest_version' => $this->upgrade_service->latestVersion(), |
|
| 122 | - 'download_url' => $this->upgrade_service->downloadUrl(), |
|
| 123 | - 'all_users' => $this->user_service->all(), |
|
| 124 | - 'unapproved' => $this->user_service->unapproved(), |
|
| 125 | - 'unverified' => $this->user_service->unverified(), |
|
| 126 | - ]; |
|
| 127 | - |
|
| 128 | - foreach ($this->tree_service->all() as $tree) { |
|
| 129 | - /** @var Tree $tree */ |
|
| 130 | - |
|
| 131 | - if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 136 | - if ($webmaster === null) { |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - I18N::init($webmaster->getPreference('language')); |
|
| 140 | - |
|
| 141 | - $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
| 142 | - $nb_errors = $error_logs->sum('nblogs'); |
|
| 143 | - |
|
| 144 | - $view_params = array_merge($view_params_site, [ |
|
| 145 | - 'tree' => $tree, |
|
| 146 | - 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
| 147 | - 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
| 148 | - 'error_logs' => $error_logs, |
|
| 149 | - 'nb_errors' => $nb_errors |
|
| 150 | - ]); |
|
| 151 | - |
|
| 152 | - $res = $res && $this->email_service->send( |
|
| 153 | - new TreeUser($tree), |
|
| 154 | - $webmaster, |
|
| 155 | - new NoReplyUser(), |
|
| 156 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 157 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 158 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $res; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * {@inheritDoc} |
|
| 167 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
| 168 | - */ |
|
| 169 | - public function configView(ServerRequestInterface $request): string |
|
| 170 | - { |
|
| 171 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 172 | - 'all_trees' => $this->tree_service->all() |
|
| 173 | - ]); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * {@inheritDoc} |
|
| 178 | - * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
| 179 | - */ |
|
| 180 | - public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
| 181 | - { |
|
| 182 | - try { |
|
| 183 | - $params = (array) $request->getParsedBody(); |
|
| 184 | - |
|
| 185 | - foreach ($this->tree_service->all() as $tree) { |
|
| 186 | - if (Auth::isManager($tree)) { |
|
| 187 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 188 | - $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - return true; |
|
| 192 | - } catch (Throwable $ex) { |
|
| 193 | - Log::addErrorLog( |
|
| 194 | - sprintf( |
|
| 195 | - 'Error while updating the Task schedule "%s". Exception: %s', |
|
| 196 | - $task_schedule->id(), |
|
| 197 | - $ex->getMessage() |
|
| 198 | - ) |
|
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 42 | + /** |
|
| 43 | + * Name of the Tree preference to check if the task is enabled for that tree |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
| 47 | + |
|
| 48 | + private ?AdminTasksModule $module; |
|
| 49 | + private HealthCheckService $healthcheck_service; |
|
| 50 | + private EmailService $email_service; |
|
| 51 | + private UserService $user_service; |
|
| 52 | + private TreeService $tree_service; |
|
| 53 | + private UpgradeService $upgrade_service; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Constructor for HealthCheckTask |
|
| 57 | + * |
|
| 58 | + * @param ModuleService $module_service |
|
| 59 | + * @param HealthCheckService $healthcheck_service |
|
| 60 | + * @param EmailService $email_service |
|
| 61 | + * @param UserService $user_service |
|
| 62 | + * @param TreeService $tree_service |
|
| 63 | + * @param UpgradeService $upgrade_service |
|
| 64 | + */ |
|
| 65 | + public function __construct( |
|
| 66 | + ModuleService $module_service, |
|
| 67 | + HealthCheckService $healthcheck_service, |
|
| 68 | + EmailService $email_service, |
|
| 69 | + UserService $user_service, |
|
| 70 | + TreeService $tree_service, |
|
| 71 | + UpgradeService $upgrade_service |
|
| 72 | + ) { |
|
| 73 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 74 | + $this->healthcheck_service = $healthcheck_service; |
|
| 75 | + $this->email_service = $email_service; |
|
| 76 | + $this->user_service = $user_service; |
|
| 77 | + $this->tree_service = $tree_service; |
|
| 78 | + $this->upgrade_service = $upgrade_service; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * {@inheritDoc} |
|
| 84 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::name() |
|
| 85 | + */ |
|
| 86 | + public function name(): string |
|
| 87 | + { |
|
| 88 | + return I18N::translate('Healthcheck Email'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * {@inheritDoc} |
|
| 93 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::defaultFrequency() |
|
| 94 | + */ |
|
| 95 | + public function defaultFrequency(): int |
|
| 96 | + { |
|
| 97 | + return 10080; // = 1 week = 7 * 24 * 60 min |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * {@inheritDoc} |
|
| 102 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\TaskInterface::run() |
|
| 103 | + */ |
|
| 104 | + public function run(TaskSchedule $task_schedule): bool |
|
| 105 | + { |
|
| 106 | + if ($this->module === null) { |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $res = true; |
|
| 111 | + |
|
| 112 | + // Compute the number of days to compute |
|
| 113 | + $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
| 114 | + //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 115 | + $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
| 116 | + $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 117 | + |
|
| 118 | + $view_params_site = [ |
|
| 119 | + 'nb_days' => $nb_days, |
|
| 120 | + 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
| 121 | + 'latest_version' => $this->upgrade_service->latestVersion(), |
|
| 122 | + 'download_url' => $this->upgrade_service->downloadUrl(), |
|
| 123 | + 'all_users' => $this->user_service->all(), |
|
| 124 | + 'unapproved' => $this->user_service->unapproved(), |
|
| 125 | + 'unverified' => $this->user_service->unverified(), |
|
| 126 | + ]; |
|
| 127 | + |
|
| 128 | + foreach ($this->tree_service->all() as $tree) { |
|
| 129 | + /** @var Tree $tree */ |
|
| 130 | + |
|
| 131 | + if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 136 | + if ($webmaster === null) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + I18N::init($webmaster->getPreference('language')); |
|
| 140 | + |
|
| 141 | + $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
| 142 | + $nb_errors = $error_logs->sum('nblogs'); |
|
| 143 | + |
|
| 144 | + $view_params = array_merge($view_params_site, [ |
|
| 145 | + 'tree' => $tree, |
|
| 146 | + 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
| 147 | + 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
| 148 | + 'error_logs' => $error_logs, |
|
| 149 | + 'nb_errors' => $nb_errors |
|
| 150 | + ]); |
|
| 151 | + |
|
| 152 | + $res = $res && $this->email_service->send( |
|
| 153 | + new TreeUser($tree), |
|
| 154 | + $webmaster, |
|
| 155 | + new NoReplyUser(), |
|
| 156 | + I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 157 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 158 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $res; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * {@inheritDoc} |
|
| 167 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::configView() |
|
| 168 | + */ |
|
| 169 | + public function configView(ServerRequestInterface $request): string |
|
| 170 | + { |
|
| 171 | + return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 172 | + 'all_trees' => $this->tree_service->all() |
|
| 173 | + ]); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * {@inheritDoc} |
|
| 178 | + * @see \MyArtJaub\Webtrees\Contracts\Tasks\ConfigurableTaskInterface::updateConfig() |
|
| 179 | + */ |
|
| 180 | + public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
| 181 | + { |
|
| 182 | + try { |
|
| 183 | + $params = (array) $request->getParsedBody(); |
|
| 184 | + |
|
| 185 | + foreach ($this->tree_service->all() as $tree) { |
|
| 186 | + if (Auth::isManager($tree)) { |
|
| 187 | + $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 188 | + $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + return true; |
|
| 192 | + } catch (Throwable $ex) { |
|
| 193 | + Log::addErrorLog( |
|
| 194 | + sprintf( |
|
| 195 | + 'Error while updating the Task schedule "%s". Exception: %s', |
|
| 196 | + $task_schedule->id(), |
|
| 197 | + $ex->getMessage() |
|
| 198 | + ) |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | 203 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
| 137 | 137 | //@phpcs:ignore Generic.Files.LineLength.TooLong |
| 138 | 138 | $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
| 139 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
| 139 | + $nb_days = (int)$interval->ceilDay()->totalDays; |
|
| 140 | 140 | |
| 141 | 141 | $view_params_site = [ |
| 142 | 142 | 'nb_days' => $nb_days, |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
| 158 | + $webmaster = $this->user_service->find((int)$tree->getPreference('WEBMASTER_USER_ID')); |
|
| 159 | 159 | if ($webmaster === null) { |
| 160 | 160 | continue; |
| 161 | 161 | } |
@@ -176,9 +176,9 @@ discard block |
||
| 176 | 176 | new TreeUser($tree), |
| 177 | 177 | $webmaster, |
| 178 | 178 | new NoReplyUser(), |
| 179 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
| 180 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 181 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 179 | + I18N::translate('Health Check Report').' - '.I18N::translate('Tree %s', $tree->name()), |
|
| 180 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
| 181 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
| 182 | 182 | ); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | */ |
| 192 | 192 | public function configView(ServerRequestInterface $request): string |
| 193 | 193 | { |
| 194 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
| 194 | + return $this->module === null ? '' : view($this->module->name().'::tasks/healthcheck/config', [ |
|
| 195 | 195 | 'all_trees' => $this->tree_service->all() |
| 196 | 196 | ]); |
| 197 | 197 | } |
@@ -203,11 +203,11 @@ discard block |
||
| 203 | 203 | public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
| 204 | 204 | { |
| 205 | 205 | try { |
| 206 | - $params = (array) $request->getParsedBody(); |
|
| 206 | + $params = (array)$request->getParsedBody(); |
|
| 207 | 207 | |
| 208 | 208 | foreach ($this->tree_service->all() as $tree) { |
| 209 | 209 | if (Auth::isManager($tree)) { |
| 210 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
| 210 | + $tree_enabled = (bool)($params['HEALTHCHECK_ENABLED_'.$tree->id()] ?? false); |
|
| 211 | 211 | $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -28,15 +28,15 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | function hook(string $hook_interface, callable $apply, $default = null) |
| 30 | 30 | { |
| 31 | - try { |
|
| 32 | - $hook_collector = app(HookServiceInterface::class)->use($hook_interface); |
|
| 33 | - if ($hook_collector !== null) { |
|
| 34 | - return $apply($hook_collector); |
|
| 35 | - } |
|
| 36 | - } catch (BindingResolutionException $ex) { |
|
| 37 | - } |
|
| 31 | + try { |
|
| 32 | + $hook_collector = app(HookServiceInterface::class)->use($hook_interface); |
|
| 33 | + if ($hook_collector !== null) { |
|
| 34 | + return $apply($hook_collector); |
|
| 35 | + } |
|
| 36 | + } catch (BindingResolutionException $ex) { |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return $default; |
|
| 39 | + return $default; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -48,5 +48,5 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | function columnIndex(int $initial_index, Collection $new_column_indexes): int |
| 50 | 50 | { |
| 51 | - return $initial_index + $new_column_indexes->filter(fn(int $i) => $i <= $initial_index)->count(); |
|
| 51 | + return $initial_index + $new_column_indexes->filter(fn(int $i) => $i <= $initial_index)->count(); |
|
| 52 | 52 | } |
@@ -30,43 +30,43 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class AuthTreePreference implements MiddlewareInterface |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * {@inheritDoc} |
|
| 35 | - * @see \Psr\Http\Server\MiddlewareInterface::process() |
|
| 36 | - */ |
|
| 37 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 38 | - { |
|
| 39 | - $tree = $request->getAttribute('tree'); |
|
| 40 | - assert($tree instanceof Tree); |
|
| 41 | - /** @var Tree $tree */ |
|
| 33 | + /** |
|
| 34 | + * {@inheritDoc} |
|
| 35 | + * @see \Psr\Http\Server\MiddlewareInterface::process() |
|
| 36 | + */ |
|
| 37 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 38 | + { |
|
| 39 | + $tree = $request->getAttribute('tree'); |
|
| 40 | + assert($tree instanceof Tree); |
|
| 41 | + /** @var Tree $tree */ |
|
| 42 | 42 | |
| 43 | - $route = $request->getAttribute('route'); |
|
| 44 | - assert($route instanceof \Aura\Router\Route); |
|
| 45 | - /** @var \Aura\Router\Route $route */ |
|
| 43 | + $route = $request->getAttribute('route'); |
|
| 44 | + assert($route instanceof \Aura\Router\Route); |
|
| 45 | + /** @var \Aura\Router\Route $route */ |
|
| 46 | 46 | |
| 47 | - $user = $request->getAttribute('user'); |
|
| 47 | + $user = $request->getAttribute('user'); |
|
| 48 | 48 | |
| 49 | - $permission_preference = $route->extras['permission_preference'] ?? ''; |
|
| 50 | - $permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference); |
|
| 49 | + $permission_preference = $route->extras['permission_preference'] ?? ''; |
|
| 50 | + $permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference); |
|
| 51 | 51 | |
| 52 | - // Permissions are configured |
|
| 53 | - if (is_numeric($permission_level)) { |
|
| 54 | - // Logged in with the correct role? |
|
| 55 | - if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 56 | - return $handler->handle($request); |
|
| 57 | - } |
|
| 52 | + // Permissions are configured |
|
| 53 | + if (is_numeric($permission_level)) { |
|
| 54 | + // Logged in with the correct role? |
|
| 55 | + if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 56 | + return $handler->handle($request); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Logged in, but without the correct role? |
|
| 60 | - if ($user instanceof User) { |
|
| 61 | - throw new HttpAccessDeniedException(); |
|
| 62 | - } |
|
| 63 | - } |
|
| 59 | + // Logged in, but without the correct role? |
|
| 60 | + if ($user instanceof User) { |
|
| 61 | + throw new HttpAccessDeniedException(); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - // Permissions no configured, or not logged in |
|
| 66 | - if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
|
| 67 | - throw new HttpAccessDeniedException(); |
|
| 68 | - } |
|
| 65 | + // Permissions no configured, or not logged in |
|
| 66 | + if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
|
| 67 | + throw new HttpAccessDeniedException(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return redirect(route(LoginPage::class, ['tree' => $tree->name(), 'url' => $request->getUri()])); |
|
| 71 | - } |
|
| 70 | + return redirect(route(LoginPage::class, ['tree' => $tree->name(), 'url' => $request->getUri()])); |
|
| 71 | + } |
|
| 72 | 72 | } |
@@ -52,7 +52,7 @@ |
||
| 52 | 52 | // Permissions are configured |
| 53 | 53 | if (is_numeric($permission_level)) { |
| 54 | 54 | // Logged in with the correct role? |
| 55 | - if (Auth::accessLevel($tree, $user) <= (int) $permission_level) { |
|
| 55 | + if (Auth::accessLevel($tree, $user) <= (int)$permission_level) { |
|
| 56 | 56 | return $handler->handle($request); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -28,76 +28,76 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | abstract class AbstractHookCollector implements HookCollectorInterface, HookInterface |
| 30 | 30 | { |
| 31 | - protected Collection $hooks; |
|
| 32 | - private ModuleInterface $module; |
|
| 31 | + protected Collection $hooks; |
|
| 32 | + private ModuleInterface $module; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Constructor for AbstractHookCollector |
|
| 36 | - * |
|
| 37 | - * @param ModuleInterface $module |
|
| 38 | - */ |
|
| 39 | - public function __construct(ModuleInterface $module) |
|
| 40 | - { |
|
| 41 | - $this->hooks = new Collection(); |
|
| 42 | - $this->module = $module; |
|
| 43 | - } |
|
| 34 | + /** |
|
| 35 | + * Constructor for AbstractHookCollector |
|
| 36 | + * |
|
| 37 | + * @param ModuleInterface $module |
|
| 38 | + */ |
|
| 39 | + public function __construct(ModuleInterface $module) |
|
| 40 | + { |
|
| 41 | + $this->hooks = new Collection(); |
|
| 42 | + $this->module = $module; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * {@inheritDoc} |
|
| 47 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
| 48 | - */ |
|
| 49 | - public function module(): ModuleInterface |
|
| 50 | - { |
|
| 51 | - return $this->module; |
|
| 52 | - } |
|
| 45 | + /** |
|
| 46 | + * {@inheritDoc} |
|
| 47 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
| 48 | + */ |
|
| 49 | + public function module(): ModuleInterface |
|
| 50 | + { |
|
| 51 | + return $this->module; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * {@inheritDoc} |
|
| 56 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name() |
|
| 57 | - */ |
|
| 58 | - public function name(): string |
|
| 59 | - { |
|
| 60 | - return $this->module->name() . '-' . |
|
| 61 | - mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * {@inheritDoc} |
|
| 56 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name() |
|
| 57 | + */ |
|
| 58 | + public function name(): string |
|
| 59 | + { |
|
| 60 | + return $this->module->name() . '-' . |
|
| 61 | + mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * {@inheritDoc} |
|
| 66 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title() |
|
| 67 | - */ |
|
| 68 | - abstract public function title(): string; |
|
| 64 | + /** |
|
| 65 | + * {@inheritDoc} |
|
| 66 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title() |
|
| 67 | + */ |
|
| 68 | + abstract public function title(): string; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritDoc} |
|
| 72 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description() |
|
| 73 | - */ |
|
| 74 | - abstract public function description(): string; |
|
| 70 | + /** |
|
| 71 | + * {@inheritDoc} |
|
| 72 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description() |
|
| 73 | + */ |
|
| 74 | + abstract public function description(): string; |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * {@inheritDoc} |
|
| 78 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface() |
|
| 79 | - */ |
|
| 80 | - abstract public function hookInterface(): string; |
|
| 76 | + /** |
|
| 77 | + * {@inheritDoc} |
|
| 78 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface() |
|
| 79 | + */ |
|
| 80 | + abstract public function hookInterface(): string; |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * {@inheritDoc} |
|
| 84 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register() |
|
| 85 | - */ |
|
| 86 | - public function register(HookInterface $hook_instance, int $order): void |
|
| 87 | - { |
|
| 88 | - if ($this->hooks->has($order)) { |
|
| 89 | - $this->hooks->splice($order + 1, 0, [$hook_instance]); |
|
| 90 | - } else { |
|
| 91 | - $this->hooks->put($order, $hook_instance); |
|
| 92 | - } |
|
| 93 | - } |
|
| 82 | + /** |
|
| 83 | + * {@inheritDoc} |
|
| 84 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register() |
|
| 85 | + */ |
|
| 86 | + public function register(HookInterface $hook_instance, int $order): void |
|
| 87 | + { |
|
| 88 | + if ($this->hooks->has($order)) { |
|
| 89 | + $this->hooks->splice($order + 1, 0, [$hook_instance]); |
|
| 90 | + } else { |
|
| 91 | + $this->hooks->put($order, $hook_instance); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * {@inheritDoc} |
|
| 97 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks() |
|
| 98 | - */ |
|
| 99 | - public function hooks(): Collection |
|
| 100 | - { |
|
| 101 | - return $this->hooks->sortKeys(); |
|
| 102 | - } |
|
| 95 | + /** |
|
| 96 | + * {@inheritDoc} |
|
| 97 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks() |
|
| 98 | + */ |
|
| 99 | + public function hooks(): Collection |
|
| 100 | + { |
|
| 101 | + return $this->hooks->sortKeys(); |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | */ |
| 58 | 58 | public function name(): string |
| 59 | 59 | { |
| 60 | - return $this->module->name() . '-' . |
|
| 60 | + return $this->module->name().'-'. |
|
| 61 | 61 | mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64); |
| 62 | 62 | } |
| 63 | 63 | |