cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Cleanup CronjobHistory

Former Member
1,816

Hello all,

Cronjobs which runs many times a day create a huge amount of CronJobHistoryEntries. Is there a solution to clean them every day?

I tried to overwrite CleanupCronJobStrategy, but failed there.

Thanks in advance.

Cheers, Serdar

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

Thanks Markus,

I solved this now by overwriting createFetchQuery

     @Override
     public FlexibleSearchQuery createFetchQuery(final CronJobModel cjm)
     {
         final Map<String, Object> params = new HashMap<>();
         final StringBuilder builder = new StringBuilder();
 
         // @formatter:off
         builder.append("SELECT {" + CronJobModel.PK + "} FROM {" + CronJobModel._TYPECODE + " AS cj} WHERE ");
 
         builder.append(" {" + CronJobModel.PK   + "} IN" +
                 "({{" +
                     "SELECT" +
                         " {" + CronJobHistoryModel.CRONJOB + "} " +
                     "FROM" +
                         " {" + CronJobHistoryModel._TYPECODE + "} " +
                     "GROUP BY" +
                         " {" + CronJobHistoryModel.CRONJOB + "} " +
                     "Having" +
                         " count({" + CronJobHistoryModel.CRONJOB + "}) > 1" +
                 "}})");
 
         if (!excludedCronJobCodes.isEmpty())
         {
             builder.append(" AND {" + CronJobModel.CODE + "} NOT IN ( ?excludedCronJobCodes ) ");
             params.put("excludedCronJobCodes", excludedCronJobCodes);
         }
         // @formatter:on
 
         final FlexibleSearchQuery query = new FlexibleSearchQuery(builder.toString(), params);
         query.setResultClassList(Arrays.asList(CronJobModel.class));
         return query;
     }

To get this working properly it is necessary to change the settings in the backoffice for each cronjob from AND to OR

Cheers, Serdar

Answers (2)

Answers (2)

former_member633554
Active Participant
0 Likes

Groovy script can do it, create a scripting job

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
flexibleSearchService = spring.getBean "flexibleSearchService"
modelService = spring.getBean "modelService"
 
int count=300
FlexibleSearchQuery query = new FlexibleSearchQuery("select {pk} from {CronJobHistory} where  {creationtime} <  DATE_SUB(SYSDATE(), INTERVAL 3 DAY  ) ");
query.setCount(count)
 
cronjobhistoryentry = flexibleSearchService.search(query).getResult();
modelService.removeAll(cronjobhistoryentry);


mpern
Product and Topic Expert
Product and Topic Expert
0 Likes

At the moment, you have to write your own cleanup job / cleanup strategy as described here:

https://help.hybris.com/1811/hcd/8b9ba2218669101483e7f2ca38a2de96.html

It boils down to:

  1. craft a query to fetch all the history entries you want to delete

  2. delete them

You can even configure a new maintenance job using the scripting engine during runtime (this is also described in the link)