use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryJpaImpl method getHostLoads.
/**
* Gets a map of hosts to the number of jobs currently loading that host
*
* @param em
* the entity manager
*
* @return the map of hosts to job counts
*/
SystemLoad getHostLoads(EntityManager em) {
final SystemLoad systemLoad = new SystemLoad();
// Find all jobs that are currently running on any given host, or get all of them
Query q = em.createNamedQuery("ServiceRegistration.hostloads");
List<Integer> statuses = new LinkedList<Integer>();
for (Status status : JOB_STATUSES_INFLUENCING_LOAD_BALANCING) {
statuses.add(status.ordinal());
}
q.setParameter("statuses", statuses);
// Note: This is used in the query to filter out workflow jobs.
// These jobs are load balanced by the workflow service directly.
q.setParameter("workflow_type", TYPE_WORKFLOW);
// Accumulate the numbers for relevant job statuses per host
for (Object result : q.getResultList()) {
Object[] resultArray = (Object[]) result;
String host = String.valueOf(resultArray[0]);
Status status = Status.values()[(int) resultArray[1]];
float load = ((Number) resultArray[2]).floatValue();
// Only queued, and running jobs are adding to the load, so every other status is discarded
if (status == null || !JOB_STATUSES_INFLUENCING_LOAD_BALANCING.contains(status)) {
load = 0.0f;
}
// Add the service registration
NodeLoad serviceLoad;
if (systemLoad.containsHost(host)) {
serviceLoad = systemLoad.get(host);
serviceLoad.setLoadFactor(serviceLoad.getLoadFactor() + load);
} else {
serviceLoad = new NodeLoad(host, load);
}
systemLoad.addNodeLoad(serviceLoad);
}
// This is important, otherwise services which have no current load are not listed in the output!
for (HostRegistration h : getHostRegistrations(em)) {
if (!systemLoad.containsHost(h.getBaseUrl())) {
systemLoad.addNodeLoad(new NodeLoad(h.getBaseUrl(), 0.0f));
}
}
return systemLoad;
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryJpaImpl method getActiveJobs.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#getActiveJobs()
*/
@Override
public List<Job> getActiveJobs() throws ServiceRegistryException {
List<Status> statuses = new ArrayList<Status>();
for (Status status : Status.values()) {
if (status.isActive())
statuses.add(status);
}
EntityManager em = null;
try {
em = emf.createEntityManager();
List<JpaJob> jpaJobs = getJobsByStatus(em, statuses.toArray(new Status[statuses.size()]));
List<Job> jobs = new ArrayList<Job>(jpaJobs.size());
for (JpaJob jpaJob : jpaJobs) {
jobs.add(jpaJob.toJob());
}
return jobs;
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryJpaImpl method update.
/**
* Sets the queue and runtimes and other elements of a persistent job based on a job that's been modified in memory.
* Times on both the objects must be modified, since the in-memory job must not be stale.
*
* @param fromDb
* The job from the database
* @param jpaJob
* The in-memory job
*/
private void update(JpaJob fromDb, JpaJob jpaJob) {
final Job job = jpaJob.toJob();
final Date now = new Date();
final Status status = job.getStatus();
final Status fromDbStatus = fromDb.getStatus();
fromDb.setPayload(job.getPayload());
fromDb.setStatus(job.getStatus());
fromDb.setDispatchable(job.isDispatchable());
fromDb.setVersion(job.getVersion());
fromDb.setOperation(job.getOperation());
fromDb.setArguments(job.getArguments());
fromDb.setBlockedJobIds(job.getBlockedJobIds());
fromDb.setBlockingJobId(job.getBlockingJobId());
if (job.getDateCreated() == null) {
jpaJob.setDateCreated(now);
fromDb.setDateCreated(now);
job.setDateCreated(now);
}
if (job.getProcessingHost() != null) {
ServiceRegistrationJpaImpl processingService = (ServiceRegistrationJpaImpl) getServiceRegistration(job.getJobType(), job.getProcessingHost());
fromDb.setProcessorServiceRegistration(processingService);
}
if (Status.RUNNING.equals(status) && !Status.WAITING.equals(fromDbStatus)) {
jpaJob.setDateStarted(now);
jpaJob.setQueueTime(now.getTime() - job.getDateCreated().getTime());
fromDb.setDateStarted(now);
fromDb.setQueueTime(now.getTime() - job.getDateCreated().getTime());
job.setDateStarted(now);
job.setQueueTime(now.getTime() - job.getDateCreated().getTime());
} else if (Status.FAILED.equals(status)) {
// failed jobs may not have even started properly
fromDb.setDateCompleted(now);
jpaJob.setDateCompleted(now);
job.setDateCompleted(now);
if (job.getDateStarted() != null) {
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
} else if (Status.FINISHED.equals(status)) {
if (job.getDateStarted() == null) {
// Some services (e.g. ingest) don't use job dispatching, since they start immediately and handle their own
// lifecycle. In these cases, if the start date isn't set, use the date created as the start date
jpaJob.setDateStarted(job.getDateCreated());
job.setDateStarted(job.getDateCreated());
}
jpaJob.setDateCompleted(now);
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setDateCompleted(now);
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setDateCompleted(now);
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class SearchServiceImplTest method testPopulateIndex.
@SuppressWarnings("unchecked")
@Test
public void testPopulateIndex() throws Exception {
// This service registry must return a list of jobs
List<String> args = new ArrayList<String>();
args.add(new DefaultOrganization().getId());
List<Job> jobs = new ArrayList<Job>();
for (long i = 0; i < 10; i++) {
MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
mediaPackage.setIdentifier(IdBuilderFactory.newInstance().newIdBuilder().createNew());
searchDatabase.storeMediaPackage(mediaPackage, acl, new Date());
String payload = MediaPackageParser.getAsXml(mediaPackage);
Job job = new JobImpl(i);
job.setArguments(args);
job.setPayload(payload);
job.setStatus(Status.FINISHED);
jobs.add(job);
}
ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
EasyMock.expect(serviceRegistry.createJob((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (List<String>) EasyMock.anyObject(), (String) EasyMock.anyObject(), EasyMock.anyBoolean())).andReturn(new JobImpl()).anyTimes();
EasyMock.expect(serviceRegistry.updateJob((Job) EasyMock.anyObject())).andReturn(new JobImpl()).anyTimes();
EasyMock.expect(serviceRegistry.getJobs((String) EasyMock.anyObject(), (Status) EasyMock.anyObject())).andReturn(jobs).anyTimes();
EasyMock.replay(serviceRegistry);
service.setServiceRegistry(serviceRegistry);
OrganizationDirectoryService orgDirectory = EasyMock.createNiceMock(OrganizationDirectoryService.class);
EasyMock.expect(orgDirectory.getOrganization((String) EasyMock.anyObject())).andReturn(new DefaultOrganization()).anyTimes();
EasyMock.replay(orgDirectory);
service.setOrganizationDirectoryService(orgDirectory);
// We should have nothing in the search index
assertEquals(0, service.getByQuery(new SearchQuery()).size());
service.populateIndex("System Admin");
// This time we should have 10 results
assertEquals(10, service.getByQuery(new SearchQuery()).size());
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryInMemoryImpl method updateInternal.
private Job updateInternal(Job job) {
Date now = new Date();
Status status = job.getStatus();
if (job.getDateCreated() == null) {
job.setDateCreated(now);
}
if (Status.RUNNING.equals(status)) {
job.setDateStarted(now);
job.setQueueTime(now.getTime() - job.getDateCreated().getTime());
} else if (Status.FAILED.equals(status)) {
// failed jobs may not have even started properly
job.setDateCompleted(now);
if (job.getDateStarted() != null) {
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
} else if (Status.FINISHED.equals(status)) {
if (job.getDateStarted() == null) {
// Some services (e.g. ingest) don't use job dispatching, since they start immediately and handle their own
// lifecycle. In these cases, if the start date isn't set, use the date created as the start date
job.setDateStarted(job.getDateCreated());
}
job.setDateCompleted(now);
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
// Cleanup local list of jobs assigned to a specific service
for (Entry<String, List<ServiceRegistrationInMemoryImpl>> service : services.entrySet()) {
for (ServiceRegistrationInMemoryImpl srv : service.getValue()) {
Set<Job> jobs = jobHosts.get(srv);
if (jobs != null) {
Set<Job> updatedJobs = new HashSet<>();
for (Job savedJob : jobs) {
if (savedJob.getId() != job.getId())
updatedJobs.add(savedJob);
}
jobHosts.put(srv, updatedJobs);
}
}
}
}
return job;
}
Aggregations