Home > Enterprise >  Spring Boot Rest API Scheduler Not Working on Heroku
Spring Boot Rest API Scheduler Not Working on Heroku

Time:01-27

I have deployed my Spring Boot Rest Api backend on Heroku Free Tier. The app has a Scheduled method that is supposed to be run once a day at 12PM. I know that Heroku free tier Dyno goes to sleep after 30mins of inactivity. So unless the dyno is active, the particular method doesn't run. I have confirmed that the method runs the way it is supposed to when the Dyno is active. I did a bit of a reasearch and found somewhat relavant answer here:

https://stackoverflow.com/a/69683216/13687634

The answer suggests that, "Define a logic and call the application's own endpoint in every 30 minutes. The scheduler will start working when the application is launched. Therefore you would guarantee that the application runs all the time." I have a couple of questions on this:

  1. What exactly the logic would be to call the end point? I believe Spring Boot Scheduling using Cron on a random controller method is not the right way?
  2. Heroku provides its own free Scheduler, but it doesn't guarrentee the Scheduler would run. So I am looking for the way to resolve it in the Api itself. Any help would be appreciated.

CodePudding user response:

You can develop a simple REST endpoint like /ping or /heath-check to get the status.

Sample

@RestController
public class PingRestController {

  @RequestMapping(method = RequestMethod.GET, path = "/api/ping")
  public ResponseEntity<String> getPing() {
    return ResponseEntity.ok("OK");
  }
}

As Spring-Boot has already Actuator support for health metrics, which you can call to check the status as well. Example

I believe you might already configured a scheduler. If not, then you can refer the link

  •  Tags:  
  • Related