Search in sources :

Example 1 with PanelStudy

use of gov.sandia.n2a.ui.studies.PanelStudy in project n2a by frothga.

the class NodeJob method monitorProgress.

public synchronized void monitorProgress() {
    if (deleted)
        return;
    if (complete >= 1 && complete != 3)
        return;
    // Limit monitoring to no more than once per second.
    long elapsed = System.currentTimeMillis() - lastMonitored;
    long wait = 1000 - elapsed;
    if (wait > 0) {
        try {
            Thread.sleep(wait);
        } catch (InterruptedException e) {
        }
    }
    lastMonitored = System.currentTimeMillis();
    float oldComplete = complete;
    MNode source = getSource();
    Host env = Host.get(source);
    Path localJobDir = Host.getJobDir(Host.getLocalResourceDir(), source);
    // If job is remote, attempt to grab its state files.
    // TODO: handle remote jobs waiting in queue. Plan is to update "started" file with queue status.
    Path finished = localJobDir.resolve("finished");
    if (!Files.exists(finished) && env instanceof Remote) {
        @SuppressWarnings("resource") Remote remote = (Remote) env;
        if (remote.isConnected() || remote.isEnabled()) {
            try {
                Path remoteJobDir = Host.getJobDir(env.getResourceDir(), source);
                Path remoteFinished = remoteJobDir.resolve("finished");
                // throws an exception if the remote file does not exist
                Files.copy(remoteFinished, finished);
            } catch (Exception e) {
            }
        }
    }
    if (complete == -1) {
        Path started = localJobDir.resolve("started");
        if (Files.exists(started)) {
            complete = 0;
            dateStarted = new Date(Host.lastModified(started));
            lastActive = dateStarted.getTime();
        }
    }
    Backend simulator = Backend.getBackend(source.get("backend"));
    if (complete >= 0 && complete < 1) {
        float percentDone = 0;
        if (expectedSimTime == 0)
            expectedSimTime = new UnitValue(source.get("duration")).get();
        if (expectedSimTime > 0)
            percentDone = (float) (simulator.currentSimTime(source) / expectedSimTime);
        if (Files.exists(finished)) {
            checkFinished(finished);
        } else {
            try {
                long currentTime = System.currentTimeMillis();
                if (simulator.isActive(source)) {
                    lastActive = currentTime;
                } else if (currentTime - lastActive > activeTimeout) {
                    if (percentDone < 1) {
                        // Give it up for dead.
                        Files.copy(new ByteArrayInputStream("dead".getBytes("UTF-8")), finished);
                        complete = 4;
                    } else // Job appears to be actually finished, even though "finished" hasn't been written yet.
                    {
                        // Fake the "finished" file. This might be overwritten later by the batch process.
                        // Most likely, it will also report that we succeeded, so presume that things are fine.
                        Files.copy(new ByteArrayInputStream("success".getBytes("UTF-8")), finished);
                        complete = 1;
                    }
                }
            } catch (Exception e) {
            }
        }
        if (complete >= 0 && complete < 1)
            complete = Math.min(0.99999f, percentDone);
    }
    if (complete == 3) {
        // Check if process is still lingering
        if (!simulator.isActive(source))
            complete = 4;
    }
    PanelRun panelRun = PanelRun.instance;
    PanelStudy panelStudy = PanelStudy.instance;
    // Probably running headless, so skip all UI updates.
    if (panelRun == null)
        return;
    if (complete != oldComplete) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                panelRun.model.nodeChanged(NodeJob.this);
                if (panelRun.displayNode == NodeJob.this) {
                    panelRun.buttonStop.setEnabled(complete < 1 || complete == 3);
                    panelRun.viewJob(true);
                } else if (panelRun.displayNode instanceof NodeFile && panelRun.displayNode.getParent() == NodeJob.this) {
                    panelRun.buttonStop.setEnabled(complete < 1 || complete == 3);
                    // Update the display every 5 seconds during the run.
                    // Some displays, such as a chart, could take longer than 5s to construct, so don't interrupt those.
                    // Always update the display when a run finishes.
                    long currentTime = System.currentTimeMillis();
                    if (complete >= 1 && complete != 3 || panelRun.displayThread == null && currentTime - lastDisplay > 5000) {
                        lastDisplay = currentTime;
                        panelRun.viewFile(true);
                    }
                }
                // panelStudy could be null for a brief moment during startup
                if (panelStudy != null)
                    panelStudy.tableSamples.updateJob(key);
            }
        });
    }
    if (!panelRun.tree.isCollapsed(new TreePath(getPath())))
        build(panelRun.tree);
}
Also used : Path(java.nio.file.Path) TreePath(javax.swing.tree.TreePath) Remote(gov.sandia.n2a.host.Remote) UnitValue(gov.sandia.n2a.language.UnitValue) Host(gov.sandia.n2a.host.Host) MNode(gov.sandia.n2a.db.MNode) IOException(java.io.IOException) Date(java.util.Date) PanelStudy(gov.sandia.n2a.ui.studies.PanelStudy) Backend(gov.sandia.n2a.plugins.extpoints.Backend) TreePath(javax.swing.tree.TreePath) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with PanelStudy

use of gov.sandia.n2a.ui.studies.PanelStudy in project n2a by frothga.

the class PanelEquations method enableStudies.

public void enableStudies() {
    // Studies should not be enabled until runs are enabled, since studies depend on job management.
    if (buttonRun.isEnabled()) {
        buttonStudy.setEnabled(true);
        PanelStudy ps = PanelStudy.instance;
        ps.buttonPause.setEnabled(ps.displayStudy != null && ps.displayStudy.complete() < 1);
        return;
    }
    // Runs are not ready yet, so check again later.
    Timer t = new Timer(1000, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            enableStudies();
        }
    });
    t.setRepeats(false);
    t.start();
}
Also used : Timer(javax.swing.Timer) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) PanelStudy(gov.sandia.n2a.ui.studies.PanelStudy)

Aggregations

PanelStudy (gov.sandia.n2a.ui.studies.PanelStudy)2 MNode (gov.sandia.n2a.db.MNode)1 Host (gov.sandia.n2a.host.Host)1 Remote (gov.sandia.n2a.host.Remote)1 UnitValue (gov.sandia.n2a.language.UnitValue)1 Backend (gov.sandia.n2a.plugins.extpoints.Backend)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 Timer (javax.swing.Timer)1 TreePath (javax.swing.tree.TreePath)1