...

Source file src/scheduler/queue/executor.go

Documentation: scheduler/queue

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package queue
    20  
    21  import (
    22  	"scheduler/config"
    23  	"scheduler/faas"
    24  	"scheduler/faas_openfaas"
    25  	"scheduler/log"
    26  	"scheduler/memdb"
    27  	"time"
    28  )
    29  
    30  // executeNow executes the passed job setting the memdb and unlocking both the job and the consumer semaphores
    31  func executeNow(job *QueuedJob) {
    32  	log.Log.Debugf("%s starting execution, with payload %t and type %s", job.Request.ServiceName, job.Request.Payload != nil, job.Request.PayloadContentType)
    33  
    34  	_ = memdb.SetFunctionRunning(job.Request.ServiceName, job.Request.ServiceType)
    35  
    36  	startExecutionTime := time.Now()
    37  
    38  	res, err := faas.FunctionExecute(job.Request.ServiceName, job.Request.Payload, job.Request.PayloadContentType)
    39  
    40  	if err != nil {
    41  		log.Log.Errorf("Cannot execute service %s: %s", job.Request.ServiceName, err.Error())
    42  		job.ErrorExecution = true
    43  	} else {
    44  		log.Log.Debugf("%s function executed", job.Request.ServiceName)
    45  
    46  		// save the res
    47  		job.Response = res
    48  		job.Timings.ExecutionTime = time.Since(startExecutionTime).Seconds()
    49  		if config.GetOpenFaasEnabled() {
    50  			job.Timings.FaasExecutionTime = faas_openfaas.GetDurationFromExecuteApiCallResponse(res)
    51  		}
    52  	}
    53  
    54  	_ = memdb.SetFunctionStopped(job.Request.ServiceName, job.Request.ServiceType)
    55  
    56  	// unlock the http request
    57  	job.Semaphore.Signal()
    58  
    59  	// unlock consumers
    60  	consumersSem.Signal()
    61  }
    62  

View as plain text