Class TaskStatusUpdater
java.lang.Object
io.cloudreactor.tasksymphony.wrapperio.TaskStatusUpdater
- All Implemented Interfaces:
AutoCloseable
This class allows JVM processes managed by the CloudReactor wrapper script
to send their status by communicating with the wrapper script, which in
turn, communicates with the CloudReactor API server. Communication with the
wrapper script is done over UDP sockets, so it is unreliable in theory, but
in practice, since it's all on localhost, it's pretty reliable.
This class can be created in a disabled state, in which case all operations
are no-ops.
- Since:
- 0.1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe default number of attempts allowed for each call to updateStatus().static final intThe default port used to communicated with the wrapper script (2373). -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new instance, using the environment variablesPROC_WRAPPER_ENABLE_STATUS_UPDATE_LISTENER(which should be mapped toTRUEto enable) andPROC_WRAPPER_STATUS_UPDATE_SOCKET_PORT(which should be mapped to a valid port number) to set the enabled flag and the port number, respectively.TaskStatusUpdater(boolean isEnabled, int outboundPort, Integer bindPort) Create a new instance, that optionally disabled and uses the argument port for communication with the wrapper script.TaskStatusUpdater(int outboundPort) Create a new instance, that is enabled and uses the argument port for communication with the wrapper script. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the underlying socket if it exists.Return the port this instance binds to.intReturn the port used to communicate with the wrapper script.booleanReturn true if communication is enabled.protected byte[]makeMessage(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) Return a byte array that encodes the parameters for a wrapper script to read.booleansendUpdate(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) Send an update message to the process wrapper script, using the default values for retries, timeout, and backoff duration.booleansendUpdate(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps, Long maxRetries, Long timeoutMillis, long backoffDurationMillis) Send an update message to the process wrapper script, if status updates are enabled.booleansendUpdateAndIgnoreError(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) Send an update message to the process wrapper script, using the default values or retries, timeout, and backoff duration.
-
Field Details
-
DEFAULT_STATUS_UPDATE_PORT
public static final int DEFAULT_STATUS_UPDATE_PORTThe default port used to communicated with the wrapper script (2373).- See Also:
-
DEFAULT_MAX_ATTEMPTS
public static final long DEFAULT_MAX_ATTEMPTSThe default number of attempts allowed for each call to updateStatus().- See Also:
-
-
Constructor Details
-
TaskStatusUpdater
public TaskStatusUpdater()Create a new instance, using the environment variablesPROC_WRAPPER_ENABLE_STATUS_UPDATE_LISTENER(which should be mapped toTRUEto enable) andPROC_WRAPPER_STATUS_UPDATE_SOCKET_PORT(which should be mapped to a valid port number) to set the enabled flag and the port number, respectively.- Since:
- 0.1.0
-
TaskStatusUpdater
Create a new instance, that optionally disabled and uses the argument port for communication with the wrapper script.- Parameters:
isEnabled- true to enable communication, false to disableoutboundPort- The port used to communicate with the wrapper script using UDP sockets.bindPort- The port the socket is bound to. Should be different from the outbound port. If null, any available port will be used.- Throws:
IllegalArgumentException- if either port number is invalid- Since:
- 0.1.0
-
TaskStatusUpdater
public TaskStatusUpdater(int outboundPort) Create a new instance, that is enabled and uses the argument port for communication with the wrapper script.- Parameters:
outboundPort- The port used to communicate with the wrapper script using UDP sockets.- Throws:
IllegalArgumentException- if the port number is invalid- Since:
- 0.1.0
-
-
Method Details
-
sendUpdate
public boolean sendUpdate(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps, Long maxRetries, Long timeoutMillis, long backoffDurationMillis) throws UpdateException, TimeoutException, InterruptedExceptionSend an update message to the process wrapper script, if status updates are enabled. The wrapper script will coalesce updates to save bandwidth so this method can be called as often as desired. This method can also be called concurrently by multiple threads.- Parameters:
successCount- The number of successful items. If null, no value will be sent.errorCount- The number of unsuccessful items. If null, no value will be sent.skippedCount- The number of skipped items. If null, no value will be sent.expectedCount- The number of expected items. If null, no value will be sent.lastStatusMessage- A message indicating the last status. If null, no value will be sent.extraProps- A map containing string keys mapped to additional properties to send. Each property value must be something serializable in JSON, including lists and dictionaries. If null, no additional properties will be sent.maxRetries- If null, the default number of attempts will be used (10). If non-null and non-negative, the value is the maximum number of times an update will be retried before throwing aTimeoutException. If non-null and negative, the update will be retried indefinitely until successful, subject to the timeout limit.timeoutMillis- If null, the default timeout will be used (10 minutes). If non-null and non-negative, the value is the maximum duration this call can take before throwing aMaxRetriesExceedException. If non-null and negative, no time limit will apply.backoffDurationMillis- The duration to wait, in milliseconds, after an unsuccessful socket operation, before the next retry. If -1, the default duration will be used.- Returns:
- true if the update succeeded, false if status updates are disabled.
- Throws:
MessageConversionException- ifextraPropscontains values that cannot be serialized to JSON.MaxRetriesExceededException- if the number of allowed retries was exceededTimeoutException- if the operation timed outInterruptedException- if the current thread is interrupted while waiting after an IOException occurredUpdateException- Since:
- 0.1.0
-
sendUpdate
public boolean sendUpdate(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) throws UpdateException, TimeoutException, InterruptedExceptionSend an update message to the process wrapper script, using the default values for retries, timeout, and backoff duration.- Parameters:
successCount- The number of successful items. If null, no value will be sent.errorCount- The number of unsuccessful items. If null, no value will be sent.skippedCount- The number of skipped items. If null, no value will be sent.expectedCount- The number of expected items. If null, no value will be sent.lastStatusMessage- A message indicating the last status. If null, no value will be sent.extraProps- A map containing string keys mapped to additional properties to send Each property value must be something serializable in JSON, including lists and dictionaries. If null, no additional properties will be sent.- Returns:
- true if the update succeeded, false if status updates are disabled.
- Throws:
MessageConversionException- ifextraPropscontains values that cannot be serialized to JSON.MaxRetriesExceededException- if the number of allowed retries was exceededTimeoutException- if the operation timed outInterruptedException- if the current thread is interrupted while waiting after an IOException occurredUpdateException- Since:
- 0.1.0
-
sendUpdateAndIgnoreError
public boolean sendUpdateAndIgnoreError(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) Send an update message to the process wrapper script, using the default values or retries, timeout, and backoff duration. Swallow the exceptions that could result from failing to send the update, retuning false instead.- Parameters:
successCount- The number of successful items. If null, no value will be sent.errorCount- The number of unsuccessful items. If null, no value will be sent.skippedCount- The number of skipped items. If null, no value will be sent.expectedCount- The number of expected items. If null, no value will be sent.lastStatusMessage- A message indicating the last status. If null, no value will be sent.extraProps- A map containing string keys mapped to additional properties to send. Each property value must be something serializable in JSON, including lists and dictionaries. If null, no additional properties will be sent.- Returns:
- true if the update succeeded, false if status updates are disabled or if the update failed.
- Since:
- 0.4.0
-
isEnabled
public boolean isEnabled()Return true if communication is enabled.- Returns:
- true if communication is enabled
- Since:
- 0.1.0
-
getOutboundPort
public int getOutboundPort()Return the port used to communicate with the wrapper script.- Returns:
- the port used to communicate with the wrapper script.
- Since:
- 0.1.0
-
getBindPort
Return the port this instance binds to. If this instance binds to any available port, return null.- Returns:
- the port this instance binds to, or null if this instance binds to any available port
- Since:
- 0.1.0
-
close
public void close()Close the underlying socket if it exists.- Specified by:
closein interfaceAutoCloseable- Since:
- 0.1.0
-
makeMessage
protected byte[] makeMessage(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String, Object> extraProps) throws MessageConversionExceptionReturn a byte array that encodes the parameters for a wrapper script to read. This is normally a JSON-encoded dictionary, with a newline at the end.- Parameters:
successCount- The number of successful items. If null, no value will be sent.errorCount- The number of unsuccessful items. If null, no value will be sent.skippedCount- The number of skipped items. If null, no value will be sent.expectedCount- The number of expected items. If null, no value will be sent.lastStatusMessage- A message indicating the last status. If null, no value will be sent.extraProps- A map containing string keys mapped to additional properties to send. Each property value must be something serializable in JSON, including lists and dictionaries. If null, no additional properties will be sent.- Returns:
- A byte array that encodes the parameters
- Throws:
MessageConversionException- if the message cannot be encoded
-