Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 545cc85

Browse files
author
Jamie Snape
committed
Migrate cleanup module settings to database
1 parent 4cedf63 commit 545cc85

File tree

12 files changed

+321
-228
lines changed

12 files changed

+321
-228
lines changed

modules/cleanup/configs/module.ini

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1+
; MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0.
2+
13
[global]
2-
; version of the module
3-
version = 1.0.0
4-
; full name
5-
fullname = Cleanup Module
6-
; description
7-
description = "Schedules tasks to perform periodic cleanup operations on the database and filesystem"
8-
; category
9-
category = Core
10-
; dependencies
4+
fullname = "Cleanup"
5+
description = "Enable scheduled tasks to perform periodic cleanup operations on the database and filesystem"
6+
category = "Core"
117
dependencies = scheduler
12-
13-
days = "1"
8+
uuid = "c13d0c2a-5c25-4eab-8ae0-ed5aefe8dc1e"
9+
version = "1.1.0"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
define('CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY', 'days_to_keep_partial_files');
22+
define('CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_DEFAULT_VALUE', 1);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/**
22+
* Admin controller for the cleanup module.
23+
*
24+
* @property Scheduler_JobModel $Scheduler_Job
25+
*/
26+
class Cleanup_AdminController extends Cleanup_AppController
27+
{
28+
/** @var array */
29+
public $_components = array('Json', 'Utility');
30+
31+
/** @var array */
32+
public $_models = array('Setting');
33+
34+
/** @var array */
35+
public $_moduleComponents = array('Admin');
36+
37+
/** Index action */
38+
public function indexAction()
39+
{
40+
$this->requireAdminPrivileges();
41+
42+
$this->view->pageTitle = 'Cleanup Module Configuration';
43+
$form = new Cleanup_Form_Admin();
44+
45+
if ($this->getRequest()->isPost()) {
46+
$data = $this->getRequest()->getPost();
47+
48+
if ($form->isValid($data)) {
49+
$values = $form->getValues();
50+
51+
foreach ($values as $key => $value) {
52+
if ($value !== null) {
53+
$this->Setting->setConfig($key, $value, $this->moduleName);
54+
}
55+
}
56+
57+
$this->ModuleComponent->Admin->schedulePerformCleanupJob(
58+
$values[CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY],
59+
UtilityComponent::getTempDirectory(),
60+
$this->userSession->Dao
61+
);
62+
}
63+
64+
$form->populate($data);
65+
} else {
66+
$elements = $form->getElements();
67+
68+
foreach ($elements as $element) {
69+
$name = $element->getName();
70+
71+
if ($name !== 'csrf' && $name !== 'submit') {
72+
$value = $this->Setting->getValueByName($name, $this->moduleName);
73+
74+
if (!is_null($value)) {
75+
$form->setDefault($name, $value);
76+
}
77+
}
78+
}
79+
}
80+
81+
$this->view->form = $form;
82+
session_start();
83+
}
84+
}

