Search code examples
javascriptjavaherokuwebpacknashorn

Heroku environment causing Java/Nashorn/ReactJS application to crash


Github repo https://github.com/damorton/dropwizardheroku-webgateway

Build ./gradlew stage

Heroku application runs fine locally using heroku local after build and webpack phases, but crashes on the Heroku environment because the application cannot read the bundle.js file produced by the webpack stage of the build.

Done so far

  • Logged into Heroku app using heroku run bash and checked if the bundle.js file was being created, it is.
  • Updated system.properties to use a similar Java version as my local env java.runtime.version=1.8.0_131

Heroku logs

Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-05-05T10:55:43.963570+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
2017-05-05T10:55:45.617664+00:00 app[web.1]: INFO  [2017-05-05 10:55:45,613] org.eclipse.jetty.util.log: Logging initialized @1649ms
2017-05-05T10:55:45.696063+00:00 app[web.1]: INFO  [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering jersey handler with root path prefix: /
2017-05-05T10:55:45.696110+00:00 app[web.1]: INFO  [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering admin handler with root path prefix: /admin
2017-05-05T10:55:45.696230+00:00 app[web.1]: INFO  [2017-05-05 10:55:45,696] io.dropwizard.assets.AssetsBundle: Registering AssetBundle with name: assets for path /assets/*
2017-05-05T10:55:59.241010+00:00 app[web.1]: InputString in is null with path assets/js/bundle.js
2017-05-05T10:55:59.243502+00:00 app[web.1]: Exception in thread "main" java.lang.NullPointerException
2017-05-05T10:55:59.243739+00:00 app[web.1]:    at java.io.Reader.<init>(Reader.java:78)
2017-05-05T10:55:59.243867+00:00 app[web.1]:    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
2017-05-05T10:55:59.243967+00:00 app[web.1]:    at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.read(NashornController.java:46)
2017-05-05T10:55:59.244023+00:00 app[web.1]:    at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.<init>(NashornController.java:27)
2017-05-05T10:55:59.244096+00:00 app[web.1]:    at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:43)
2017-05-05T10:55:59.244149+00:00 app[web.1]:    at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:23)
2017-05-05T10:55:59.244212+00:00 app[web.1]:    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
2017-05-05T10:55:59.244255+00:00 app[web.1]:    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
2017-05-05T10:55:59.244306+00:00 app[web.1]:    at io.dropwizard.cli.Cli.run(Cli.java:75)
2017-05-05T10:55:59.244391+00:00 app[web.1]:    at io.dropwizard.Application.run(Application.java:79)
2017-05-05T10:55:59.244436+00:00 app[web.1]:    at com.bitbosh.dropwizardheroku.webgateway.Main.main(Main.java:26)

From the logs the application is throwing a NullPointerException and this is caused by the Nashorn engine in NashornController.java evalulating a Reader that was created from an InputStream of the bundle.js file.

What I cant figure out is why it works OK locally, but crashes on the Heroku environment. If I do not create the bundle.js file and evaluate the Javascript source code thats added to the bundle everything is working correctly.


Solution

  • Problem

    Heroku will run ./gradlew stage if not using Spring Boot or Ratpack, this means developers need to specify what happens during their build in a stage task run by the platform. The tasks that stage depends on are not run in any predefined order. This results in the webpack task being run after installDist, building the Jar before bundle.js existed.

    Solution

    Define the order in which the tasks should run.

    task stage(dependsOn: ['installDist', 'webpack', 'npmInstall', 'clean'])
    npmInstall.mustRunAfter clean
    webpack.mustRunAfter npmInstall
    installDist.mustRunAfter webpack
    

    https://devcenter.heroku.com/articles/deploying-gradle-apps-on-heroku#verify-that-your-build-file-is-set-up-correctly