Class TaskStatusUpdater

java.lang.Object
io.cloudreactor.tasksymphony.wrapperio.TaskStatusUpdater
All Implemented Interfaces:
AutoCloseable

public class TaskStatusUpdater extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The default number of attempts allowed for each call to updateStatus().
    static final int
    The default port used to communicated with the wrapper script (2373).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance, using the environment variables PROC_WRAPPER_ENABLE_STATUS_UPDATE_LISTENER (which should be mapped to TRUE to enable) and PROC_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 Type
    Method
    Description
    void
    Close the underlying socket if it exists.
    Return the port this instance binds to.
    int
    Return the port used to communicate with the wrapper script.
    boolean
    Return 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.
    boolean
    sendUpdate(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.
    boolean
    sendUpdate(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.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_STATUS_UPDATE_PORT

      public static final int DEFAULT_STATUS_UPDATE_PORT
      The default port used to communicated with the wrapper script (2373).
      See Also:
    • DEFAULT_MAX_ATTEMPTS

      public static final long DEFAULT_MAX_ATTEMPTS
      The default number of attempts allowed for each call to updateStatus().
      See Also:
  • Constructor Details

    • TaskStatusUpdater

      public TaskStatusUpdater()
      Create a new instance, using the environment variables PROC_WRAPPER_ENABLE_STATUS_UPDATE_LISTENER (which should be mapped to TRUE to enable) and PROC_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

      public 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.
      Parameters:
      isEnabled - true to enable communication, false to disable
      outboundPort - 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, InterruptedException
      Send 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 a TimeoutException. 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 a MaxRetriesExceedException. 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 - if extraProps contains values that cannot be serialized to JSON.
      MaxRetriesExceededException - if the number of allowed retries was exceeded
      TimeoutException - if the operation timed out
      InterruptedException - if the current thread is interrupted while waiting after an IOException occurred
      UpdateException
      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, InterruptedException
      Send 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 - if extraProps contains values that cannot be serialized to JSON.
      MaxRetriesExceededException - if the number of allowed retries was exceeded
      TimeoutException - if the operation timed out
      InterruptedException - if the current thread is interrupted while waiting after an IOException occurred
      UpdateException
      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

      public Integer 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:
      close in interface AutoCloseable
      Since:
      0.1.0
    • makeMessage

      protected byte[] makeMessage(Long successCount, Long errorCount, Long skippedCount, Long expectedCount, String lastStatusMessage, Map<String,Object> extraProps) throws MessageConversionException
      Return 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