From 16d1069fe9161a1d6caba757ff90411b844136a1 Mon Sep 17 00:00:00 2001 From: NhanPT Date: Wed, 24 Aug 2022 11:00:02 +0700 Subject: [PATCH] fix error job runner --- .gitignore | 3 +- db/db.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ db/log.go | 22 ++++++++++++++ db/syslog.go | 19 ++++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 5 ++- utils/job.go | 43 ++++++++++++++++++++++---- 8 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 db/db.go create mode 100644 db/log.go create mode 100644 db/syslog.go diff --git a/.gitignore b/.gitignore index 1ab9e5c..5bb6b12 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -config.yaml \ No newline at end of file +config.yaml +ocean.db \ No newline at end of file diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..1c57c02 --- /dev/null +++ b/db/db.go @@ -0,0 +1,86 @@ +package db + +import ( + "database/sql" + "log" + "os" +) + +const dbPath = "./ocean.db" +const serviceTag = "database" + +var db *sql.DB = nil + +func Connect() error { + // Create SQLite file + dbExist := true + _, err := os.Stat(dbPath) + if os.IsNotExist(err) { + dbExist = false + file, err := os.Create(dbPath) + if err != nil { + log.Fatalln(err.Error()) + return err + } + file.Close() + } + + // Open the created SQLite File + db, err = sql.Open("sqlite3", dbPath) + if err != nil { + return err + } + + // Defer Closing the database + defer db.Close() + + // Create Database Tables + if !dbExist { + err = createTable() + if err != nil { + return err + } + } + return nil +} + +func checkDb() { + if db == nil { + log.Fatalln("Database is not setup yet!") + } +} + +func createTable() error { + checkDb() + createTableSQL := []string{ + `CREATE TABLE logs ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "job" VARCHAR NOT NULL, + "path" VARCHAR NOT NULL, + "time" DATETIME DEFAULT (datetime('now','localtime')), + "success" BOOLEAN, + "error" TEXT);`, + `CREATE TABLE syslogs ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "tag" VARCHAR NOT NULL, + "time" DATETIME DEFAULT (datetime('now','localtime')), + "error" TEXT);`, + } + // `CREATE INDEX idx_contacts_name ON logs (first_name, last_name);` + + for _, sql := range createTableSQL { + statement, err := db.Prepare(sql) + if err != nil { + log.Fatalln("Prepare table failed: ", err.Error()) + return err + } + + _, err = statement.Exec() + if err != nil { + log.Fatalln("Create table failed: ", err.Error()) + return err + } + } + + return nil +} diff --git a/db/log.go b/db/log.go new file mode 100644 index 0000000..4a114a1 --- /dev/null +++ b/db/log.go @@ -0,0 +1,22 @@ +package db + +func AddLog(data struct { + job string + path string + success bool + errorMessage string +}) error { + checkDb() + insertSQL := `INSERT INTO logs(job, path, success, error) VALUES (?, ?, ?, ?)` + statement, err := db.Prepare(insertSQL) + if err != nil { + AddSystemLog(serviceTag, err) + return err + } + _, err = statement.Exec(data.job, data.path, data.success, data.errorMessage) + if err != nil { + AddSystemLog(serviceTag, err) + return err + } + return nil +} diff --git a/db/syslog.go b/db/syslog.go new file mode 100644 index 0000000..5c425a3 --- /dev/null +++ b/db/syslog.go @@ -0,0 +1,19 @@ +package db + +import "log" + +func AddSystemLog(tag string, err error) error { + checkDb() + insertSQL := `INSERT INTO syslogs(tag, error) VALUES (?, ?)` + statement, err := db.Prepare(insertSQL) + if err != nil { + log.Println(err.Error()) + return err + } + _, err = statement.Exec(tag, err.Error()) + if err != nil { + log.Println(err.Error()) + return err + } + return nil +} diff --git a/go.mod b/go.mod index cb58bcb..4ac5554 100644 --- a/go.mod +++ b/go.mod @@ -28,4 +28,5 @@ require ( github.com/aws/aws-sdk-go-v2/service/sts v1.16.13 // indirect github.com/aws/smithy-go v1.12.1 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/mattn/go-sqlite3 v1.14.15 // indirect ) diff --git a/go.sum b/go.sum index 76802db..2cd8378 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= diff --git a/main.go b/main.go index 37e56ef..1b6bf7e 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,10 @@ func main() { } cron := cron.New() cron.AddFunc(cronTime, func() { - runner.Run() + err = runner.Run() + if err != nil { + log.Println(err.Error()) + } }) cron.Start() runner.Cron = cron diff --git a/utils/job.go b/utils/job.go index 865c38c..bdb4de0 100644 --- a/utils/job.go +++ b/utils/job.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "log" "os" "path/filepath" "strings" @@ -18,7 +19,7 @@ type JobRunner struct { Service services.Service } -func (runner JobRunner) Run() error { +func (runner *JobRunner) Run() error { uploadFile := "" uploadPath := runner.Job.Path @@ -26,13 +27,16 @@ func (runner JobRunner) Run() error { if os.IsNotExist(err) { return err } + if !stats.IsDir() { + uploadFile = stats.Name() + } - if runner.Job.UseZip || stats.IsDir() { - pwd, _ := os.Getwd() + if (runner.Job.UseZip && !strings.Contains(uploadPath, ".zip")) || stats.IsDir() { uploadFile = createZipName(runner.Job.Name) - uploadPath = filepath.Join(pwd, "temp", uploadFile) + uploadPath = createTempPath(uploadFile) err = ZipFolder(runner.Job.Path, uploadFile) if err != nil { + log.Println(err.Error()) return err } } @@ -41,7 +45,17 @@ func (runner JobRunner) Run() error { return errors.New("No service found with this name: " + runner.Job.UploadTo) } remotePath := createRemotePath(uploadFile) - return runner.Service.Upload(uploadPath, remotePath) + err = runner.Service.Upload(uploadPath, remotePath) + if err != nil { + log.Println(err.Error()) + return err + } + log.Println("File " + uploadFile + "uploaded to " + runner.Service.Name()) + + if strings.Contains(uploadPath, tempFolder()) { + return os.Remove(uploadPath) + } + return nil } func createZipName(name string) string { @@ -54,6 +68,23 @@ func createZipName(name string) string { return result } +func tempFolder() string { + pwd, _ := os.Getwd() + tempPath := filepath.Join(pwd, "temp") + + if _, err := os.Stat(tempPath); errors.Is(err, os.ErrNotExist) { + err := os.Mkdir(tempPath, os.ModePerm) + if err != nil { + log.Println(err) + } + } + return tempPath +} + +func createTempPath(fileName string) string { + return filepath.Join(tempFolder(), fileName) +} + func createRemotePath(fileName string) string { - return filepath.Join("/", time.Now().Format("2006-02-13"), fileName) + return filepath.Join("/", time.Now().Format("2006-01-02"), fileName) }