@SuppressWarnings("incomplete-switch") private void restart() { switch (state.get()) { case STARTING: Trace.debug( "Returning early from restart. Already starting for project {0} and kind {1}", project.getName(), kind); case DISCONNECTED: Trace.debug( "Endpoint disconnected and skipping restart for project {0} and kind {1}", project.getName(), kind); return; } try { // TODO enhance fix to only check project once conn.refresh(project); Trace.debug( "WatchManager Rescheduling watch job for project {0} and kind {1}", project.getName(), kind); startWatch(project, backoff, lastConnect, this); } catch (Exception e) { Trace.debug( "WatchManager Unable to rescheduling watch job for project {0} and kind {1}", e, project.getName(), kind); stopWatch(project, conn); } }
@Override public void connected(List<IResource> resources) { Trace.debug( "WatchManager Endpoint connected to {0} with {1} resources", conn.toString(), resources.size()); this.resources.addAll(resources); }
public WatchListener( IProject project, IOpenShiftConnection conn, String kind, int backoff, long lastConnect) { Trace.debug( "WatchManager Adding WatchListener for {0} and kind {1}", project.getName(), kind); this.project = project; this.conn = conn; this.backoff = backoff; this.lastConnect = lastConnect; this.kind = kind; if (System.currentTimeMillis() - lastConnect > BACKOFF_RESET) { backoff = 0; } Trace.debug( "WatchManager Initial watch backoff of {0} ms", FIBONACCI[backoff] * BACKOFF_MILLIS); }
public void start(int backoff, long lastConnect) { if (state.getAndSet(State.STARTING) == State.STARTING) { Trace.debug("In the process of starting watch already. Returning early"); return; } this.backoff = backoff; this.lastConnect = lastConnect; Trace.info("Starting watch on project {0} for kind {1}", project.getName(), kind); IClient client = getClientFor(project); if (client != null) { new RestartWatchJob(client).schedule(); } }
@Override protected IStatus run(IProgressMonitor monitor) { try { connect(client); } catch (Exception e) { Trace.debug( "Exception starting watch on project {0} and {1} kind", e, project.getName(), kind); backoff++; if (backoff >= FIBONACCI.length) { Trace.info( "Exceeded backoff attempts trying to reconnect watch for {0} and kind {1}", project.getName(), kind); watches.remove(project); state.set(State.DISCONNECTED); return Status.OK_STATUS; } final long delay = FIBONACCI[backoff] * BACKOFF_MILLIS; Trace.debug( "Delaying watch restart by {0}ms for project {1} and kinds {2} ", delay, project.getName(), kind); new RestartWatchJob(client).schedule(delay); } return Status.OK_STATUS; }
@Override public void received(IResource resource, ChangeType change) { Trace.debug("Watch received change in {0} state\n{1}", state, resource.toJson(false)); if (State.CONNECTED == state.get()) { IResource newItem = null; IResource oldItem = null; int index = resources.indexOf(resource); if (ChangeType.ADDED.equals(change)) { resources.add(resource); newItem = resource; } else if (ChangeType.DELETED.equals(change)) { oldItem = index > NOT_FOUND ? resources.remove(index) : resource; } else if (ChangeType.MODIFIED.equals(change)) { if (index > NOT_FOUND) { oldItem = resources.remove(index); } resources.add(resource); newItem = resource; } ConnectionsRegistrySingleton.getInstance() .fireConnectionChanged(conn, ConnectionProperties.PROPERTY_RESOURCE, oldItem, newItem); } }
@Override public void disconnected() { Trace.debug("WatchManager Endpoint disconnected to {0}.", conn.toString()); state.set(State.DISCONNECTED); }