use of org.springframework.scheduling.annotation.Scheduled in project weixin-boot by guhanjie.
the class AccessTokenKit method refreshToken.
@Scheduled(fixedRate = 6000000)
public synchronized void refreshToken() {
LOGGER.info("Starting to refresh access token...");
try {
String url = WeixinConstants.API_ACCESS_TOKEN;
url = url.replaceAll("APPID", weixinContants.APPID);
url = url.replaceAll("APPSECRET", weixinContants.APPSECRET);
WeixinHttpUtil.sendGet(url, new WeixinHttpCallback() {
@Override
public void process(String json) {
AccessToken at = JSONObject.parseObject(json, AccessToken.class);
if (at != null && at.getAccess_token() != null) {
token = at.getAccess_token();
LOGGER.info("Success to refresh access token:[{}].", token);
} else {
LOGGER.error("Failed to refresh access token.");
}
}
});
} catch (Exception e) {
LOGGER.error("Failed to refresh access token.", e);
}
}
use of org.springframework.scheduling.annotation.Scheduled in project Gemma by PavlidisLab.
the class SubmittedTasksMaintenance method doSubmittedTasksMaintenance.
/**
* Check if a task has been running or queued for too long, and cancel it if necessary. Email alert will always be
* sent in that case.
*/
@Scheduled(fixedDelay = 120000)
public void doSubmittedTasksMaintenance() {
// Assumes collection implementing weakly consistent iterator with remove support.
Collection<SubmittedTask<? extends TaskResult>> tasks = taskRunningService.getSubmittedTasks();
int numQueued = 0;
int numRunning = 0;
int numDone = 0;
int numCancelled = 0;
for (SubmittedTask<?> task : tasks) {
if (!task.getStatus().equals(SubmittedTask.Status.COMPLETED)) {
SubmittedTasksMaintenance.log.info("Checking task: " + task.getTaskCommand().getClass().getSimpleName() + task.getTaskId() + " started=" + task.getStartTime() + " status=" + task.getStatus());
}
switch(task.getStatus()) {
case QUEUED:
numQueued++;
Date submissionTime = task.getSubmissionTime();
Integer maxQueueWait = task.getTaskCommand().getMaxQueueMinutes();
assert submissionTime != null;
assert maxQueueWait != null;
if (submissionTime.before(DateUtils.addMinutes(new Date(), -maxQueueWait))) {
SubmittedTasksMaintenance.log.warn("Submitted task " + task.getTaskCommand().getClass().getSimpleName() + " has been queued for too long (max=" + maxQueueWait + "minutes), attempting to cancel: " + task.getTaskId());
task.addEmailAlert();
// -> email admin? is worker dead?
task.requestCancellation();
}
break;
case RUNNING:
numRunning++;
Date startTime = task.getStartTime();
int maxRunTime = task.getTaskCommand().getMaxRuntime();
assert startTime != null;
if (startTime.before(DateUtils.addMinutes(new Date(), -maxRunTime))) {
SubmittedTasksMaintenance.log.warn("Running task is taking too long, attempting to cancel: " + task.getTaskId() + " " + task.getTaskCommand().getClass().getSimpleName());
task.addEmailAlert();
task.requestCancellation();
}
break;
case CANCELLING:
numCancelled++;
break;
case FAILED:
// fall through
case COMPLETED:
numDone++;
if (task.getFinishTime().before(DateUtils.addMinutes(new Date(), -SubmittedTasksMaintenance.MAX_KEEP_TRACK_AFTER_COMPLETED_MINUTES))) {
SubmittedTasksMaintenance.log.debug(task.getStatus().name() + " task result not retrieved, timing out: " + task.getTaskId() + " " + task.getTaskCommand().getClass().getSimpleName());
// concurrent modification.
tasks.remove(task);
}
break;
case UNKNOWN:
break;
default:
break;
}
}
if (tasks.size() > 0 && numDone != tasks.size())
SubmittedTasksMaintenance.log.info(tasks.size() + " tasks monitored; Done: " + numDone + "; Running: " + numRunning + "; Cancelled: " + numCancelled + "; Queued: " + numQueued);
}
use of org.springframework.scheduling.annotation.Scheduled in project rpki-validator-3 by RIPE-NCC.
the class RpkiObjectCleanupService method cleanupRpkiObjects.
/**
* Marks all RPKI objects that are reachable from a trust anchor by following the entries in the manifests.
* Objects that are no longer reachable will be deleted after a configurable grace duration.
*/
@Scheduled(initialDelay = 60_000, fixedDelayString = "${rpki.validator.rpki.object.cleanup.interval.ms}")
public long cleanupRpkiObjects() {
Instant now = Instant.now();
for (TrustAnchor trustAnchor : trustAnchors.findAll()) {
transactionTemplate.execute((status) -> {
entityManager.setFlushMode(FlushModeType.COMMIT);
log.debug("tracing objects for trust anchor {}", trustAnchor);
X509ResourceCertificate resourceCertificate = trustAnchor.getCertificate();
if (resourceCertificate != null) {
traceCertificateAuthority(now, resourceCertificate);
}
return null;
});
}
return deleteUnreachableObjects(now);
}
use of org.springframework.scheduling.annotation.Scheduled in project rpki-validator-3 by RIPE-NCC.
the class ValidationRunCleanupService method cleanupValidationRuns.
@Scheduled(initialDelay = 60_000, fixedDelayString = "${rpki.validator.validation.run.cleanup.interval.ms}")
@Transactional
public long cleanupValidationRuns() {
// Delete all validation runs older than `cleanupGraceDuration` that have a later validation run.
Instant completedBefore = Instant.now().minus(cleanupGraceDuration);
long removedCount = validationRuns.removeOldValidationRuns(completedBefore);
log.info("Removed {} old validation runs", removedCount);
return removedCount;
}
use of org.springframework.scheduling.annotation.Scheduled in project rpki-validator-3 by RIPE-NCC.
the class RpkiRepositoryValidationService method validateRsyncRepositories.
@Scheduled(initialDelay = 10_000, fixedDelay = 10_000)
public void validateRsyncRepositories() {
entityManager.setFlushMode(FlushModeType.COMMIT);
Instant cutoffTime = Instant.now().minus(rsyncRepositoryDownloadInterval);
log.info("updating all rsync repositories that have not been downloaded since {}", cutoffTime);
Set<TrustAnchor> affectedTrustAnchors = new HashSet<>();
final RsyncRepositoryValidationRun validationRun = new RsyncRepositoryValidationRun();
validationRunRepository.add(validationRun);
Stream<RpkiRepository> repositories = rpkiRepositories.findRsyncRepositories();
Map<String, RpkiObject> objectsBySha256 = new HashMap<>();
Map<URI, RpkiRepository> fetchedLocations = new HashMap<>();
ValidationResult results = repositories.filter((repository) -> {
boolean needsUpdate = repository.isPending() || repository.getLastDownloadedAt() == null || repository.getLastDownloadedAt().isBefore(cutoffTime);
if (!needsUpdate) {
fetchedLocations.put(URI.create(repository.getRsyncRepositoryUri()), repository);
}
return needsUpdate;
}).map((repository) -> processRsyncRepository(affectedTrustAnchors, validationRun, fetchedLocations, objectsBySha256, repository)).collect(() -> ValidationResult.withLocation("placeholder"), ValidationResult::addAll, ValidationResult::addAll);
validationRun.completeWith(results);
affectedTrustAnchors.forEach(validationRunRepository::runCertificateTreeValidation);
}
Aggregations