@@ -3,66 +3,79 @@ UPDATE GenericItem[processor=de.hybris.platform.commerceservices.impex.impl.Conf
33$sessionLanguage=$config-sanecleanup.jobs.sessionlanguage
44
55# @readme ProcessTaskLog
6- # We recommend customer to BusinessProcess cleanup, which will eventually take care of TaskLogs cleanup. There might be the few scenarios for ProcessTaskLog cleanUp
7- #1. The customer wants to keep the BusinessProcess for reporting, although we don't recommend it.
8- #1. The customer might be using the custom task without any business process.
9- #
10- # -- Queries is adjusted based MSSQL
11- # -- further adjustment required for other Database Type
6+ # We recommend customer to BusinessProcess cleanup, which will eventually take care of TaskLogs cleanup.
7+ # There might be the few scenarios for ProcessTaskLog cleanup:
8+ # 1. The customer wants to keep the BusinessProcess for reporting, although we don't recommend it.
9+ # 1. The customer might be using the custom task without any business process.
10+ # -- Query tested with MS SQL
11+ # -- Adjust the date calculation for
12+ # -- other database types
1213# SELECT
13- # COUNT({pk})
14+ # COUNT({l:pk}) AS "total",
15+ # MIN({l:modifiedtime}) AS "oldest",
16+ # MAX({l:modifiedtime}) AS "newest"
1417# FROM
15- # {ProcessTaskLog}
18+ # {ProcessTaskLog AS l }
1619# WHERE
17- # {creationTime} < DATEADD(
20+ # {l: creationTime} < DATEADD(
1821# month,
1922# -2,
20- # SYSDATETIME ()
23+ # GETUTCDATE ()
2124# )
22- #
23-
2425INSERT_UPDATE Script; code[unique=true]; description[default = $sessionLanguage]; content; active[default=true]
2526;directCleanupProcessTaskLogScript;"Delete ProcessTaskLog" ;"
26- import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
27- import de.hybris.platform.util.persistence.PersistenceUtils;
28- fss = spring.getBean('flexibleSearchService')
29- ms = spring.getBean('modelService')
27+ import de.hybris.platform.servicelayer.search.FlexibleSearchQuery
28+ import de.hybris.platform.util.persistence.PersistenceUtils
29+
30+ import java.time.Instant
31+ import java.time.ZoneId
32+ import java.time.temporal.ChronoUnit
33+
34+ def fss = spring.getBean('flexibleSearchService')
35+ def ms = spring.getBean('modelService')
3036
3137PersistenceUtils.doWithSLDPersistence({ ->
32- // -3 3 Month ADJUST
33- processTaskLogQuery = new FlexibleSearchQuery(/SELECT {pk} FROM {ProcessTaskLog} WHERE {creationTime} < DATEADD(month, -3, SYSDATETIME() ) /);
34- processTaskLogQuery.setCount(10000); // ADJUST TOTAL COUNT
35- processTaskLogCounts = fss.search(processTaskLogQuery).result
36- // out.println('ProcessTaskLog count: ' + processTaskLogCounts.size())
37- if (processTaskLogCounts.size() > 0) {
38- // out.println('Deleting ProcessTaskLog: ' + processTaskLogCounts.size())
39- try { // try bulk remove
40- ms.removeAll(processTaskLogCounts)
41- counter = processTaskLogCounts.size()
38+ // TODO: adjust retention period
39+ def retention = Date.from(Instant.now().atZone(ZoneId.systemDefault()).minus(3, ChronoUnit.MONTHS).toInstant())
40+ def processTaskLogQuery = new FlexibleSearchQuery('SELECT {pk} FROM {ProcessTaskLog} WHERE {creationTime} < ?CALC_RETENTION_TIME');
41+ processTaskLogQuery.addQueryParameter('CALC_RETENTION_TIME', retention)
42+ processTaskLogQuery.setCount(10000); // TODO: adjust total count
43+ def processTaskLogs = fss.search(processTaskLogQuery).result
44+ // out.println('ProcessTaskLog count: ' + processTaskLogs.size())
45+ if (processTaskLogs.size() > 0) {
46+ // out.println('Deleting ProcessTaskLog: ' + processTaskLogs.size())
47+ def counter
48+ try { // try bulk remove
49+ ms.removeAll(processTaskLogs)
50+ counter = processTaskLogs.size()
51+ processTaskLogs.each {
52+ ms.detach(it)
53+ }
4254
43- } catch (Exception bulkEx) { // if exception do individual remove
44- // out.println('Exception: ' + bulkEx.getMessage())
45- counter = 0
46- for (processTaskLogCount in processTaskLogCounts) {
47- try{
48- ms.remove(processTaskLogCount)
49- counter++
50- // out.println(counter + ' | Deleted - ProcessTaskLog: ' + processTaskLogCount?.pk + ' - ' + processTaskLogCount?.actionId + ' - ' + processTaskLogCount?.process)
51- } catch (Exception indEx) {
52- // out.println('Exception: ' + indEx.getMessage())
55+ } catch (Exception bulkEx) { // if exception do individual remove
56+ // out.println('Exception: ' + bulkEx.getMessage())
57+ counter = 0
58+ for (taskLog in processTaskLogs) {
59+ try {
60+ ms.remove(taskLog)
61+ ms.detach(taskLog)
62+ counter++
63+ // out.println(counter + ' | Deleted - ProcessTaskLog: ' + taskLog?.pk + ' - ' + taskLog?.actionId + ' - ' + taskLog?.process)
64+ } catch (Exception indEx) {
65+ // out.println('Exception: ' + indEx.getMessage())
66+ }
5367 }
5468 }
55- }
56- // out.println('Deleted ProcessTaskLog: ' + counter)
57- // processTaskLogQuery.setCount(-1);
58- // processTaskLogCounts = fss.search(processTaskLogQuery).result
59- // return [deleted: counter, remaining: processTaskLogCounts.size()]
60- return [deleted: counter]
69+ // out.println('Deleted ProcessTaskLog: ' + counter)
70+ // processTaskLogQuery.setCount(-1);
71+ // processTaskLogs = fss.search(processTaskLogQuery).result
72+ // return [deleted: counter, remaining: processTaskLogs.size()]
73+ return [deleted: counter]
6174
62- } else {
63- // out.println('NO ProcessTaskLog to delete.')
64- return [deleted: 0, remaining: 0]
65- }
75+ } else {
76+ // out.println('NO ProcessTaskLog to delete.')
77+ return [deleted: 0, remaining: 0]
78+ }
6679});
6780";
6881
@@ -72,6 +85,6 @@ INSERT_UPDATE ScriptingJob;code[unique = true];scriptURI
7285INSERT_UPDATE CronJob;code[unique=true]; job(code); sessionLanguage(isoCode)[default = $sessionLanguage];
7386;directCleanupProcessTaskLogCronjob;directCleanupProcessTaskLogScriptJob ;
7487
75- # runs every 40 mins ADJUST
76- INSERT_UPDATE Trigger; cronjob(code)[unique = true]; cronExpression; active
88+ # every 40 minutes
89+ INSERT Trigger; cronjob(code)[unique = true]; cronExpression; active
7790;directCleanupProcessTaskLogCronjob ;0 0/40 * ? * * * ;true
0 commit comments