Search in sources :

Example 6 with Fit3DJob

use of bio.fkaiser.fit3d.web.model.Fit3DJob in project fit3d by fkaiserbio.

the class JobManager method init.

@PostConstruct
public void init() {
    // schedule updating of load for execution
    TimerTask loadMonitoringTask = new TimerTask() {

        @Override
        public void run() {
            int runningJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isRunning).count();
            int enqueuedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isEnqueued).count();
            int finishedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isFinished).count();
            int failedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isFailed).count();
            jobLoadManager.setRunningJobCount(runningJobCount);
            jobLoadManager.setEnqueuedJobCount(enqueuedJobCount);
            jobLoadManager.setFinishedJobCount(finishedJobCount);
            jobLoadManager.setFailedJobCount(failedJobCount);
        }
    };
    // schedule job cleaning
    TimerTask jobCleaningTask = new TimerTask() {

        @Override
        public void run() {
            logger.info("looking for expired jobs");
            List<Fit3DJob> oldJobs = managedJobs.values().stream().flatMap(Collection::stream).filter(job -> job.getJobAgeInHours() > Fit3DWebConstants.JobManager.JOB_AGE_IN_HOURS).collect(Collectors.toList());
            for (Fit3DJob oldJob : oldJobs) {
                oldJob.cancel();
                try {
                    oldJob.delete();
                    Optional<List<Fit3DJob>> jobList = managedJobs.values().stream().filter(jobs -> jobs.stream().anyMatch(job -> job.equals(oldJob))).findFirst();
                    if (jobList.isPresent()) {
                        Iterator<Fit3DJob> iterator = jobList.get().iterator();
                        while (iterator.hasNext()) {
                            Fit3DJob currentJob = iterator.next();
                            if (currentJob.equals(oldJob)) {
                                iterator.remove();
                                logger.info("removed job {} from managed jobs", oldJob);
                            }
                        }
                    }
                } catch (IOException e) {
                    logger.error("failed to delete old job {}", oldJob);
                }
            }
        }
    };
    TimerTask jobNotificationTask = new TimerTask() {

        @Override
        public void run() {
            List<Fit3DJob> notifiableJobs = managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isSendMail).collect(Collectors.toList());
            for (Fit3DJob job : notifiableJobs) {
                mailNotifier.sendMail(job);
                job.setSendMail(false);
                mongoCollection.updateOne(eq("jobIdentifier", job.toString()), new Document("$set", new Document().append("sendMail", false)));
            }
        }
    };
    Timer timer = new Timer(true);
    timer.schedule(loadMonitoringTask, 1000, Fit3DWebConstants.JobManager.LOAD_UPDATE_INTERVAL);
    timer.schedule(jobCleaningTask, 1000, Fit3DWebConstants.JobManager.CLEANUP_INTERVAL);
    timer.schedule(jobNotificationTask, 1000, 1000);
}
Also used : JobConverter(bio.fkaiser.fit3d.web.converter.JobConverter) Document(org.bson.Document) java.util(java.util) Logger(org.slf4j.Logger) MongoCollection(com.mongodb.client.MongoCollection) LoggerFactory(org.slf4j.LoggerFactory) Fit3DJob(bio.fkaiser.fit3d.web.model.Fit3DJob) IOException(java.io.IOException) Fit3DWebConstants(bio.fkaiser.fit3d.web.Fit3DWebConstants) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Future(java.util.concurrent.Future) MongoClient(com.mongodb.MongoClient) PostConstruct(javax.annotation.PostConstruct) Filters.eq(com.mongodb.client.model.Filters.eq) MongoCollection(com.mongodb.client.MongoCollection) Fit3DJob(bio.fkaiser.fit3d.web.model.Fit3DJob) IOException(java.io.IOException) Document(org.bson.Document) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Fit3DJob (bio.fkaiser.fit3d.web.model.Fit3DJob)6 Document (org.bson.Document)5 MongoClient (com.mongodb.MongoClient)3 PostConstruct (javax.annotation.PostConstruct)3 Fit3DWebConstants (bio.fkaiser.fit3d.web.Fit3DWebConstants)2 JobConverter (bio.fkaiser.fit3d.web.converter.JobConverter)2 Fit3DJobParameters (bio.fkaiser.fit3d.web.model.Fit3DJobParameters)2 MongoCollection (com.mongodb.client.MongoCollection)2 Filters.eq (com.mongodb.client.model.Filters.eq)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 java.util (java.util)2 Future (java.util.concurrent.Future)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ExchangeDefinition (bio.fkaiser.fit3d.web.model.ExchangeDefinition)1 Collection (java.util.Collection)1 List (java.util.List)1 FacesMessage (javax.faces.application.FacesMessage)1