Search in sources :

Example 1 with OTPAppConstruction

use of org.opentripplanner.standalone.configure.OTPAppConstruction in project OpenTripPlanner by opentripplanner.

the class OTPMain method startOTPServer.

/**
 * All startup logic is in an instance method instead of the static main method so it is possible to build graphs
 * from web services or scripts, not just from the command line. If options cause an OTP API server to start up,
 * this method will return when the web server shuts down.
 *
 * @throws RuntimeException if an error occurs while loading the graph.
 */
private static void startOTPServer(CommandLineParameters params) {
    LOG.info("Searching for configuration and input files in {}", params.getBaseDirectory().getAbsolutePath());
    Graph graph = null;
    OTPAppConstruction app = new OTPAppConstruction(params);
    // Validate data sources, command line arguments and config before loading and
    // processing input data to fail early
    app.validateConfigAndDataSources();
    /* Load graph from disk if one is not present from build. */
    if (params.doLoadGraph() || params.doLoadStreetGraph()) {
        DataSource inputGraph = params.doLoadGraph() ? app.store().getGraph() : app.store().getStreetGraph();
        SerializedGraphObject obj = SerializedGraphObject.load(inputGraph);
        graph = obj.graph;
        app.config().updateConfigFromSerializedGraph(obj.buildConfig, obj.routerConfig);
    }
    /* Start graph builder if requested. */
    if (params.doBuildStreet() || params.doBuildTransit()) {
        // Abort building a graph if the file can not be saved
        SerializedGraphObject.verifyTheOutputGraphIsWritableIfDataSourceExist(app.graphOutputDataSource());
        GraphBuilder graphBuilder = app.createGraphBuilder(graph);
        if (graphBuilder != null) {
            graphBuilder.run();
            // Hand off the graph to the server as the default graph
            graph = graphBuilder.getGraph();
        } else {
            throw new IllegalStateException("An error occurred while building the graph.");
        }
        // Store graph and config used to build it, also store router-config for easy deployment
        // with using the embedded router config.
        new SerializedGraphObject(graph, app.config().buildConfig(), app.config().routerConfig()).save(app.graphOutputDataSource());
    }
    if (graph == null) {
        LOG.error("Nothing to do, no graph loaded or build. Exiting.");
        System.exit(101);
    }
    if (!params.doServe()) {
        LOG.info("Done building graph. Exiting.");
        return;
    }
    // Index graph for travel search
    graph.index();
    Router router = new Router(graph, app.config().routerConfig());
    router.startup();
    /* Start visualizer if requested. */
    if (params.visualize) {
        router.graphVisualizer = new GraphVisualizer(router);
        router.graphVisualizer.run();
    }
    // However, currently the server runs in a blocking way and waits for shutdown, so has to run last.
    if (params.doServe()) {
        GrizzlyServer grizzlyServer = app.createGrizzlyServer(router);
        // Loop to restart server on uncaught fatal exceptions.
        while (true) {
            try {
                grizzlyServer.run();
                return;
            } catch (Throwable throwable) {
                LOG.error("An uncaught error occurred inside OTP. Restarting server. Error was: {}", ThrowableUtils.detailedString(throwable));
            }
        }
    }
}
Also used : GraphVisualizer(org.opentripplanner.visualizer.GraphVisualizer) Graph(org.opentripplanner.routing.graph.Graph) Router(org.opentripplanner.standalone.server.Router) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder) GrizzlyServer(org.opentripplanner.standalone.server.GrizzlyServer) OTPAppConstruction(org.opentripplanner.standalone.configure.OTPAppConstruction) SerializedGraphObject(org.opentripplanner.routing.graph.SerializedGraphObject) DataSource(org.opentripplanner.datastore.DataSource)

Aggregations

DataSource (org.opentripplanner.datastore.DataSource)1 GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)1 Graph (org.opentripplanner.routing.graph.Graph)1 SerializedGraphObject (org.opentripplanner.routing.graph.SerializedGraphObject)1 OTPAppConstruction (org.opentripplanner.standalone.configure.OTPAppConstruction)1 GrizzlyServer (org.opentripplanner.standalone.server.GrizzlyServer)1 Router (org.opentripplanner.standalone.server.Router)1 GraphVisualizer (org.opentripplanner.visualizer.GraphVisualizer)1