Andrea Fazzi пре 5 година
родитељ
комит
5049216437

+ 2 - 1
generator/generators.go

@@ -1,7 +1,8 @@
 package generator
 
 type Generator interface {
-	Run(jobId uint)
+	Run()
+	SetJobId(id uint)
 }
 
 var Generators map[string]Generator

+ 23 - 15
generator/generators/list/list.go

@@ -7,6 +7,7 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+	"sync"
 	"text/template"
 	"time"
 
@@ -22,11 +23,10 @@ import (
 )
 
 type ListGenerator struct {
+	sync.Mutex
 	Config *config.ConfigT
-}
 
-func NewListGenerator(config *config.ConfigT) *ListGenerator {
-	return &ListGenerator{config}
+	JobID uint
 }
 
 var funcMap template.FuncMap = template.FuncMap{
@@ -47,7 +47,7 @@ func (generator *ListGenerator) prepareRemoteFolder() {
 func (generator *ListGenerator) generate(outputPath string, teachers []*orm.Teacher, funcMap template.FuncMap) {
 	filename := filepath.Join(outputPath, "elenco_docenti")
 	// tplContent, err := ncClient.Download("")
-	tpl, err := tpl_util.LoadTextTemplateFromString("./generator/generators/list/list.tpl.md", funcMap)
+	tpl, err := tpl_util.LoadTextTemplate("./generator/generators/list/list.tpl.md", funcMap)
 	if err != nil {
 		panic(err)
 	}
@@ -129,11 +129,21 @@ func groupClasses(classes []*orm.Class) string {
 	return strings.Join(groups, ",")
 }
 
-func (generator *ListGenerator) Run(jobId uint) {
+func NewListGenerator(config *config.ConfigT) *ListGenerator {
+	return &ListGenerator{Config: config}
+}
+
+func (generator *ListGenerator) SetJobId(id uint) {
+	generator.Lock()
+	generator.JobID = id
+	generator.Unlock()
+}
+
+func (generator *ListGenerator) Run() {
 	ncClient, err := cloud.Dial(
-		config.Config.Cloud.Url,
-		config.Config.Cloud.Username,
-		config.Config.Cloud.Password,
+		generator.Config.Cloud.Url,
+		generator.Config.Cloud.Username,
+		generator.Config.Cloud.Password,
 	)
 	if err != nil {
 		panic(err)
@@ -141,15 +151,15 @@ func (generator *ListGenerator) Run(jobId uint) {
 
 	log.Printf("Connecting to karmen...")
 	client, err := karmen_client.Dial(
-		config.Config.Url,
-		config.Config.Admin.Username,
-		config.Config.Admin.Password,
+		generator.Config.Url,
+		generator.Config.Admin.Username,
+		generator.Config.Admin.Password,
 	)
 	if err != nil {
 		log.Println(err)
 	}
 
-	job, err := client.GetJob(jobId)
+	job, err := client.GetJob(generator.JobID)
 	if err != nil {
 		panic(err)
 	}
@@ -165,7 +175,7 @@ func (generator *ListGenerator) Run(jobId uint) {
 		panic(err)
 	}
 
-	outputPath := filepath.Join(config.Config.Documents.OutputPath, fmt.Sprintf("%d/%d", job.DocumentID, job.ID))
+	outputPath := filepath.Join(generator.Config.Documents.OutputPath, fmt.Sprintf("%d/%d", job.DocumentID, job.ID))
 
 	if err := os.MkdirAll(outputPath, 0777); err != nil {
 		panic(err)
@@ -177,8 +187,6 @@ func (generator *ListGenerator) Run(jobId uint) {
 
 	job.Files = append(job.Files, &orm.File{Path: "elenco_docenti.pdf"})
 
-	time.Sleep(10 * time.Second)
-
 	err = client.UpdateJob(job)
 	if err != nil {
 		panic(err)

+ 12 - 0
generator/generators/list/main/config.yaml

@@ -0,0 +1,12 @@
+document_name: "Elenco docenti"
+url: "http://localhost:3000"
+domain: "foo.org"
+log_level: 2
+
+cloud:
+  username: admin
+  password: password
+  url: "http://localhost:8080/remote.php/webdav/"
+  
+documents:
+  output_path: "output"

+ 21 - 0
generator/generators/list/main/main.go

@@ -0,0 +1,21 @@
+package main
+
+import (
+	"gogs.carducci-dante.gov.it/karmen/core/config"
+	"gogs.carducci-dante.gov.it/karmen/core/generator/generators/list"
+)
+
+type Config struct {
+	*config.ConfigT
+
+	DocumentName string `yaml:"document_name"`
+}
+
+func main() {
+	var conf *Config
+
+	err := config.ReadFile("config.yaml", conf.ConfigT)
+
+	gen := list.NewListGenerator(conf)
+	gen.Run()
+}

+ 6 - 2
orm/document.go

@@ -165,7 +165,7 @@ func GetDocumentForAdd(args map[string]string) (interface{}, error) {
 
 func GetDocumentExecute(args map[string]string) (interface{}, error) {
 	var document Document
-	if err := DB().Preload("Jobs", "end <> ?", time.Time{}).First(&document, args["id"]).Error; err != nil {
+	if err := DB().First(&document, args["id"]).Error; err != nil {
 		return nil, err
 	}
 
@@ -181,7 +181,11 @@ func GetDocumentExecute(args map[string]string) (interface{}, error) {
 
 	log.Printf("Start concurrent job %d for generator %s", job.ID, document.Name)
 
-	go generator.Generators["list"].Run(job.ID)
+	gen := generator.Generators["list"]
+
+	gen.SetJobId(job.ID)
+
+	go gen.Run()
 
 	return &document, nil
 }