All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sirius.web.crunchlog.DeleteOldCrunchlogs Maven / Gradle / Ivy

There is a newer version: 22.2.3
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.web.crunchlog;

import sirius.kernel.di.std.Framework;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Counter;
import sirius.kernel.health.Exceptions;
import sirius.kernel.timer.EveryDay;

import java.time.Duration;
import java.time.Instant;

/**
 * Deletes crunchlog files older than one year.
 */
@Framework("web.crunchlog")
@Register
public class DeleteOldCrunchlogs implements EveryDay {

    private static final int ONE_YEAR = 365;

    @Part
    private Crunchlog crunchlog;

    @Override
    public String getConfigKeyName() {
        return "crunchlog-cleanup";
    }

    @Override
    public void runTimer() throws Exception {
        Counter filesDeleted = new Counter();
        Instant now = Instant.now();
        crunchlog.collectAllCompletedFiles(file -> {
            Instant fileDate = Instant.ofEpochMilli(file.lastModified());
            if (Duration.between(fileDate, now).toDays() > ONE_YEAR) {
                try {
                    file.delete();
                    filesDeleted.inc();
                } catch (Exception e) {
                    Exceptions.handle(Crunchlog.LOG, e);
                }
            }
        });

        if (filesDeleted.getCount() > 0) {
            Crunchlog.LOG.INFO("Deleted %s old files", filesDeleted.getCount());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy