CleanUp Cronjob

 

CleanUp Cronjob

CleanupCronJobStrategy can be used  to clean up instances of the CronJob. This strategy is based on maintenance framework created for processing instances of any type. For more information, see cronjob service. Default settings for the cron job cleanup mechanism is to remove instances of the CronJob, which fulfil the following:

  • Has no trigger
  • Are successfully finished

Are older than 14 days

DM-04: Clean Up Cronjob

SAP CX Field Services recommends to declare and activate all clean up cronjob necessary to keep database platform in good shape.

Beware: the information here can be interesting for an audit trail. Make sure to backup the database and only cleanup if there is no need for these entries anymore.


#Clean Up Cronjob Reference: CleanupCronJobStrategy

INSERT_UPDATE Script; code[unique = true]       ; active[unique = true] ; scriptType(code); content

                    ; cleanupCronJobScript ; true                  ; GROOVY   ;"

import de.hybris.platform.core.model.media.MediaModel;

import de.hybris.platform.cronjob.enums.CronJobResult;

import de.hybris.platform.cronjob.enums.CronJobStatus;

import de.hybris.platform.cronjob.model.CronJobModel;

import de.hybris.platform.cronjob.model.TriggerModel;

import de.hybris.platform.servicelayer.config.ConfigurationService;

import de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;

import de.hybris.platform.servicelayer.cronjob.PerformResult;

import de.hybris.platform.servicelayer.exceptions.ModelRemovalException;

import de.hybris.platform.servicelayer.model.ModelService;

import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import de.hybris.platform.servicelayer.search.SearchResult;

import org.apache.commons.collections.CollectionUtils;

import org.apache.commons.lang.time.DateUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Required;


import java.util.Date;

import java.util.List;

import java.util.Map;


import static java.lang.String.format;


Logger LOGGER = LoggerFactory.getLogger(""CleanupCronJob"");


def modelService = spring.getBean 'modelService'

def flexibleSearchService = spring.getBean 'flexibleSearchService'

 

int daysOld = 90;


final Map < String, Object > params = new HashMap < String, Object > ();

final StringBuilder builder = new StringBuilder();


builder.append(""SELECT {"" + CronJobModel.PK + ""} FROM {"" + CronJobModel._TYPECODE + "" AS c} "");

builder.append(""WHERE {c."" + CronJobModel.PK + ""} NOT IN ("");

builder.append("" {{SELECT {"" + TriggerModel.CRONJOB + ""} FROM {"" + TriggerModel._TYPECODE + ""} "");

builder.append(""WHERE {"" + TriggerModel.CRONJOB + ""} IS NOT NULL}}"");

builder.append("") "");

builder.append(""AND {"" + CronJobModel.STATUS + ""} IN ( ?status ) "");

builder.append(""AND {"" + CronJobModel.RESULT + ""} IN ( ?result ) "");

builder.append(""AND {"" + CronJobModel.ENDTIME + ""} < ?date"");


params.put(""date"", new Date(System.currentTimeMillis() - 1000L * 3600L * 24 * daysOld));

params.put(""result"", CronJobResult.SUCCESS);

params.put(""status"", CronJobStatus.FINISHED);


LOGGER.info(""CleanupCronJob Query :  "" + builder.toString() + ""\nparams : "" + params.toString());


SearchResult < CronJobModel > cronJobModelSearchResult = flexibleSearchService.search(builder.toString(), params);


if (CollectionUtils.isEmpty(cronJobModelSearchResult.getResult())) {

  LOGGER.info(""No old Cronjobs found to remove"");

} else {

  try {

    LOGGER.info(format(""Removing [%s] old cronjob..."", cronJobModelSearchResult.getCount()));

    List < CronJobModel > cronJobsToRemove = cronJobModelSearchResult.getResult();

    modelService.removeAll(cronJobsToRemove);

    LOGGER.info(""Successfully removed old cron jobs"");

  } catch(ModelRemovalException e) {

    LOGGER.error(""Exception while trying to remove old cronjobs : "" + e.getMessage());

    LOGGER.info(""Exception while trying to remove old cronjobs : "" + e.getMessage());

    LOGGER.error(e);

  }

  LOGGER.info(""END CLEAN OLD CRONJOBS : @ "" + new Date(System.currentTimeMillis()));

}

"


INSERT_UPDATE ScriptingJob; code[unique = true] ; scriptURI

                          ; cleanupCronJob ; model://cleanupCronJobScript


INSERT_UPDATE CronJob; code[unique = true] ; job(code)           ; singleExecutable; sessionLanguage(isocode)

                     ; cleanupCronJob ; cleanupCronJob ; false           ; en


# Run at 12am every day to cleanup the cronjobs

INSERT_UPDATE Trigger; cronjob(code)[unique = true]; cronExpression  ;

                     ; cleanupCronJob             ; 0 0 0 ? * * ;

Comments

Popular Posts