@Test void canRun() { final int days = 5; final int negativeDays = -1 * days; final int pageSize = 10; Mockito.when(this.jobCleanupProperties.getRetention()).thenReturn(days).thenReturn(negativeDays); Mockito.when(this.jobCleanupProperties.getPageSize()).thenReturn(pageSize); Mockito.when(this.commandDeactivationProperties.getCommandCreationThreshold()).thenReturn(60); Mockito.when(this.commandDeactivationProperties.getJobCreationThreshold()).thenReturn(30); final ArgumentCaptor<Instant> argument = ArgumentCaptor.forClass(Instant.class); final long deletedCount1 = 6L; final long deletedCount2 = 18L; final long deletedCount3 = 2L; Mockito .when( this.persistenceService.deleteJobsCreatedBefore( Mockito.any(Instant.class), Mockito.eq(JobStatus.getActiveStatuses()), Mockito.eq(pageSize) ) ) .thenReturn(deletedCount1) .thenReturn(0L) .thenReturn(deletedCount2) .thenReturn(deletedCount3) .thenReturn(0L); Mockito .when( this.persistenceService.deleteUnusedClusters( Mockito.eq(EnumSet.of(ClusterStatus.TERMINATED)), Mockito.any(Instant.class) ) ).thenReturn(1L, 2L); Mockito.when(this.persistenceService.deleteUnusedFiles(Mockito.any(Instant.class))).thenReturn(3L, 4L); Mockito.when(this.persistenceService.deleteUnusedTags(Mockito.any(Instant.class))).thenReturn(5L, 6L); Mockito .when(this.persistenceService.deleteUnusedApplications(Mockito.any(Instant.class))) .thenReturn(11L, 117L); Mockito .when( this.persistenceService.updateStatusForUnusedCommands( Mockito.eq(CommandStatus.INACTIVE), Mockito.any(Instant.class), Mockito.eq(EnumSet.of(CommandStatus.DEPRECATED, CommandStatus.ACTIVE)), Mockito.any(Instant.class) ) ) .thenReturn(50, 242); Mockito .when( this.persistenceService.deleteUnusedCommands( Mockito.eq(EnumSet.of(CommandStatus.INACTIVE)), Mockito.any(Instant.class) ) ) .thenReturn(11L, 81L); final Calendar before = Calendar.getInstance(JobConstants.UTC); this.task.run(); this.task.run(); final Calendar after = Calendar.getInstance(JobConstants.UTC); if (before.get(Calendar.DAY_OF_YEAR) == after.get(Calendar.DAY_OF_YEAR)) { Mockito .verify(this.persistenceService, Mockito.times(5)) .deleteJobsCreatedBefore( argument.capture(), Mockito.eq(JobStatus.getActiveStatuses()), Mockito.eq(pageSize) ); final Calendar date = Calendar.getInstance(JobConstants.UTC); date.set(Calendar.HOUR_OF_DAY, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); date.add(Calendar.DAY_OF_YEAR, negativeDays); Assertions.assertThat(argument.getAllValues().get(0).toEpochMilli()).isEqualTo(date.getTime().getTime()); Assertions.assertThat(argument.getAllValues().get(1).toEpochMilli()).isEqualTo(date.getTime().getTime()); Mockito.verify(this.persistenceService, Mockito.times(2)).deleteUnusedClusters( Mockito.eq(EnumSet.of(ClusterStatus.TERMINATED)), Mockito.any(Instant.class) ); Mockito .verify(this.persistenceService, Mockito.times(2)) .deleteUnusedFiles(Mockito.any(Instant.class)); Mockito .verify(this.persistenceService, Mockito.times(2)) .deleteUnusedTags(Mockito.any(Instant.class)); Mockito .verify(this.persistenceService, Mockito.times(2)) .deleteUnusedApplications(Mockito.any(Instant.class)); Mockito .verify(this.persistenceService, Mockito.times(2)) .deleteUnusedCommands(Mockito.eq(EnumSet.of(CommandStatus.INACTIVE)), Mockito.any(Instant.class)); Mockito .verify(this.persistenceService, Mockito.times(2)) .updateStatusForUnusedCommands( Mockito.eq(CommandStatus.INACTIVE), Mockito.any(Instant.class), Mockito.eq(EnumSet.of(CommandStatus.DEPRECATED, CommandStatus.ACTIVE)), Mockito.any(Instant.class) ); } }
@Override public void run() { final long start = System.nanoTime(); final Instant runtime = Instant.now(); final Set<Tag> tags = Sets.newHashSet(); try { this.deleteJobs(); final Instant creationThreshold = runtime.minus(1L, ChronoUnit.HOURS); this.deleteClusters(creationThreshold); this.deactivateCommands(runtime); this.deleteCommands(creationThreshold); this.deleteApplications(creationThreshold); this.deleteFiles(creationThreshold); this.deleteTags(creationThreshold); MetricsUtils.addSuccessTags(tags); } catch (final Throwable t) { MetricsUtils.addFailureTagsWithException(tags, t); throw t; } finally { this.registry .timer(DATABASE_CLEANUP_DURATION_TIMER_NAME, tags) .record(System.nanoTime() - start, TimeUnit.NANOSECONDS); } }
DatabaseCleanupTask extends LeaderTask { @Override public void run() { final long start = System.nanoTime(); final Instant runtime = Instant.now(); final Set<Tag> tags = Sets.newHashSet(); try { this.deleteJobs(); final Instant creationThreshold = runtime.minus(1L, ChronoUnit.HOURS); this.deleteClusters(creationThreshold); this.deactivateCommands(runtime); this.deleteCommands(creationThreshold); this.deleteApplications(creationThreshold); this.deleteFiles(creationThreshold); this.deleteTags(creationThreshold); MetricsUtils.addSuccessTags(tags); } catch (final Throwable t) { MetricsUtils.addFailureTagsWithException(tags, t); throw t; } finally { this.registry .timer(DATABASE_CLEANUP_DURATION_TIMER_NAME, tags) .record(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } }
DatabaseCleanupTask extends LeaderTask { @Override public void run() { final long start = System.nanoTime(); final Instant runtime = Instant.now(); final Set<Tag> tags = Sets.newHashSet(); try { this.deleteJobs(); final Instant creationThreshold = runtime.minus(1L, ChronoUnit.HOURS); this.deleteClusters(creationThreshold); this.deactivateCommands(runtime); this.deleteCommands(creationThreshold); this.deleteApplications(creationThreshold); this.deleteFiles(creationThreshold); this.deleteTags(creationThreshold); MetricsUtils.addSuccessTags(tags); } catch (final Throwable t) { MetricsUtils.addFailureTagsWithException(tags, t); throw t; } finally { this.registry .timer(DATABASE_CLEANUP_DURATION_TIMER_NAME, tags) .record(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } DatabaseCleanupTask(
        @NotNull final DatabaseCleanupProperties cleanupProperties,
        @NotNull final Environment environment,
        @NotNull final DataServices dataServices,
        @NotNull final MeterRegistry registry
    ); }
DatabaseCleanupTask extends LeaderTask { @Override public void run() { final long start = System.nanoTime(); final Instant runtime = Instant.now(); final Set<Tag> tags = Sets.newHashSet(); try { this.deleteJobs(); final Instant creationThreshold = runtime.minus(1L, ChronoUnit.HOURS); this.deleteClusters(creationThreshold); this.deactivateCommands(runtime); this.deleteCommands(creationThreshold); this.deleteApplications(creationThreshold); this.deleteFiles(creationThreshold); this.deleteTags(creationThreshold); MetricsUtils.addSuccessTags(tags); } catch (final Throwable t) { MetricsUtils.addFailureTagsWithException(tags, t); throw t; } finally { this.registry .timer(DATABASE_CLEANUP_DURATION_TIMER_NAME, tags) .record(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } DatabaseCleanupTask(
        @NotNull final DatabaseCleanupProperties cleanupProperties,
        @NotNull final Environment environment,
        @NotNull final DataServices dataServices,
        @NotNull final MeterRegistry registry
    ); @Override GenieTaskScheduleType getScheduleType(); @Override Trigger getTrigger(); @Override void run(); @Override void cleanup(); }
DatabaseCleanupTask extends LeaderTask { @Override public void run() { final long start = System.nanoTime(); final Instant runtime = Instant.now(); final Set<Tag> tags = Sets.newHashSet(); try { this.deleteJobs(); final Instant creationThreshold = runtime.minus(1L, ChronoUnit.HOURS); this.deleteClusters(creationThreshold); this.deactivateCommands(runtime); this.deleteCommands(creationThreshold); this.deleteApplications(creationThreshold); this.deleteFiles(creationThreshold); this.deleteTags(creationThreshold); MetricsUtils.addSuccessTags(tags); } catch (final Throwable t) { MetricsUtils.addFailureTagsWithException(tags, t); throw t; } finally { this.registry .timer(DATABASE_CLEANUP_DURATION_TIMER_NAME, tags) .record(System.nanoTime() - start, TimeUnit.NANOSECONDS); } } DatabaseCleanupTask(
        @NotNull final DatabaseCleanupProperties cleanupProperties,
        @NotNull final Environment environment,
        @NotNull final DataServices dataServices,
        @NotNull final MeterRegistry registry
    ); @Override GenieTaskScheduleType getScheduleType(); @Override Trigger getTrigger(); @Override void run(); @Override void cleanup();  }