Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_management.php
1 <?php
24 error_reporting(E_ALL);
25 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
26 
27 if ((php_sapi_name() != 'cli')) {
28  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
29 }
30 
31 if (count($_SERVER['argv']) < 2) {
32  echo "USAGE : php hipo_management.php MATRIX_ROOT [-remove_all_jobs]\n";
33  exit();
34 }
35 
36 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
37 if (empty($SYSTEM_ROOT)) {
38  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
39  exit();
40 }
41 
42 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
43  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
44  exit();
45 }
46 
47 $truncate_table = FALSE;
48 if (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == '-remove_all_jobs') {
49  $truncate_table = TRUE;
50 } else if (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] != '-remove_all_jobs') {
51  echo "USAGE : php hipo_management.php MATRIX_ROOT [-remove_all_jobs]\n";
52  exit();
53 }
54 
55 require_once $SYSTEM_ROOT.'/core/include/init.inc';
56 require_once SQ_FUDGE_PATH.'/general/datetime.inc';
57 
58 $am = $GLOBALS['SQ_SYSTEM']->am;
59 $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
60 
61 $root_user = $am->getSystemAsset('root_user');
62 $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
63 
64 $source_jobs = Array();
65 $now = time();
66 
67 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db3');
68 $db = MatrixDAL::getDb();
69 $sql = 'SELECT source_code_name, code_name, job_type
70  FROM sq_hipo_job';
71 try {
72  $query = MatrixDAL::preparePdoQuery($sql);
73  $results = MatrixDAL::executePdoAssoc($query);
74 } catch (Exception $e) {
75  throw new Exception('Unable to get HIPO jobs due to database error: '.$e->getMessage());
76 }
77 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
78 
79 if ($truncate_table) {
80  echo "Found ".count($results)." jobs.";
81  if (count($results) > 0) {
82  echo "\nMake sure no HIPO jobs are running currently. Selecting 'yes' will remove those HIPO jobs too.\n";
83  echo "Are you sure you want to continue removing all HIPO jobs? (y/n)\n";
84  $wish = rtrim(fgets(STDIN, 4094));
85 
86  if (strtolower($wish) == 'y') {
87  echo "Now truncating Hipo job table\t";
88  $sql = 'TRUNCATE TABLE sq_hipo_job';
89  try {
90  $ok = MatrixDAL::executeSql($sql);
91  if ($ok !== FALSE) {
92  echo "[ OK ]\n";
93  } else {
94  echo "[ FAILED ]\n";
95  }
96  }catch (Exception $e) {
97  throw new Exception('Unable to truncate table due to : '.$e->getMessage());
98  }
99  }
100  } else {
101  echo "\n";
102  }
103  exit();
104 }
105 // Filter out dependants
106 foreach ($results as $result) {
107  $source_name = array_get_index($result, 'source_code_name', '');
108  $job_name = array_get_index($result, 'code_name', '');
109 
110  if (!empty($job_name) && $job_name == $source_name) {
111  $source_jobs[] = $result;
112  }//end if
113 }//end foreach
114 
115 if (empty($source_jobs)) {
116  echo translate('hipo_currently_no_jobs')."\n";
117  exit;
118 }//end if
119 
120 foreach ($source_jobs as $index => $job) {
121  $source_code_name = array_get_index($job, 'source_code_name', '');
122  $source_job_type = array_get_index($job, 'job_type', '');
123  if (empty($source_code_name)) continue;
124  $source_job = $hh->getJob($source_code_name);
125  if (is_null($source_job)) continue;
126  echo ($index+1).': ';
127  echo ucwords(str_replace('_', ' ', $source_job_type));
128  echo '( '.$source_job->percentDone().'% )';
129  echo "\tLast Updated: ".easy_time_total($source_job->last_updated - $now, TRUE)."\n";
130  unset($source_job);
131 }//end foreach
132 
133 echo 'Enter the number of the job to change: (Press q to quit)';
134 $choice = rtrim(fgets(STDIN, 4094));
135 
136 if (strtolower($choice) == 'q') exit;
137 $actual_choice = ($choice-1);
138 if (!isset($source_jobs[$actual_choice])) {
139  echo "Incorrect entry\n";
140  exit;
141 }//end if
142 
143 echo "Options\n\tr - resume\n\tk - kill\n\tq - quit\nChoice:";
144 $action = rtrim(fgets(STDIN, 4094));
145 $action = strtolower($action);
146 
147 $action = (string) $action;
148 switch ($action) {
149  case 'r':
150  // Recover/resume
151  $source_code_name = array_get_index($source_jobs[$actual_choice], 'source_code_name', '');
152  $source_job_type = array_get_index($source_jobs[$actual_choice], 'job_type', '');
153  if (empty($source_code_name)) exit;
154  $source_job = $hh->getJob($source_code_name);
155  if (is_null($source_job)) exit;
156 
157  echo 'Resuming HIPO Job ';
158  echo ucwords(str_replace('_', ' ', $source_job_type))."\t";
159 
160  // Do itttttt
161  $status = $source_job->process();
162  if ($status) {
163  echo '[ OK ]';
164  } else {
165  echo '[ !! ]';
166  }//end if
167  echo "\n";
168  break;
169 
170  case 'k':
171  // Kill kill kill
172  $source_code_name = array_get_index($source_jobs[$actual_choice], 'source_code_name', '');
173  $source_job_type = array_get_index($source_jobs[$actual_choice], 'job_type', '');
174  if (empty($source_code_name)) exit;
175  $source_job = $hh->getJob($source_code_name);
176  if (is_null($source_job)) exit;
177 
178  echo 'Aborting HIPO Job ';
179  echo ucwords(str_replace('_', ' ', $source_job_type))."\n";
180  $source_job->abort();
181  break;
182 
183  case 'q':
184  default:
185  exit;
186 }//end switch
187 
188 ?>