modules/cleanup/controllers/ConfigController.php

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Admin component for the cleanup module. */
22+
class Cleanup_AdminComponent extends AppComponent
23+
{
24+
/** @var string */
25+
public $moduleName = 'cleanup';
26+
27+
/**
28+
* Schedule the perform cleanup job.
29+
*
30+
* @param int $days days to keep partial files
31+
* @param string $tempDirectory temporary directory
32+
* @param null|UserDao $userDao user scheduling the job
33+
*/
34+
public function schedulePerformCleanupJob($days, $tempDirectory, $userDao = null)
35+
{
36+
/** @var Scheduler_JobModel $jobModel */
37+
$jobModel = MidasLoader::loadModel('Job', 'scheduler');
38+
$jobDaos = $jobModel->getJobsByTask('TASK_CLEANUP_PERFORM_CLEANUP');
39+
$cleanupJobDao = false;
40+
41+
foreach ($jobDaos as $jobDao) {
42+
if ($jobDao->getTask() === 'TASK_CLEANUP_PERFORM_CLEANUP') {
43+
$cleanupJobDao = $jobDao;
44+
break;
45+
}
46+
}
47+
48+
if ($cleanupJobDao === false) {
49+
/** @var Scheduler_JobDao $cleanupJobDao */
50+
$cleanupJobDao = MidasLoader::newDao('JobDao', 'scheduler');
51+
$cleanupJobDao->setTask('TASK_CLEANUP_PERFORM_CLEANUP');
52+
$cleanupJobDao->setPriority(1);
53+
$cleanupJobDao->setRunOnlyOnce(0);
54+
$cleanupJobDao->setFireTime(date('Y-m-d', strtotime('+1 day'.date('Y-m-d H:i:s'))).' 01:00:00');
55+
$cleanupJobDao->setTimeInterval(86400);
56+
$cleanupJobDao->setStatus(SCHEDULER_JOB_STATUS_TORUN);
57+
58+
if (!is_null($userDao)) {
59+
$cleanupJobDao->setCreatorId($userDao->getKey());
60+
}
61+
}
62+
63+
$cleanupJobDao->setParams(JsonComponent::encode(array('days' => $days, 'tempDirectory' => $tempDirectory)));
64+
65+
$jobModel->save($cleanupJobDao);
66+
}
67+
}

modules/cleanup/controllers/forms/ConfigForm.php renamed to modules/cleanup/database/InstallScript.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,19 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21-
/** Cleanup module configuration form */
22-
class Cleanup_ConfigForm extends AppForm
23-
{
24-
/** create form */
25-
public function createConfigForm()
26-
{
27-
$form = new Zend_Form();
28-
29-
$form->setAction($this->webroot.'/cleanup/config/index')->setMethod('post');
30-
31-
$olderThan = new Zend_Form_Element_Text('olderThan');
21+
require_once BASE_PATH.'/modules/cleanup/constant/module.php';
3222

33-
$submit = new Zend_Form_Element_Submit('submitConfig');
34-
$submit->setLabel('Save configuration');
35-
36-
$form->addElements(array($olderThan, $submit));
23+
/** Install the cleanup module. */
24+
class Cleanup_InstallScript extends MIDASModuleInstallScript
25+
{
26+
/** @var string */
27+
public $moduleName = 'cleanup';
3728

38-
return $form;
29+
/** Post database install. */
30+
public function postInstall()
31+
{
32+
/** @var SettingModel $settingModel */
33+
$settingModel = MidasLoader::loadModel('Setting');
34+
$settingModel->setConfig(CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY, CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_DEFAULT_VALUE, $this->moduleName);
3935
}
4036
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Upgrade the cleanup module to version 1.1.0. */
22+
class Cleanup_Upgrade_1_1_0 extends MIDASUpgrade
23+
{
24+
/** @var string */
25+
public $moduleName = 'cleanup';
26+
27+
/** Post database upgrade. */
28+
public function postUpgrade()
29+
{
30+
/** @var SettingModel $settingModel */
31+
$settingModel = MidasLoader::loadModel('Setting');
32+
$configPath = LOCAL_CONFIGS_PATH.DIRECTORY_SEPARATOR.$this->moduleName.'.local.ini';
33+
34+
if (file_exists($configPath)) {
35+
$config = new Zend_Config_Ini($configPath, 'global');
36+
$settingModel->setConfig(CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY, $config->get('days', CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_DEFAULT_VALUE), $this->moduleName);
37+
38+
$config = new Zend_Config_Ini($configPath, null, true);
39+
unset($config->global->days);
40+
41+
$writer = new Zend_Config_Writer_Ini();
42+
$writer->setConfig($config);
43+
$writer->setFilename($configPath);
44+
$writer->write();
45+
} else {
46+
$settingModel->setConfig(CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY, CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_DEFAULT_VALUE, $this->moduleName);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)