Skip to content

Commit

Permalink
Two-tier service initialization ready for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Sep 17, 2024
1 parent 1647e13 commit 29635d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.integratedmodelling.klab.services;

import com.google.common.collect.Sets;
import org.integratedmodelling.common.authentication.Authentication;
import org.integratedmodelling.common.authentication.scope.AbstractServiceDelegatingScope;
import org.integratedmodelling.common.authentication.scope.ChannelImpl;
Expand All @@ -14,6 +15,7 @@
import org.integratedmodelling.klab.rest.ServiceReference;
import org.integratedmodelling.klab.services.application.ServiceNetworkedInstance;
import org.integratedmodelling.klab.services.base.BaseService;
import org.springframework.security.core.parameters.P;

import java.util.*;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -290,36 +292,51 @@ private void timedTasks() {
check all needed services; put self offline if not available or not there, online otherwise; if
there's a change in online status, report it through the service scope
*/
boolean ok = true;
boolean okEssentials = true;
boolean okOperationals = true;

var essentials = getEssentialServices();
var operational = getOperationalServices();
var allservices = EnumSet.copyOf(essentials);
allservices.addAll(operational);

boolean wasAvailable = serviceScope.isAvailable();

for (var serviceType : getEssentialServices()) {
for (var serviceType : allservices) {
var service = currentServices.get(serviceType);
if (service == null) {
service = this.createDefaultService(serviceType, serviceScope,
(System.currentTimeMillis() - bootTime) / 1000);
ok = service != null && service.status().isAvailable();
okEssentials = essentials.contains(serviceType) && service != null && service.status().isAvailable();
okOperationals = operational.contains(serviceType) && service != null && service.status().isAvailable();

if (service != null) {
registerService(service, true);
}
} else if (!service.status().isAvailable()) {
ok = false;
okEssentials = !essentials.contains(serviceType);
okOperationals = !operational.contains(serviceType);
}
}

if (ok) {
if (okEssentials) {
setAvailable(true);
serviceScope.setStatus(Scope.Status.STARTED);
} else {
setAvailable(false);
serviceScope.setStatus(Scope.Status.WAITING);
}

if (okOperationals) {
setAvailable(okEssentials);
} else {
setAvailable(false);
}

firstCall = false;

if (wasAvailable != ok) {
if (ok) {
if (wasAvailable != okEssentials) {
if (okEssentials) {
if (initialized.get()) {
serviceScope.send(Message.MessageClass.ServiceLifecycle,
Message.MessageType.ServiceAvailable, klabService().capabilities(serviceScope));
Expand All @@ -338,7 +355,7 @@ private void timedTasks() {
if status is OK and the service hasn't been initialized, set maintenance mode and call
initializeService().
*/
if (ok && !initialized.get()) {
if (okEssentials && !initialized.get()) {
setBusy(true);
klabService().initializeService();
klabService().setInitialized(true);
Expand All @@ -348,6 +365,14 @@ private void timedTasks() {
setBusy(false);
}

if (okEssentials && okOperationals && !operationalized.get()) {
setBusy(true);
operationalized.set(true);
klabService().operationalizeService();
klabService().setOperational(true);
setBusy(false);
}

/*
if subscribed and configured interval has passed, send service health status through the scope
*/
Expand All @@ -369,6 +394,7 @@ protected void setAvailable(boolean b) {
serviceScope.setMaintenanceMode(!b);
}


protected void setBusy(boolean b) {
serviceScope.setAtomicOperationMode(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class BaseService implements KlabService {
protected final ServiceStartupOptions startupOptions;
private ScopeManager _scopeManager;
private boolean initialized;
private boolean operational;

protected BaseService(AbstractServiceDelegatingScope scope, KlabService.Type serviceType,
ServiceStartupOptions options) {
Expand Down Expand Up @@ -359,7 +360,14 @@ public String registerContext(ContextScope contextScope) {
serviceSessionScope.getId() : null;
}


/**
* Called by ServiceInstance after initializeService was successful
*
* @param b
*/
public void setOperational(boolean b) {
this.operational = true;
}
/**
* Called by ServiceInstance after initializeService was successful
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void initializeService() {
@Override
public void operationalizeService() {
// reasoner is available, index the kbox
Logging.INSTANCE.info("Reasoner is available: indexing semantic assets");
indexKnowledge();
}

Expand Down

0 comments on commit 29635d4

Please sign in to comment.