-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the NotificationCleaner and do a little refactoring.
- Loading branch information
1 parent
10247be
commit c7a1dd6
Showing
4 changed files
with
295 additions
and
217 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
housekeeping/src/main/groovy/whelk/housekeeping/NotificationCleaner.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package whelk.housekeeping | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Log4j2 | ||
import whelk.Whelk | ||
|
||
import java.sql.Connection | ||
import java.sql.PreparedStatement | ||
import java.sql.ResultSet | ||
import java.sql.SQLType | ||
import java.sql.Timestamp | ||
import java.sql.Types | ||
import java.time.Instant | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoUnit | ||
|
||
@CompileStatic | ||
@Log4j2 | ||
class NotificationCleaner extends HouseKeeper { | ||
private static final int DAYS_TO_KEEP_INACTIVE = 14 | ||
private String status = "OK" | ||
private final Whelk whelk | ||
|
||
public NotificationCleaner(Whelk whelk) { | ||
this.whelk = whelk | ||
} | ||
|
||
public String getName() { | ||
return "Notifications cleaner" | ||
} | ||
|
||
public String getStatusDescription() { | ||
return status | ||
} | ||
|
||
public String getCronSchedule() { | ||
return "* * * * *" | ||
} | ||
|
||
public void trigger() { | ||
Connection connection | ||
PreparedStatement statement | ||
ResultSet resultSet | ||
|
||
connection = whelk.getStorage().getOuterConnection() | ||
connection.setAutoCommit(false) | ||
try { | ||
Timestamp from = Timestamp.from(Instant.now().minus(DAYS_TO_KEEP_INACTIVE, ChronoUnit.DAYS)) | ||
|
||
Set<String> typesToClean = whelk.getJsonld().getSubClasses("AdministrativeNotice") | ||
typesToClean.add("AdministrativeNotice") | ||
|
||
String sql = "SELECT id FROM lddb WHERE collection = 'none' AND data#>>'{@graph,1,@type}' = ANY (?) AND modified < ? AND created < ?;" | ||
connection.setAutoCommit(false) | ||
statement = connection.prepareStatement(sql) | ||
statement.setArray(1, connection.createArrayOf("TEXT", typesToClean as String[])) | ||
statement.setTimestamp(2, from) | ||
statement.setTimestamp(3, from) | ||
statement.setFetchSize(512) | ||
resultSet = statement.executeQuery() | ||
while (resultSet.next()) { | ||
String id = resultSet.getString("id") | ||
whelk.remove(id, "NotificationGenerator", "SEK") | ||
} | ||
} catch (Throwable e) { | ||
status = "Failed with:\n" + e + "\nat:\n" + e.getStackTrace().toString() | ||
throw e | ||
} finally { | ||
connection.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.