@@ -29,16 +29,14 @@ protected function configure()
29
29
->setName ('files:update_directory_max_size ' )
30
30
->setAliases (array ('fudms ' ))
31
31
->setDescription ('Increases the max disk space for all the courses reaching a certain threshold. ' )
32
- ->addOption (
32
+ ->addArgument (
33
33
'threshold ' ,
34
- null ,
35
- InputOption::VALUE_NONE ,
34
+ InputArgument::REQUIRED ,
36
35
'Sets the threshold, in %, above which a course size should be automatically increased '
37
36
)
38
- ->addOption (
39
- 'add-size ' ,
40
- null ,
41
- InputOption::VALUE_NONE ,
37
+ ->addArgument (
38
+ 'size ' ,
39
+ InputArgument::REQUIRED ,
42
40
'Number of MB to add to the max size of the course '
43
41
)
44
42
;
@@ -52,7 +50,9 @@ protected function configure()
52
50
protected function execute (InputInterface $ input , OutputInterface $ output )
53
51
{
54
52
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
56
56
if (empty ($ add )) {
57
57
$ add = 100 ;
58
58
}
@@ -62,66 +62,89 @@ protected function execute(InputInterface $input, OutputInterface $output)
62
62
return ;
63
63
}
64
64
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... ' );
71
73
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 ();
76
78
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
+ }
85
98
}
86
- }
87
99
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?)
118
141
}
119
- } else {
120
- //Quota is 0 (unlimited?)
121
142
}
122
143
}
123
144
}
145
+ $ output ->writeln ('Done increasing disk space ' );
146
+ } else {
147
+ $ output ->writeln ('The connection does not seem to be a valid PDO connection ' );
124
148
}
125
- $ output ->writeln ('Done increasing disk space ' );
126
149
}
127
150
}
0 commit comments