...
  
    Source file
    src/scheduler/api/configuration.go
  
  
     1  
    18  
    19  package api
    20  
    21  import (
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"scheduler/config"
    26  	"scheduler/errors"
    27  	"scheduler/log"
    28  	"scheduler/scheduler"
    29  	"scheduler/types"
    30  	"scheduler/utils"
    31  )
    32  
    33  
    34  func GetConfiguration(w http.ResponseWriter, r *http.Request) {
    35  	configuration, err := json.Marshal(config.GetConfigurationDynamicCopy())
    36  	if err != nil {
    37  		log.Log.Errorf("Cannot encode configuration to json")
    38  		errors.ReplyWithError(&w, errors.GenericError, nil)
    39  		return
    40  	}
    41  
    42  	utils.HttpSendJSONResponse(&w, 200, string(configuration), nil)
    43  }
    44  
    45  
    46  
    47  func SetConfiguration(w http.ResponseWriter, r *http.Request) {
    48  	currentConfiguration := config.GetConfigurationDynamicCopy()
    49  	reqBody, _ := ioutil.ReadAll(r.Body)
    50  
    51  	var newConfiguration *config.ConfigurationDynamic
    52  	var err error
    53  
    54  	
    55  	err = json.Unmarshal(reqBody, ¤tConfiguration)
    56  	newConfiguration = currentConfiguration
    57  	if err != nil {
    58  		log.Log.Errorf("Cannot encode passed configuration: %s", err)
    59  		errors.ReplyWithError(&w, errors.GenericError, nil)
    60  		return
    61  	}
    62  
    63  	config.SetRunningFunctionMax(newConfiguration.ParallelRunningFunctionsMax)
    64  	config.SetQueueLengthMax(newConfiguration.QueueLengthMax)
    65  	config.SetQueueEnabled(newConfiguration.QueueEnabled)
    66  
    67  	
    68  	err = config.SaveConfigurationDynamicToConfigFile()
    69  	if err != nil {
    70  		log.Log.Warningf("Cannot save configuration to file %s", config.GetConfigFilePath())
    71  	}
    72  
    73  	log.Log.Infof("Configuration updated")
    74  
    75  	w.WriteHeader(200)
    76  }
    77  
    78  
    79  func GetScheduler(w http.ResponseWriter, r *http.Request) {
    80  	sched, err := json.Marshal(scheduler.GetScheduler())
    81  	if err != nil {
    82  		log.Log.Errorf("Cannot encode configuration to json")
    83  		errors.ReplyWithError(&w, errors.GenericError, nil)
    84  		return
    85  	}
    86  
    87  	utils.HttpSendJSONResponse(&w, 200, string(sched), nil)
    88  }
    89  
    90  
    91  func SetScheduler(w http.ResponseWriter, r *http.Request) {
    92  	var proposedScheduler = types.SchedulerDescriptor{}
    93  	reqBody, _ := ioutil.ReadAll(r.Body)
    94  
    95  	err := json.Unmarshal(reqBody, &proposedScheduler)
    96  	if err != nil {
    97  		log.Log.Errorf("Cannot decode passed configuration: %s", err.Error())
    98  		errors.ReplyWithError(&w, errors.InputNotValid, nil)
    99  		return
   100  	}
   101  
   102  	err = scheduler.SetScheduler(&proposedScheduler)
   103  	if err != nil {
   104  		log.Log.Errorf("Cannot set new scheduler: %s", err.Error())
   105  		errors.ReplyWithErrorMessage(&w, errors.GenericError, err.Error(), nil)
   106  		return
   107  	}
   108  
   109  	
   110  	err = config.SaveConfigurationSchedulerToConfigFile(scheduler.GetScheduler())
   111  	if err != nil {
   112  		log.Log.Errorf("Cannot save configuration to file %s", config.GetConfigSchedulerFilePath())
   113  	}
   114  
   115  	log.Log.Infof("Configuration updated with scheduler: %s", scheduler.GetName())
   116  
   117  	w.WriteHeader(200)
   118  }
   119  
View as plain text