class JobHelper {

    public static long calculateRuntimeDuration(long maxRuntimeSecs, Instant startedAt) {
        long terminateJobInSecs = maxRuntimeSecs;
        if (maxRuntimeSecs > 0) {
            Instant now = Instant.now();

            if (now.isAfter(startedAt)) {
                // Job was already running (Occurs when master was restarted)
                long elapsedSeconds = now.getEpochSecond() - startedAt.getEpochSecond();
                // Calculate remaining time to run
                terminateJobInSecs = maxRuntimeSecs - elapsedSeconds;

                if (terminateJobInSecs <= 0) {
                    // Runtime has already reached terminate.
                    // trigger terminate in a second.
                    terminateJobInSecs = 1;

                }
            }

        }
        return terminateJobInSecs;
    }

    private  JobHelper();

}