class MetadataSetupService {

    public void cleanupRequestedInstances(Stack stack) {
        Set<InstanceMetaData> requestedInstances = instanceMetaDataService.findNotTerminatedForStack(stack.getId()).stream()
                .filter(instanceMetaData -> instanceMetaData.getInstanceStatus() == InstanceStatus.REQUESTED)
                .collect(Collectors.toSet());
        requestedInstances.forEach(instanceMetaData -> {
            instanceMetaData.setTerminationDate(clock.getCurrentTimeMillis());
            instanceMetaData.setInstanceStatus(InstanceStatus.TERMINATED);
        });
        instanceMetaDataService.saveAll(requestedInstances);
    }

}