From d0e147137d592d469a3c0fe48ebfd48380c4b302 Mon Sep 17 00:00:00 2001 From: "Miguel A. C" <30386061+doncicuto@users.noreply.github.com> Date: Thu, 7 Dec 2017 21:05:45 +0100 Subject: [PATCH] feat(service): add restart policy options in service create/details (#1479) --- .../createService/createServiceController.js | 18 ++- .../createService/createservice.html | 71 +--------- .../includes/update-restart.html | 132 ++++++++++++++++++ app/components/service/includes/restart.html | 10 +- app/components/service/serviceController.js | 10 +- app/helpers/serviceHelper.js | 2 +- 6 files changed, 161 insertions(+), 82 deletions(-) create mode 100644 app/components/createService/includes/update-restart.html diff --git a/app/components/createService/createServiceController.js b/app/components/createService/createServiceController.js index 32b7e5c78..ab85f14ed 100644 --- a/app/components/createService/createServiceController.js +++ b/app/components/createService/createServiceController.js @@ -35,7 +35,11 @@ function ($q, $scope, $state, $timeout, Service, ServiceHelper, ConfigService, C MemoryLimit: 0, MemoryReservation: 0, MemoryLimitUnit: 'MB', - MemoryReservationUnit: 'MB' + MemoryReservationUnit: 'MB', + RestartCondition: 'any', + RestartDelay: '5s', + RestartMaxAttempts: 0, + RestartWindow: '0s' }; $scope.state = { @@ -243,12 +247,21 @@ function ($q, $scope, $state, $timeout, Service, ServiceHelper, ConfigService, C function prepareUpdateConfig(config, input) { config.UpdateConfig = { Parallelism: input.Parallelism || 0, - Delay: ServiceHelper.translateHumanDurationToNanos(input.UpdateDelay), + Delay: ServiceHelper.translateHumanDurationToNanos(input.UpdateDelay) || 0, FailureAction: input.FailureAction, Order: input.UpdateOrder }; } + function prepareRestartPolicy(config, input) { + config.TaskTemplate.RestartPolicy = { + Condition: input.RestartCondition || 'any', + Delay: ServiceHelper.translateHumanDurationToNanos(input.RestartDelay) || 5000000000, + MaxAttempts: input.RestartMaxAttempts || 0, + Window: ServiceHelper.translateHumanDurationToNanos(input.RestartWindow) || 0 + }; + } + function preparePlacementConfig(config, input) { config.TaskTemplate.Placement.Constraints = ServiceHelper.translateKeyValueToPlacementConstraints(input.PlacementConstraints); config.TaskTemplate.Placement.Preferences = ServiceHelper.translateKeyValueToPlacementPreferences(input.PlacementPreferences); @@ -348,6 +361,7 @@ function ($q, $scope, $state, $timeout, Service, ServiceHelper, ConfigService, C preparePlacementConfig(config, input); prepareResourcesCpuConfig(config, input); prepareResourcesMemoryConfig(config, input); + prepareRestartPolicy(config, input); return config; } diff --git a/app/components/createService/createservice.html b/app/components/createService/createservice.html index 426696e04..9a86ee73c 100644 --- a/app/components/createService/createservice.html +++ b/app/components/createService/createservice.html @@ -130,7 +130,7 @@
  • Volumes
  • Network
  • Labels
  • -
  • Update config
  • +
  • Update config & Restart
  • Secrets
  • Configs
  • Resources & Placement
  • @@ -372,74 +372,7 @@ -
    -
    - -
    - -
    - -
    -
    -

    - Maximum number of tasks to be updated simultaneously (0 to update all at once). -

    -
    -
    - - -
    - -
    - -
    -
    -

    - Amount of time between updates expressed by a number followed by unit (ns|us|ms|s|m|h). Example: 1m. -

    -
    -
    - - -
    - -
    -
    - - -
    -
    -
    -

    - Action taken on failure to start after update. -

    -
    - -
    - - -
    - - -
    -
    - - -
    -
    -
    -

    - Operation order on failure. -

    -
    - -
    - -
    -
    +
    diff --git a/app/components/createService/includes/update-restart.html b/app/components/createService/includes/update-restart.html new file mode 100644 index 000000000..da1529b20 --- /dev/null +++ b/app/components/createService/includes/update-restart.html @@ -0,0 +1,132 @@ +
    +
    + Update config +
    + +
    + +
    + +
    +
    +

    + Maximum number of tasks to be updated simultaneously (0 to update all at once). +

    +
    +
    + + +
    + +
    + +
    +
    +

    + Amount of time between updates expressed by a number followed by unit (ns|us|ms|s|m|h). Default value is 0s, 0 seconds. +

    +
    +
    + + +
    + +
    +
    + + +
    +
    +
    +

    + Action taken on failure to start after update. +

    +
    +
    + + +
    + +
    +
    + + +
    +
    +
    +

    + Operation order on failure. +

    +
    +
    + + +
    + Restart policy +
    + +
    + +
    +
    + + + +
    +
    +
    +

    + Restart when condition is met (default condition "any"). +

    +
    +
    + + +
    + +
    + +
    +
    +

    + Delay between restart attempts expressed by a number followed by unit (ns|us|ms|s|m|h). Default value is 5s, 5 seconds. +

    +
    +
    + + +
    + +
    + +
    +
    +

    + Maximum attempts to restart a given task before giving up (default value is 0, which means unlimited). +

    +
    +
    + + +
    + +
    + +
    +
    +

    + Time window to evaluate restart attempts expressed by a number followed by unit (ns|us|ms|s|m|h). Default value is 0 seconds, which is unbounded. +

    +
    +
    + +
    \ No newline at end of file diff --git a/app/components/service/includes/restart.html b/app/components/service/includes/restart.html index 1e5293bda..e42de0173 100644 --- a/app/components/service/includes/restart.html +++ b/app/components/service/includes/restart.html @@ -25,11 +25,11 @@ Restart delay - +

    - Delay between restart attempts. Time in seconds. + Delay between restart attempts expressed by a number followed by unit (ns|us|ms|s|m|h). Default value is 5s, 5 seconds.

    @@ -40,18 +40,18 @@

    - Maximum attempts to restart a given container before giving up (default value is 0, which is ignored). + Maximum attempts to restart a given task before giving up (default value is 0, which means unlimited).

    Restart window - +

    - The time window used to evaluate the restart policy (default value is 0, which is unbounded). Time in seconds. + Time window to evaluate restart attempts expressed by a number followed by unit (ns|us|ms|s|m|h). Default value is 0 seconds, which is unbounded.

    diff --git a/app/components/service/serviceController.js b/app/components/service/serviceController.js index 6c3ea83c6..45518e02b 100644 --- a/app/components/service/serviceController.js +++ b/app/components/service/serviceController.js @@ -232,16 +232,16 @@ function ($q, $scope, $transition$, $state, $location, $timeout, $anchorScroll, config.UpdateConfig = { Parallelism: service.UpdateParallelism, - Delay: ServiceHelper.translateHumanDurationToNanos(service.UpdateDelay), + Delay: ServiceHelper.translateHumanDurationToNanos(service.UpdateDelay) || 0, FailureAction: service.UpdateFailureAction, Order: service.UpdateOrder }; config.TaskTemplate.RestartPolicy = { Condition: service.RestartCondition, - Delay: service.RestartDelay * 1000000000, + Delay: ServiceHelper.translateHumanDurationToNanos(service.RestartDelay) || 5000000000, MaxAttempts: service.RestartMaxAttempts, - Window: service.RestartWindow * 1000000000 + Window: ServiceHelper.translateHumanDurationToNanos(service.RestartWindow) || 0 }; if (service.Ports) { @@ -310,8 +310,8 @@ function ($q, $scope, $transition$, $state, $location, $timeout, $anchorScroll, } function transformDurations(service) { - service.RestartDelay = service.RestartDelay / 1000000000 || 5; - service.RestartWindow = service.RestartWindow / 1000000000 || 0; + service.RestartDelay = ServiceHelper.translateNanosToHumanDuration(service.RestartDelay) || '5s'; + service.RestartWindow = ServiceHelper.translateNanosToHumanDuration(service.RestartWindow) || '0s'; service.UpdateDelay = ServiceHelper.translateNanosToHumanDuration(service.UpdateDelay) || '0s'; } diff --git a/app/helpers/serviceHelper.js b/app/helpers/serviceHelper.js index 0903166d4..b25def767 100644 --- a/app/helpers/serviceHelper.js +++ b/app/helpers/serviceHelper.js @@ -142,7 +142,7 @@ angular.module('portainer.helpers').factory('ServiceHelper', [function ServiceHe }; helper.translateHumanDurationToNanos = function(humanDuration) { - var nanos = 0; + var nanos; var regex = /^([0-9]+)(h|m|s|ms|us|ns)$/i; var matches = humanDuration.match(regex);