public void setJmxUrl() { ((EntityInternal)entity).setAttribute(JMX_URL, getJmxUrl()); }
DynamicTasks.queue("pre-start", new Runnable() { public void run() { MachineLocation machine = machineS.get(); log.info("Starting {} on machine {}", entity(), machine); entity().addLocations(ImmutableList.of((Location)machine)); // elsewhere we rely on (public) hostname being set _after_ subnet_hostname // (to prevent the tiny possibility of races resulting in hostname being returned // simply because subnet is still being looked up) Optional<String> lh = Machines.getSubnetHostname(machine); Optional<String> la = Machines.getSubnetIp(machine); if (lh.isPresent()) entity().setAttribute(Attributes.SUBNET_HOSTNAME, lh.get()); if (la.isPresent()) entity().setAttribute(Attributes.SUBNET_ADDRESS, la.get()); entity().setAttribute(Attributes.HOSTNAME, machine.getAddress().getHostName()); entity().setAttribute(Attributes.ADDRESS, machine.getAddress().getHostAddress()); preStartCustom(machine); }}); }
DynamicTasks.queue("pre-stop", new Callable<String>() { public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { log.debug("Skipping stop of entity "+entity()+" when already stopped"); return "Already stopped"; } entity().setAttribute(SoftwareProcess.SERVICE_STATE, Lifecycle.STOPPING); entity().setAttribute(SoftwareProcess.SERVICE_UP, false); preStopCustom(); return null; }});
/** runs the tasks needed to start, wrapped by setting {@link Attributes#SERVICE_STATE} appropriately */ public void start(Collection<? extends Location> locations) { entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); try { startInLocations(locations); DynamicTasks.waitForLast(); if (entity().getAttribute(Attributes.SERVICE_STATE) == Lifecycle.STARTING) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.RUNNING); } catch (Throwable t) { entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.ON_FIRE); throw Exceptions.propagate(t); } }
DynamicTasks.queue("starting", new Runnable() { public void run() { // startInLocations will look up the location, and provision a machine if necessary // (if it remembered the provisioning location) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); startInLocations(null); DynamicTasks.waitForLast(); if (entity().getAttribute(Attributes.SERVICE_STATE) == Lifecycle.STARTING) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.RUNNING); }}); }
@Override public void onEvent(SensorEvent<Long> event) { Long oldval = getAttribute(CassandraCluster.FIRST_NODE_STARTED_TIME_UTC); Long newval = event.getValue(); if (oldval == null && newval != null) { setAttribute(CassandraCluster.FIRST_NODE_STARTED_TIME_UTC, newval); for (CassandraCluster member : Iterables.filter(getMembers(), CassandraCluster.class)) { ((EntityInternal)member).setAttribute(CassandraCluster.FIRST_NODE_STARTED_TIME_UTC, newval); } } } });
/** default restart impl, stops processes if possible, then starts the entity again */ public void restart() { entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STOPPING); DynamicTasks.queue("stopping (process)", new Callable<String>() { public String call() { try { stopProcessesAtMachine(); DynamicTasks.waitForLast(); } catch (Exception e) { String msg = "Could not stop "+entity()+" (process) when restarting: "+e; log.debug(msg); return msg; } return "Stop of process completed with no errors."; }}); DynamicTasks.queue("starting", new Runnable() { public void run() { // startInLocations will look up the location, and provision a machine if necessary // (if it remembered the provisioning location) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); startInLocations(null); DynamicTasks.waitForLast(); if (entity().getAttribute(Attributes.SERVICE_STATE) == Lifecycle.STARTING) entity().setAttribute(Attributes.SERVICE_STATE, Lifecycle.RUNNING); }}); }
entity().setAttribute(Attributes.HOSTNAME, null); entity().setAttribute(Attributes.ADDRESS, null); entity().setAttribute(Attributes.SUBNET_HOSTNAME, null); entity().setAttribute(Attributes.SUBNET_ADDRESS, null); if (provisioner != null) provisioner.release((MachineLocation)machine); } catch (Throwable t) {
public MachineLocation call() throws Exception { final Map<String,Object> flags = obtainProvisioningFlags(location); if (!(location instanceof LocalhostMachineProvisioningLocation)) log.info("Starting {}, obtaining a new location instance in {} with ports {}", new Object[] {entity(), location, flags.get("inboundPorts")}); entity().setAttribute(SoftwareProcess.PROVISIONING_LOCATION, location); MachineLocation machine; try { machine = Tasks.withBlockingDetails("Provisioning machine in "+location, new Callable<MachineLocation>() { public MachineLocation call() throws NoMachinesAvailableException { return location.obtain(flags); }}); if (machine == null) throw new NoMachinesAvailableException("Failed to obtain machine in "+location.toString()); } catch (Exception e) { throw Exceptions.propagate(e); } if (log.isDebugEnabled()) log.debug("While starting {}, obtained new location instance {}", entity(), (machine instanceof SshMachineLocation ? machine+", details "+((SshMachineLocation)machine).getUser()+":"+Entities.sanitize(((SshMachineLocation)machine).getAllConfig(false)) : machine)); return machine; } }).build());
protected boolean tryCheckStartPid() { if (pidFile==null) return false; // if it's still up after 5s assume we are good (default behaviour) Time.sleep(Duration.FIVE_SECONDS); if (!DynamicTasks.queue(SshEffectorTasks.isPidFromFileRunning(pidFile).runAsRoot()).get()) { throw new IllegalStateException("The process for "+entity()+" appears not to be running (pid file "+pidFile+")"); } // and set the PID entity().setAttribute(Attributes.PID, Integer.parseInt(DynamicTasks.queue(SshEffectorTasks.ssh("cat "+pidFile).runAsRoot()).block().getStdout().trim())); return true; }
throw Exceptions.propagate((Throwable)stoppingProcess.getUnchecked()); entity().setAttribute(SoftwareProcess.SERVICE_UP, false); entity().setAttribute(SoftwareProcess.SERVICE_STATE, Lifecycle.STOPPED);
protected boolean tryStopPid() { Integer pid = entity().getAttribute(Attributes.PID); if (pid==null) { if (entity().getAttribute(Attributes.SERVICE_STATE)==Lifecycle.RUNNING && pidFile==null) log.warn("No PID recorded for "+entity()+" when running, with PID file "+pidFile+"; skipping kill in "+Tasks.current()); else if (log.isDebugEnabled()) log.debug("No PID recorded for "+entity()+"; skipping ("+entity().getAttribute(Attributes.SERVICE_STATE)+" / "+pidFile+")"); return false; } // allow non-zero exit as process may have already been killed DynamicTasks.queue(SshEffectorTasks.ssh( "kill "+pid, "sleep 5", BashCommands.ok("kill -9 "+pid)).allowingNonZeroExitCode().runAsRoot()).block(); if (DynamicTasks.queue(SshEffectorTasks.isPidRunning(pid).runAsRoot()).get()) { throw new IllegalStateException("Process for "+entity()+" in "+pid+" still running after kill"); } entity().setAttribute(Attributes.PID, null); return true; }