Class ServiceTask

All Implemented Interfaces:
Named, DefaultedDocPropertyHolder, DocPropertyHolder, Cloneable

public class ServiceTask extends ServiceConfigurationBase implements Cloneable, Named
The main base configuration for all services within the CloudNet cluster. A task is normally permanent stored somewhere to allow loads after a node restart. Therefore, service tasks are the main configuration point for everyone who needs either permanent storing of a service configuration or who doesn't work with the CloudNet api to start a service.
Since:
4.0
  • Field Details

    • NAMING_REGEX

      public static final String NAMING_REGEX
      The jvm static pattern which validate that a task or service name is acceptable. It for example doesn't allow slashes to prevent navigation out of the service directory itself (path traversal).
      See Also:
    • NAMING_PATTERN

      public static final Pattern NAMING_PATTERN
    • name

      private final String name
    • runtime

      private final String runtime
    • hostAddress

      private final String hostAddress
    • javaCommand

      private final String javaCommand
    • nameSplitter

      private final String nameSplitter
    • disableIpRewrite

      private final boolean disableIpRewrite
    • maintenance

      private final boolean maintenance
    • autoDeleteOnStop

      private final boolean autoDeleteOnStop
    • staticServices

      private final boolean staticServices
    • groups

      private final Set<String> groups
    • associatedNodes

      private final Set<String> associatedNodes
    • deletedFilesAfterStop

      private final Set<String> deletedFilesAfterStop
    • processConfiguration

      private final ProcessConfiguration processConfiguration
    • startPort

      private final int startPort
    • minServiceCount

      private final int minServiceCount
  • Constructor Details

    • ServiceTask

      protected ServiceTask(@NonNull @NonNull String name, @NonNull @NonNull String runtime, @Nullable @Nullable String hostAddress, @Nullable @Nullable String javaCommand, @NonNull @NonNull String nameSplitter, boolean disableIpRewrite, boolean maintenance, boolean autoDeleteOnStop, boolean staticServices, @NonNull @NonNull Set<String> groups, @NonNull @NonNull Set<String> associatedNodes, @NonNull @NonNull Set<String> deletedFilesAfterStop, @NonNull @NonNull ProcessConfiguration processConfiguration, int startPort, int minServiceCount, @NonNull @NonNull Set<ServiceTemplate> templates, @NonNull @NonNull Set<ServiceDeployment> deployments, @NonNull @NonNull Set<ServiceRemoteInclusion> includes, @NonNull @NonNull Document properties)
      Constructs a new service task instance.
      Parameters:
      name - the name of the service task.
      runtime - the runtime, used to determine the service factory for services of the task.
      hostAddress - the host address all services of this task are bound to.
      javaCommand - the overridden java command to use, if null the node specific one will be used.
      nameSplitter - the splitter to put into the service name, between the task name and service id.
      disableIpRewrite - true if the ip set in a service configuration file should not get touched.
      maintenance - true if the task should be in maintenance.
      autoDeleteOnStop - true if services based on the task should get deleted when stopping them.
      staticServices - true if services created based on the task should be static.
      groups - the groups to automatically include to all services started based on the task.
      associatedNodes - the nodes which are allowed to pickup services based on the task.
      deletedFilesAfterStop - the files which should automatically get deleted when stopping a service.
      processConfiguration - the process configuration of the task.
      startPort - the start port of the task.
      minServiceCount - the amount of services which should be online by default.
      templates - the templates to include on each service created based on the task.
      deployments - the deployments which should be added initially to each service.
      includes - the includes which should be added initially to each service.
      properties - the properties of the task.
      Throws:
      NullPointerException - if one of the parameters (except the java command) is null.
  • Method Details

    • builder

      @NonNull public static ServiceTask.Builder builder()
      Constructs a new builder for a service task.
      Returns:
      a new service task builder.
    • builder

      @NonNull public static ServiceTask.Builder builder(@NonNull @NonNull ServiceTask serviceTask)
      Constructs a new builder for a service task which has the same properties set than the given service task.

      When calling build directly after constructing a builder using this method, it will result in a service task which is equal but not the same as the given one.

      Parameters:
      serviceTask - the service task to copy the properties of.
      Returns:
      a builder for a service task with the properties of the given one already set.
      Throws:
      NullPointerException - if the given service task is null.
    • name

      @NonNull public @NonNull String name()
      Get the unique name identifier of this object in the related scope.
      Specified by:
      name in interface Named
      Returns:
      the unique name identifier of this object in the related scope.
    • runtime

      @NonNull public @NonNull String runtime()
      Get the runtime to use when services gets created based on this configuration. Runtimes are there to allow different types of configuration for running in different environments, for example inside a docker container. The given runtime must be registered on the node which picks up the service, if not it will result in an error.
      Returns:
      the runtime to use when creating a service based on this configuration.
    • hostAddress

      @Nullable public @Nullable String hostAddress()
      Get the host address that all services of this task are bound to. The host address is not required to be an ip address, it is possible that the host address is just an ip alias which needs to be resolved using the configuration of the node. The host address might be null, in that case the fallback host of the node is used.
      Returns:
      the host address all services of this task are bound to.
    • javaCommand

      @Nullable public @Nullable String javaCommand()
      Get the java command to use when starting a service. This can for example be used to use different java distros or versions on services. If no java command is set the java command configured in the node that is picking up the service will be used.
      Returns:
      the java command to use when starting services based on this configuration.
    • nameSplitter

      @NonNull public @NonNull String nameSplitter()
      Get the splitter to put between the name of the task and the numeric id of it when creating a full display name variant of the underlying service.
      Returns:
      the name splitter of the underlying service.
    • disableIpRewrite

      public boolean disableIpRewrite()
      Get if rewriting the ip set in the service configuration file (for example the server.properties file) should be disabled and the set ip should be used. Note that if disabled the node starting the service will not read the ip from the configuration, therefore the configured ip of the node is still used as the service address.
      Returns:
      true if the node starting a service based on the task should not change the ip in the service config.
    • maintenance

      public boolean maintenance()
      Get if this service task is in maintenance. This option is used to restrict for example who is able to join on a service. On the other hand, CloudNet will not try to start any service of tasks which are in maintenance even if the configured minimum service count is more than 0.
      Returns:
      true if this task is in maintenance, false otherwise.
    • autoDeleteOnStop

      public boolean autoDeleteOnStop()
      Get if services should be deleted when stopping the service. This does only mean that the service gets unregistered and is no longer available for starting, but does not mean that all service files get deleted when the service is static.

      If this option is false the service will be stopped and then go back to the prepared state, ready to get started again.

      Returns:
      if the service should get unregistered when stopping it.
    • staticServices

      public boolean staticServices()
      Get if services which get created based on this configuration are static. Files of static services will not get deleted when deleting the service. That means that starting the exact service after it was deleted, will result in the same service files to be present.

      NOTE: static services are not automatically synced between nodes, therefore a specific node to start the service on should be present, else it might result in a different configuration (or world) than expected.

      Returns:
      if services created based on this configuration should be static.
    • jvmOptions

      @NonNull public @NonNull Collection<String> jvmOptions()
      Get the jvm options which should get applied to the service command line. JVM options are there to configure the behaviour of the jvm, for example the garbage collector.
      Specified by:
      jvmOptions in class ServiceConfigurationBase
      Returns:
      the jvm options to set for services created based on this configuration.
    • processParameters

      @NonNull public @NonNull Collection<String> processParameters()
      Get the process parameters which should get appended to the command line. Process parameters are there to configure the application, for example setting an option like --online-mode=true.
      Specified by:
      processParameters in class ServiceConfigurationBase
      Returns:
      the process parameters to set for services created based on this configuration.
    • environmentVariables

      @NonNull public @Unmodifiable @NonNull Map<String,String> environmentVariables()
      Get the environment variables which should get appended to all environments of services which are created based on this configuration.
      Specified by:
      environmentVariables in class ServiceConfigurationBase
      Returns:
      the environment variables to set for services created based on this configuration.
    • groups

      @NonNull public @NonNull Collection<String> groups()
      Get the names of the groups whose configuration should get included when starting a service based on this configuration. Each configuration is only included when it is present on the node starting the service, if not it will silently be ignored. Note that groups targeting the same environment as this configuration will get included automatically.
      Returns:
      the names of the group configurations to include their configurations before starting.
    • associatedNodes

      @NonNull public @NonNull Collection<String> associatedNodes()
      Get the unique ids of all nodes which are allowed to pick up and manage the underlying service. This method returns an empty collection if all nodes are allowed to pick up the service, but never null.

      Note: this method might return an empty collection if a specific node was already chosen, you might need to check that too.

      Returns:
      the unique ids of the nodes which are allowed to pick up and manage the underlying service.
    • deletedFilesAfterStop

      @NonNull public @NonNull Collection<String> deletedFilesAfterStop()
      Get the files (or directories) which should get deleted when stopping the service (before deployments are executed). Trying to delete files outside the service directory will result in an exception.
      Returns:
      a set of path to files/directories which should get deleted when stopping a service based on this config.
    • processConfiguration

      @NonNull public @NonNull ProcessConfiguration processConfiguration()
      Get the process configuration to apply to all services which get created based on this configuration.
      Returns:
      the process configuration to apply to all services.
    • startPort

      public @Range(from=1L,to=65535L) int startPort()
      The port of the service to start on. If the given port is already taken it will be counted up until it reaches the port limit (65535) or one of the ports in between is free to be taken. Port changes because of counting up will not be reflected into this configuration and vice-versa.
      Returns:
      the port number to start the service on, might be counted up when the port is already taken.
    • minServiceCount

      public @Range(from=0L,to=2147483647L) int minServiceCount()
      Get the amount of services which should be always online if this task is not in maintenance. CloudNet tries to start services until the minimum amount of them is online if possible, if for example no nodes are online which are able to pick up the service, the minimum service count might not get reached.
      Returns:
      the minimum amount of services which CloudNet should try to keep online.
    • clone

      @NonNull public @NonNull ServiceTask clone()
      Overrides:
      clone in class Object