package org.jimmutable.cloud.utils;

import org.jimmutable.cloud.ApplicationId;
import org.jimmutable.cloud.CloudExecutionEnvironment;
import org.jimmutable.cloud.cache.CacheKey;

/**
 * This class is meant to run on start up of any Jimmutable Cloud application.
 * At startup of CEE, we kick off a background thread that will consistently
 * ping a cache key with a heartbeat. Users can then check if an application has
 * reported a heartbeat in recent time via ApplicationHeartbeatUtils.
 */
public class ApplicationHeartbeat implements Runnable
{
	private ApplicationId application_id;
	private ApplicationId application_sub_service_id;

	ApplicationHeartbeat( ApplicationId application_id, ApplicationId application_sub_service_id )
	{
		this.application_id = application_id;
		this.application_sub_service_id = application_sub_service_id;
		runStartbeat();
	}
	
	/*
	 * This just gives us a marker for when this application first started
	 */
	private void runStartbeat()
	{
		CacheKey application_cache_key = ApplicationHeartbeatUtils.createStartbeatCacheKey(application_id, application_sub_service_id);
		String current_time = Long.toString(System.currentTimeMillis());
		CloudExecutionEnvironment.getSimpleCurrent().getSimpleCacheService().put(application_cache_key, current_time);
	}

	@Override
	public void run()
	{
		CacheKey application_cache_key = ApplicationHeartbeatUtils.createHeartbeatCacheKey(application_id, application_sub_service_id);
		String current_time = Long.toString(System.currentTimeMillis());
		CloudExecutionEnvironment.getSimpleCurrent().getSimpleCacheService().put(application_cache_key, current_time);
	}

}
@@ -840,21 +840,111 @@
 e_id;%0A%09%09
-runSt
+// Kick off the beginning marker%0A%09%09sendStartbeat();%0A%09%09// Kick off an initial heartbeat%0A%09%09sendHe
 artbeat(
@@ -953,10 +953,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09/*%0A
@@ -1045,19 +1045,20 @@
 te void 
-run
+send
 Startbea
@@ -1383,34 +1383,34 @@
 %7D%0A%0A%09
-@Override%0A%09public void run
+private void sendHeartbeat
 ()%0A%09
@@ -1600,32 +1600,32 @@
 tTimeMillis());%0A
-
 %09%09CloudExecution
@@ -1725,9 +1725,65 @@
 e);%0A%09%7D%0A%0A
+%09@Override%0A%09public void run()%0A%09%7B%0A%09%09sendHeartbeat();%0A%09%7D%0A%0A
 %7D

Add an initial heartbeat before the delay so we catch any fast running services before they terminate