Skip to content

Commit

Permalink
fix error job runner
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanPT committed Aug 24, 2022
1 parent e9776ee commit 16d1069
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/

config.yaml
config.yaml
ocean.db
86 changes: 86 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions db/log.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions db/syslog.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 37 additions & 6 deletions utils/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"errors"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -18,21 +19,24 @@ type JobRunner struct {
Service services.Service
}

func (runner JobRunner) Run() error {
func (runner *JobRunner) Run() error {
uploadFile := ""
uploadPath := runner.Job.Path

stats, err := os.Stat(uploadPath)
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
}
}
Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit 16d1069

Please sign in to comment.