-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcfbot_gc.py
More file actions
150 lines (141 loc) · 4.52 KB
/
cfbot_gc.py
File metadata and controls
150 lines (141 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
import cfbot_config
import cfbot_util
import logging
def gc(conn):
cursor = conn.cursor()
cursor.execute("""set work_mem = '256MB'""")
# Delete large objects to free up disk space. These should have been
# ingested by the highlights system after a short time (though we have no
# state machine to track that).
#
# XXX In fact we don't really need the artifact and task_command rows at
# all, so we could perhaps trim them much sooner?
cursor.execute(
"""
update artifact
set body = null
from task
where artifact.task_id = task.task_id
and artifact.body is not null
and task.created < now() - interval '1 days' * %s
""",
(cfbot_config.RETENTION_LARGE_OBJECTS,),
)
logging.info("garbage collected %d artifact bodies", cursor.rowcount)
cursor.execute(
"""
update task_command
set log = null
from task
where task_command.task_id = task.task_id
and task_command.log is not null
and task.created < now() - interval '1 days' * %s
""",
(cfbot_config.RETENTION_LARGE_OBJECTS,),
)
logging.info("garbage collected %d task_command logs", cursor.rowcount)
conn.commit()
# Trim old builds and dependent data in referential integrity order.
cursor.execute(
"""
delete from artifact
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from test
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from task_command
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from task_command
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from highlight
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from task_status_history
where task_id in (select task_id
from task join build using (build_id)
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from task
where build_id in (select build_id
from build
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from branch
where build_id in (select build_id
from build
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from build_status_history
where build_id in (select build_id
from build
where build.created < now() - interval '1 day' * %s)""",
(cfbot_config.RETENTION_ALL,),
)
cursor.execute(
"""
delete from build
where created < now() - interval '1 day' * %s""",
(cfbot_config.RETENTION_ALL,),
)
logging.info(
"garbage collected %d builds older than %d days",
cursor.rowcount,
cfbot_config.RETENTION_ALL,
)
conn.commit()
# Trim old branches that don't have an associated build (legacy or they
# failed to apply and never got a build).
cursor.execute(
"""
delete from branch
where build_id is null
and created < now() - interval '1 day' * %s""",
(cfbot_config.RETENTION_ALL,),
)
logging.info(
"garbage collected %d branches with no build older than %d days",
cursor.rowcount,
cfbot_config.RETENTION_ALL,
)
conn.commit()
if __name__ == "__main__":
with cfbot_util.db() as conn:
gc(conn)