Skip to content

Commit 5c3b6e1

Browse files
committed
Fix file:update_directory_max_size command (remove mysql calls)
1 parent 9393f4e commit 5c3b6e1

File tree

1 file changed

+83
-60
lines changed

1 file changed

+83
-60
lines changed

src/Chash/Command/Files/UpdateDirectoryMaxSizeCommand.php

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ protected function configure()
2929
->setName('files:update_directory_max_size')
3030
->setAliases(array('fudms'))
3131
->setDescription('Increases the max disk space for all the courses reaching a certain threshold.')
32-
->addOption(
32+
->addArgument(
3333
'threshold',
34-
null,
35-
InputOption::VALUE_NONE,
34+
InputArgument::REQUIRED,
3635
'Sets the threshold, in %, above which a course size should be automatically increased'
3736
)
38-
->addOption(
39-
'add-size',
40-
null,
41-
InputOption::VALUE_NONE,
37+
->addArgument(
38+
'size',
39+
InputArgument::REQUIRED,
4240
'Number of MB to add to the max size of the course'
4341
)
4442
;
@@ -52,7 +50,9 @@ protected function configure()
5250
protected function execute(InputInterface $input, OutputInterface $output)
5351
{
5452
parent::execute($input, $output);
55-
$add = $input->getOption('add-size'); //1 if the option was set
53+
$conn = $this->getConnection($input);
54+
55+
$add = $input->getArgument('size'); //1 if the option was set
5656
if (empty($add)) {
5757
$add = 100;
5858
}
@@ -62,66 +62,89 @@ protected function execute(InputInterface $input, OutputInterface $output)
6262
return;
6363
}
6464

65-
$threshold = $input->getOption('threshold');
66-
if (empty($threshold)) {
67-
$threshold = 75;
68-
}
69-
$this->writeCommandHeader($output, 'Using threshold: '.$threshold);
70-
$this->writeCommandHeader($output, 'Checking courses dir...');
65+
if ($conn instanceof \Doctrine\DBAL\Connection) {
66+
67+
$threshold = $input->getArgument('threshold');
68+
if (empty($threshold)) {
69+
$threshold = 75;
70+
}
71+
$this->writeCommandHeader($output, 'Using threshold: '.$threshold);
72+
$this->writeCommandHeader($output, 'Checking courses dir...');
7173

72-
// Get database and path information
73-
$coursesPath = $this->getConfigurationHelper()->getSysPath();
74-
$connection = $this->getConnection($input);
75-
$_configuration = $this->getConfigurationHelper()->getConfiguration();
74+
// Get database and path information
75+
$coursesPath = $this->getConfigurationHelper()->getSysPath();
76+
$connection = $this->getConnection($input);
77+
$_configuration = $this->getConfigurationHelper()->getConfiguration();
7678

77-
$courseTable = $_configuration['main_database'].'.course';
78-
$globalCourses = array();
79-
$sql = "SELECT c.id as cid, c.code as ccode, c.directory as cdir, c.disk_quota as cquota
80-
FROM $courseTable c";
81-
$res = mysql_query($sql);
82-
if ($res && mysql_num_rows($res) > 0) {
83-
while ($row = mysql_fetch_assoc($res)) {
84-
$globalCourses[$row['cdir']] = array('id' => $row['cid'], 'code' => $row['ccode'], 'quota' => $row['cquota']);
79+
$courseTable = $_configuration['main_database'].'.course';
80+
$globalCourses = array();
81+
$sql = "SELECT c.id as cid, c.code as ccode, c.directory as cdir, c.disk_quota as cquota
82+
FROM $courseTable c";
83+
try {
84+
$stmt = $conn->prepare($sql);
85+
$stmt->execute();
86+
} catch (\PDOException $e) {
87+
$output->write('SQL error!'.PHP_EOL);
88+
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
89+
}
90+
if ($stmt->rowCount() > 0) {
91+
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
92+
$globalCourses[$row['cdir']] = array(
93+
'id' => $row['cid'],
94+
'code' => $row['ccode'],
95+
'quota' => $row['cquota']
96+
);
97+
}
8598
}
86-
}
8799

88-
$dirs = $this->getConfigurationHelper()->getDataFolders();
89-
if (count($dirs) > 0) {
90-
foreach ($dirs as $dir) {
91-
$file = $dir->getFileName();
92-
$res = exec('du -s '.$dir->getRealPath()); // results are returned in KB (under Linux)
93-
$res = preg_split('/\s/',$res);
94-
$size = round($res[0]/1024,1); // $size is stored in MB
95-
if (isset($globalCourses[$file]['code'])) {
96-
$code = $globalCourses[$file]['code'];
97-
$quota = round($globalCourses[$file]['quota']/(1024*1024), 0); //quota is originally in Bytes in DB. Store in MB
98-
$rate = '-';
99-
if ($quota > 0) {
100-
$newAllowedSize = $quota;
101-
$rate = round(($size/$newAllowedSize)*100, 0); //rate is a percentage of disk use vs allowed quota, in MB
102-
$increase = false;
103-
while ($rate > $threshold) { // Typically 80 > 75 -> increase quota
104-
//$output->writeln('...Rate '.$rate.' is larger than '.$threshold.', so increase allowed size');
105-
// Current disk usage goes beyond threshold. Increase allowed size by 100MB
106-
$newAllowedSize += $add;
107-
//$output->writeln('....New allowed size is '.$newAllowedSize);
108-
$rate = round(($size/$newAllowedSize)*100, 0);
109-
//$output->writeln('...Rate is now '.$rate);
110-
$increase = true;
111-
}
112-
$newAllowedSize = $newAllowedSize*1024*1024;
113-
//$output->writeln('Allowed size is '.$newAllowedSize.' Bytes, or '.round($newAllowedSize/(1024*1024)));
114-
$sql = "UPDATE $courseTable SET disk_quota = $newAllowedSize WHERE id = ".$globalCourses[$file]['id'];
115-
$res = mysql_query($sql);
116-
if ($increase) {
117-
$output->writeln('Increased max size of '.$globalCourses[$file]['code'].'('.$globalCourses[$file]['id'].') to '.$newAllowedSize);
100+
$dirs = $this->getConfigurationHelper()->getDataFolders();
101+
if (count($dirs) > 0) {
102+
foreach ($dirs as $dir) {
103+
$file = $dir->getFileName();
104+
$res = exec('du -s '.$dir->getRealPath()); // results are returned in KB (under Linux)
105+
$res = preg_split('/\s/', $res);
106+
$size = round($res[0] / 1024, 1); // $size is stored in MB
107+
if (isset($globalCourses[$file]['code'])) {
108+
$code = $globalCourses[$file]['code'];
109+
$quota = round($globalCourses[$file]['quota'] / (1024 * 1024),
110+
0); //quota is originally in Bytes in DB. Store in MB
111+
$rate = '-';
112+
if ($quota > 0) {
113+
$newAllowedSize = $quota;
114+
$rate = round(($size / $newAllowedSize) * 100,
115+
0); //rate is a percentage of disk use vs allowed quota, in MB
116+
$increase = false;
117+
while ($rate > $threshold) { // Typically 80 > 75 -> increase quota
118+
//$output->writeln('...Rate '.$rate.' is larger than '.$threshold.', so increase allowed size');
119+
// Current disk usage goes beyond threshold. Increase allowed size by 100MB
120+
$newAllowedSize += $add;
121+
//$output->writeln('....New allowed size is '.$newAllowedSize);
122+
$rate = round(($size / $newAllowedSize) * 100, 0);
123+
//$output->writeln('...Rate is now '.$rate);
124+
$increase = true;
125+
}
126+
$newAllowedSize = $newAllowedSize * 1024 * 1024;
127+
//$output->writeln('Allowed size is '.$newAllowedSize.' Bytes, or '.round($newAllowedSize/(1024*1024)));
128+
$sql = "UPDATE $courseTable SET disk_quota = $newAllowedSize WHERE id = ".$globalCourses[$file]['id'];
129+
try {
130+
$stmt2 = $conn->prepare($sql);
131+
$stmt2->execute();
132+
} catch (\PDOException $e) {
133+
$output->write('SQL error!'.PHP_EOL);
134+
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
135+
}
136+
if ($increase) {
137+
$output->writeln('Increased max size of '.$globalCourses[$file]['code'].'('.$globalCourses[$file]['id'].') to '.$newAllowedSize);
138+
}
139+
} else {
140+
//Quota is 0 (unlimited?)
118141
}
119-
} else {
120-
//Quota is 0 (unlimited?)
121142
}
122143
}
123144
}
145+
$output->writeln('Done increasing disk space');
146+
} else {
147+
$output->writeln('The connection does not seem to be a valid PDO connection');
124148
}
125-
$output->writeln('Done increasing disk space');
126149
}
127150
}

0 commit comments

Comments
 (0)