Search in sources :

Example 6 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setLowPrioritySettings.

@ApiOperation(value = "Update low priority service settings", notes = "Only corePoolSize and maximumPoolSize are used")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/low-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setLowPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            int currentCorePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE);
            if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, model.getCorePoolSize() == null ? currentCorePoolSize : model.getCorePoolSize())) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    Common.backgroundProcessing.setLowPriorityServiceCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = Common.backgroundProcessing.getLowPriorityServiceCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() == null) {
                    // Get the info for the user
                    int maximumPoolSize = Common.backgroundProcessing.getLowPriorityServiceMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = Common.backgroundProcessing.getLowPriorityServiceActiveCount();
                int largestPoolSize = Common.backgroundProcessing.getLowPriorityServiceLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set low priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method getHighPriorityThreadPoolSettings.

@ApiOperation(value = "Get the High Priority Service Thread Pool Settings", notes = "active count and largest pool size are read only")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/high-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> getHighPriorityThreadPoolSettings(HttpServletRequest request) {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            ThreadPoolExecutor executor = (ThreadPoolExecutor) Common.timer.getExecutorService();
            int corePoolSize = executor.getCorePoolSize();
            int maximumPoolSize = executor.getMaximumPoolSize();
            int activeCount = executor.getActiveCount();
            int largestPoolSize = executor.getLargestPoolSize();
            ThreadPoolSettingsModel model = new ThreadPoolSettingsModel(corePoolSize, maximumPoolSize, activeCount, largestPoolSize);
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to access high priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (com.serotonin.m2m2.vo.User)6 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)6 ThreadPoolSettingsModel (com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 RestValidationMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage)4 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2