|
| 1 | +import time |
| 2 | + |
| 3 | +from pyinfraboxutils import get_logger, get_env |
| 4 | +from pyinfraboxutils import dbpool |
| 5 | +from pyinfraboxutils.storage import storage |
| 6 | + |
| 7 | +logger = get_logger("gc") |
| 8 | + |
| 9 | +class GC(object): |
| 10 | + def run(self): |
| 11 | + # TODO: Delete storage objects: uploads, outputs |
| 12 | + # TODO: Delete images from registry |
| 13 | + |
| 14 | + while True: |
| 15 | + db = dbpool.get() |
| 16 | + try: |
| 17 | + logger.info('Starting next GC run') |
| 18 | + self._gc(db) |
| 19 | + logger.info('Finished GC run') |
| 20 | + logger.info('') |
| 21 | + except Exception as e: |
| 22 | + logger.exception(e) |
| 23 | + finally: |
| 24 | + dbpool.put(db) |
| 25 | + |
| 26 | + time.sleep(3600) |
| 27 | + |
| 28 | + def _gc(self, db): |
| 29 | + self._gc_job_console_output(db) |
| 30 | + self._gc_job_output(db) |
| 31 | + self._gc_test_runs(db) |
| 32 | + self._gc_orphaned_projects(db) |
| 33 | + self._gc_storage_job_cache(db) |
| 34 | + |
| 35 | + def _gc_job_console_output(self, db): |
| 36 | + # Delete the console output of jobs |
| 37 | + # which are older than 30 days |
| 38 | + r = db.execute_one_dict(''' |
| 39 | + SELECT count(*) as count |
| 40 | + FROM job |
| 41 | + WHERE created_at < NOW() - INTERVAL '30 days' |
| 42 | + AND console != 'deleted' |
| 43 | + ''') |
| 44 | + |
| 45 | + logger.info('Deleting console output of %s jobs', r['count']) |
| 46 | + |
| 47 | + r = db.execute(''' |
| 48 | + UPDATE job |
| 49 | + SET console = 'deleted' |
| 50 | + WHERE created_at < NOW() - INTERVAL '30 days' |
| 51 | + AND console != 'deleted' |
| 52 | + ''') |
| 53 | + |
| 54 | + db.commit() |
| 55 | + |
| 56 | + def _gc_test_runs(self, db): |
| 57 | + # Delete the test_runs |
| 58 | + # which are older than 30 days |
| 59 | + r = db.execute_one_dict(''' |
| 60 | + SELECT count(*) as count |
| 61 | + FROM test_run |
| 62 | + WHERE timestamp < NOW() - INTERVAL '14 days' |
| 63 | + ''') |
| 64 | + |
| 65 | + logger.info('Deleting %s test_runs', r['count']) |
| 66 | + |
| 67 | + r = db.execute(''' |
| 68 | + DELETE |
| 69 | + FROM test_run |
| 70 | + WHERE timestamp < NOW() - INTERVAL '14 days' |
| 71 | + ''') |
| 72 | + |
| 73 | + db.commit() |
| 74 | + |
| 75 | + |
| 76 | + def _gc_job_output(self, db): |
| 77 | + # Delete orphaned entries in the console table |
| 78 | + # which are older than one day |
| 79 | + r = db.execute_one_dict(''' |
| 80 | + SELECT count(*) count |
| 81 | + FROM console |
| 82 | + WHERE date < NOW() - INTERVAL '1 day' |
| 83 | + ''') |
| 84 | + |
| 85 | + logger.info('Deleting %s orphaned console entries', r['count']) |
| 86 | + |
| 87 | + r = db.execute(''' |
| 88 | + DELETE |
| 89 | + FROM console |
| 90 | + WHERE date < NOW() - INTERVAL '1 day' |
| 91 | + ''') |
| 92 | + |
| 93 | + db.commit() |
| 94 | + |
| 95 | + def _gc_orphaned_projects(self, db): |
| 96 | + # All the orphaned rows after a |
| 97 | + # project has been deleted |
| 98 | + tables = [ |
| 99 | + 'auth_token', 'build', 'collaborator', 'commit', |
| 100 | + 'job', 'job_badge', 'job_markup', 'measurement', |
| 101 | + 'pull_request', 'repository', 'secret', 'source_upload', 'test', |
| 102 | + 'test_run' |
| 103 | + ] |
| 104 | + for t in tables: |
| 105 | + self._gc_table_content_of_deleted_project(db, t) |
| 106 | + |
| 107 | + def _gc_table_content_of_deleted_project(self, db, table): |
| 108 | + r = db.execute_one_dict(''' |
| 109 | + SELECT count(*) as count |
| 110 | + FROM %s |
| 111 | + WHERE NOT EXISTS ( |
| 112 | + SELECT project.id |
| 113 | + FROM project |
| 114 | + WHERE %s.project_id = project.id |
| 115 | + ) |
| 116 | + ''' % (table, table)) |
| 117 | + |
| 118 | + logger.info('Deleting %s orphaned rows from %s', r['count'], table) |
| 119 | + |
| 120 | + db.execute(''' |
| 121 | + DELETE |
| 122 | + FROM %s |
| 123 | + WHERE NOT EXISTS ( |
| 124 | + SELECT project.id |
| 125 | + FROM project |
| 126 | + WHERE %s.project_id = project.id |
| 127 | + ) |
| 128 | + ''' % (table, table)) |
| 129 | + |
| 130 | + db.commit() |
| 131 | + |
| 132 | + def _gc_storage_source_upload(self): |
| 133 | + pass |
| 134 | + |
| 135 | + def _gc_storage_job_cache(self, db): |
| 136 | + # Delete all cache of all jobs which have not |
| 137 | + # been executed in the last 7 days |
| 138 | + r = db.execute_many_dict(''' |
| 139 | + SELECT DISTINCT project_id, name |
| 140 | + FROM job |
| 141 | + WHERE |
| 142 | + created_at > NOW() - INTERVAL '14 days' |
| 143 | + EXCEPT |
| 144 | + SELECT DISTINCT project_id, name from job where created_at > NOW() - INTERVAL '7 days' |
| 145 | + ''') |
| 146 | + |
| 147 | + logger.info('Deleting caches of %s jobs', len(r)) |
| 148 | + |
| 149 | + for j in r: |
| 150 | + logger.info('Deleting cache %s/%s', j['project_id'], j['name']) |
| 151 | + key = 'project_%s_job_%s.tar.snappy' % (j['project_id'], j['name']) |
| 152 | + storage.delete_cache(key) |
| 153 | + |
| 154 | +def main(): |
| 155 | + get_env('INFRABOX_DATABASE_DB') |
| 156 | + get_env('INFRABOX_DATABASE_USER') |
| 157 | + get_env('INFRABOX_DATABASE_PASSWORD') |
| 158 | + get_env('INFRABOX_DATABASE_HOST') |
| 159 | + get_env('INFRABOX_DATABASE_PORT') |
| 160 | + |
| 161 | + gc = GC() |
| 162 | + gc.run() |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments