generator: Can perform synchronous generation
This commit is contained in:
parent
ec98e521dc
commit
1769938205
13 changed files with 214 additions and 81 deletions
|
@ -1,25 +1,21 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/generation"
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var GeneratorSocket string
|
||||
|
||||
func declareClaimsRoutes(router *gin.RouterGroup) {
|
||||
// Tasks
|
||||
router.GET("/claims", getClaims)
|
||||
|
@ -292,7 +288,7 @@ func clearClaims(c *gin.Context) {
|
|||
}
|
||||
|
||||
func generateTeamIssuesFile(team fic.Team) error {
|
||||
if GeneratorSocket == "" {
|
||||
if generation.GeneratorSocket == "" {
|
||||
if my, err := team.MyIssueFile(); err != nil {
|
||||
return fmt.Errorf("Unable to generate issue FILE (tid=%d): %w", team.Id, err)
|
||||
} else if j, err := json.Marshal(my); err != nil {
|
||||
|
@ -301,35 +297,16 @@ func generateTeamIssuesFile(team fic.Team) error {
|
|||
return fmt.Errorf("Unable to write issues' file: %w", err)
|
||||
}
|
||||
} else {
|
||||
buf, err := json.Marshal(fic.GenStruct{Type: fic.GenTeamIssues, TeamId: team.Id})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Something is wrong with JSON encoder: %w", err)
|
||||
}
|
||||
|
||||
sockType := "unix"
|
||||
if strings.Contains(GeneratorSocket, ":") {
|
||||
sockType = "tcp"
|
||||
}
|
||||
|
||||
socket, err := net.Dial(sockType, GeneratorSocket)
|
||||
resp, err := generation.PerformGeneration(fic.GenStruct{Type: fic.GenTeamIssues, TeamId: team.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer socket.Close()
|
||||
defer resp.Body.Close()
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: func(network, addr string) (net.Conn, error) {
|
||||
return socket, nil
|
||||
},
|
||||
},
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
v, _ := ioutil.ReadAll(resp.Body)
|
||||
return fmt.Errorf("%s", string(v))
|
||||
}
|
||||
|
||||
resp, err := httpClient.Post("http://localhost/enqueue", "application/json", bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to enqueue new generation event: %w", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in a new issue