24 #include "maintagent.h" 38 PGresult *
PQexecCheck(
int exitNumber,
char *
SQL,
char *file,
const int line)
41 result = PQexec(
pgConn, SQL);
67 long startTime, endTime;
68 char *
SQL =
"VACUUM ANALYZE";
70 startTime = (long)time(0);
75 endTime = (long)time(0);
76 printf(
"Vacuum Analyze took %ld seconds\n", endTime-startTime);
92 char *invalidUploadRefs =
"DELETE FROM foldercontents WHERE foldercontents_mode = 2 AND child_id NOT IN (SELECT upload_pk FROM upload)";
93 char *invalidUploadtreeRefs =
"DELETE FROM foldercontents WHERE foldercontents_mode = 4 AND child_id NOT IN (SELECT uploadtree_pk FROM uploadtree)";
94 char *unRefFolders =
"DELETE FROM folder WHERE folder_pk \ 95 NOT IN (SELECT child_id FROM foldercontents WHERE foldercontents_mode = 1) AND folder_pk != '1'";
96 long startTime, endTime;
98 startTime = (long)time(0);
101 result =
PQexecCheck(-120, invalidUploadRefs, __FILE__, __LINE__);
102 countTuples = PQcmdTuples(result);
104 printf(
"%s Invalid folder upload References\n", countTuples);
107 result =
PQexecCheck(-121, invalidUploadtreeRefs, __FILE__, __LINE__);
108 countTuples = PQcmdTuples(result);
110 printf(
"%s Invalid folder uploadtree References\n", countTuples);
113 result =
PQexecCheck(-122, unRefFolders, __FILE__, __LINE__);
114 countTuples = PQcmdTuples(result);
116 printf(
"%s unreferenced folders\n", countTuples);
118 endTime = (long)time(0);
119 printf(
"Validate folders took %ld seconds\n", endTime-startTime);
156 LOG_NOTICE(
"Verify file permissions is not implemented yet");
171 long startTime, endTime;
173 char *
SQL =
"DELETE FROM upload WHERE upload_pk \ 174 IN (SELECT upload_fk FROM uploadtree WHERE parent IS NULL AND pfile_fk IS NULL) \ 175 OR (upload_pk NOT IN (SELECT upload_fk FROM uploadtree) \ 176 AND (expire_action IS NULL OR expire_action != 'd') AND pfile_fk IS NOT NULL)";
178 startTime = (long)time(0);
180 result =
PQexecCheck(-130, SQL, __FILE__, __LINE__);
181 countTuples = PQcmdTuples(result);
184 endTime = (long)time(0);
185 printf(
"%s Uploads with no pfiles (%ld seconds)\n", countTuples, endTime-startTime);
200 int droppedCount = 0;
202 long startTime, endTime;
204 char *
SQL =
"SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'" 205 "AND table_schema = 'public' AND (table_name SIMILAR TO '^metaanalysis_[[:digit:]]+$'" 206 "OR table_name SIMILAR TO '^delup_%');";
208 startTime = (long)time(0);
210 result = PQexec(
pgConn, SQL);
212 countTuples = PQntuples(result);
214 for (row = 0; row < countTuples; row++) {
215 snprintf(SQLBuf, MAXSQL,
"DROP TABLE %s", PQgetvalue(result, row, 0));
221 endTime = (long)time(0);
222 printf(
"%d Orphaned temp tables were dropped (%ld seconds)\n", droppedCount, endTime-startTime);
245 LOG_NOTICE(
"Process expired uploads is not implemented yet");
270 LOG_NOTICE(
"Remove orphaned files from the repository is not implemented yet");
294 LOG_NOTICE(
"Remove orphaned gold files from the repository is not implemented yet");
306 long startTime, endTime;
308 char *SQL1 =
"CREATE TEMPORARY TABLE tmp_upload_prio(ordprio serial, uploadid int, groupid int)";
309 char *SQL2 =
"INSERT INTO tmp_upload_prio (uploadid, groupid) (SELECT upload_fk uploadid, group_fk groupid FROM upload_clearing ORDER BY priority ASC);";
310 char *SQL3 =
"UPDATE upload_clearing SET priority = ordprio FROM tmp_upload_prio WHERE uploadid=upload_fk AND group_fk=groupid;";
312 startTime = (long)time(0);
318 endTime = (long)time(0);
319 printf(
"Normalized upload priorities (%ld seconds)\n", endTime-startTime);
333 long startTime, endTime;
334 char *
SQL=
"SELECT table_catalog FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = 'public' AND (table_name SIMILAR TO 'upload%') LIMIT 1";
336 startTime = (long)time(0);
338 result = PQexec(
pgConn, SQL);
341 memset(SQLBuf,
'\0',
sizeof(SQLBuf));
342 snprintf(SQLBuf,
sizeof(SQLBuf),
"REINDEX DATABASE %s;", PQgetvalue(result, 0, 0));
346 endTime = (long)time(0);
347 printf(
"Time taken for reindexing the database : %ld seconds\n", endTime-startTime);
361 long startTime, endTime;
363 char *SQL1 =
"DELETE FROM uploadtree UT " 364 " WHERE NOT EXISTS ( " 367 " WHERE UT.upload_fk = U.upload_pk " 370 char *SQL2 =
"DELETE FROM clearing_decision AS CD " 371 " WHERE NOT EXISTS ( " 373 " FROM uploadtree UT " 374 " WHERE CD.uploadtree_fk = UT.uploadtree_pk " 375 " ) AND CD.scope = '0';";
377 char *SQL3 =
"DELETE FROM clearing_event CE " 378 " WHERE NOT EXISTS ( " 380 " FROM uploadtree UT " 381 " WHERE CE.uploadtree_fk = UT.uploadtree_pk " 382 " ) AND NOT EXISTS ( " 384 " FROM clearing_decision CD" 385 " WHERE CD.uploadtree_fk = CE.uploadtree_fk " 386 " AND CD.scope = '1'" 389 char *SQL4 =
"DELETE FROM clearing_decision_event CDE" 390 " WHERE NOT EXISTS ( " 392 " FROM clearing_event CE " 393 " WHERE CE.clearing_event_pk = CDE.clearing_event_fk " 396 char *SQL5 =
"DELETE FROM obligation_map OM " 397 " WHERE NOT EXISTS ( " 399 " FROM license_ref LR " 400 " WHERE OM.rf_fk = LR.rf_pk " 403 char *SQL6 =
"DELETE FROM obligation_candidate_map OCM " 404 " WHERE NOT EXISTS ( " 406 " FROM license_ref LR " 407 " WHERE OCM.rf_fk = LR.rf_pk " 410 startTime = (long)time(0);
412 result =
PQexecCheck(-200, SQL1, __FILE__, __LINE__);
413 countTuples = PQcmdTuples(result);
415 printf(
"%s Orphaned records have been removed from uploadtree table\n", countTuples);
418 result =
PQexecCheck(-201, SQL2, __FILE__, __LINE__);
419 countTuples = PQcmdTuples(result);
421 printf(
"%s Orphaned records have been removed from clearing_decision table\n", countTuples);
424 result =
PQexecCheck(-202, SQL3, __FILE__, __LINE__);
425 countTuples = PQcmdTuples(result);
427 printf(
"%s Orphaned records have been removed from clearing_event table\n", countTuples);
430 result =
PQexecCheck(-203, SQL4, __FILE__, __LINE__);
431 countTuples = PQcmdTuples(result);
433 printf(
"%s Orphaned records have been removed from clearing_decision_event table\n", countTuples);
436 result =
PQexecCheck(-204, SQL5, __FILE__, __LINE__);
437 countTuples = PQcmdTuples(result);
439 printf(
"%s Orphaned records have been removed from obligation_map table\n", countTuples);
442 result =
PQexecCheck(-205, SQL6, __FILE__, __LINE__);
443 countTuples = PQcmdTuples(result);
445 printf(
"%s Orphaned records have been removed from obligation_candidate_map table\n", countTuples);
448 endTime = (long)time(0);
450 printf(
"Time taken for removing orphaned rows from database : %ld seconds\n", endTime-startTime);
463 PGresult* updateResult;
466 int removedCount = 0;
469 long startTime, endTime;
473 char *
SQL =
"SELECT jq_pk, jq_log FROM job ja " 474 "INNER JOIN job jb ON ja.job_upload_fk = jb.job_upload_fk " 475 "INNER JOIN jobqueue jq ON jb.job_pk = jq.jq_job_fk " 476 "WHERE ja.job_name = 'Delete' AND jq_log IS NOT NULL " 477 "AND jq_log != 'removed';";
479 startTime = (long)time(0);
481 result = PQexec(
pgConn, SQL);
486 countTuples = PQntuples(result);
488 updateStatement = fo_dbManager_PrepareStamement(dbManager,
489 "updateRemovedLogFromJobqueue",
490 "UPDATE jobqueue SET jq_log = 'removed' WHERE jq_pk = $1;",
int);
492 for (row = 0; row < countTuples; row++)
494 fo_dbManager_begin(dbManager);
495 jobQueueId = atoi(PQgetvalue(result, row, 0));
496 logPath = PQgetvalue(result, row, 1);
497 if (stat(logPath, &statbuf) == -1)
499 LOG_NOTICE(
"Log file '%s' does not exists", logPath);
504 LOG_VERBOSE2(
"Removed file '%s'", logPath);
514 LOG_ERROR(
"Unable to update the value of jobqueue.jq_log to 'removed' " 515 "for jq_pk = %d", jobQueueId);
517 fo_dbManager_commit(dbManager);
521 endTime = (long)time(0);
522 printf(
"%d / %d Orphaned log files were removed " 523 "(%ld seconds)\n", removedCount, countTuples, endTime - startTime);
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
FUNCTION void removeOrphanedFiles()
Remove orphaned files from the repository (slow) Loop through each file in the repository and make su...
FUNCTION void removeTemps()
Remove orphaned temp tables from deprecated pkgmettagetta and old delagent.
FUNCTION void deleteOrphanGold()
Delete orphaned gold files from the repository Loop through each gold file in the repository and make...
FUNCTION void reIndexAllTables()
reindex of all indexes in fossology database
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
PGconn * pgConn
Database connection.
FUNCTION void vacAnalyze()
Do database vacuum and analyze.
FUNCTION void removeOrphanedRows()
remove orphaned rows from fossology database
FUNCTION void verifyFilePerms(int fix)
Verify and optionally fix file permissions.
FUNCTION void normalizeUploadPriorities()
Normalize priority of Uploads.
PGresult * PQexecCheck(int exitNumber, char *SQL, char *file, const int line)
simple wrapper which includes PQexec and fo_checkPQcommand
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
FUNCTION void processExpired()
Process expired uploads (slow)
char SQL[256]
SQL query to execute.
FUNCTION void validateFolders()
Validate folder and foldercontents tables.
fo_dbManager * dbManager
fo_dbManager object
FUNCTION void removeOrphanedLogFiles()
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
FUNCTION void removeUploads()
Remove Uploads with no pfiles.
FUNCTION void PQexecCheckClear(int exitNumber, char *SQL, char *file, const int line)
Execute SQL query and create the result and clear the result.
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return...