Commit 5b1b6c78 authored by Pasan Mallawaarachchi's avatar Pasan Mallawaarachchi 💻

dev

parent 0d911a0c
File added
...@@ -120,148 +120,155 @@ func main() { ...@@ -120,148 +120,155 @@ func main() {
panic(configErr) panic(configErr)
} }
if config.AppMod.Production == "0" { _, reqErrOutbox := http.NewRequest(config.Requests.Get, config.Curl.CheckMail, nil)
fmt.Println("Starting the Email Manager...") if reqErrOutbox != nil {
} fmt.Println("Error Connectiong to CouchDB")
time.Sleep(time.Millisecond * 300) os.Exit(1)
mailData = make(chan string) } else {
outboxReadTime = make(chan string)
outboxDeleteTime = make(chan string) if config.AppMod.Production == "0" {
fmt.Println("Starting the Email Manager...")
for i := 1; i < 4; i++ { }
waitGroup.Add(1) time.Sleep(time.Millisecond * 300)
go worker(i) mailData = make(chan string)
} outboxReadTime = make(chan string)
outboxDeleteTime = make(chan string)
for 1 == 1 {
if childs < 4 { for i := 1; i < 4; i++ {
childs++ waitGroup.Add(1)
//time.Sleep(time.Second * 1) go worker(i)
}
/**
* make cURL request to read "OUTBOX" DB for documents
* the following cURL request returns the oldest document of the DB
*
*/
req, reqErr := http.NewRequest(config.Requests.Get, config.Curl.CheckMail, nil)
//req, reqErr := http.NewRequest("GET", "http://root:root@0.0.0.0:5984/outbox/_all_docs/?limit=1", nil)
if reqErr != nil {
alert := " Error creating request to OUTBOX DB \n" + reqErr.Error()
alertAdmin(alert)
panic(reqErr)
}
req.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
alert := " Error in getting email from the OUTBOX DB \n" + respErr.Error()
alertAdmin(alert)
panic(respErr)
}
defer resp.Body.Close()
respbody, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(respbody))
/**
* struct the response of the of the cURL request
* after "Unmarshal", the "newDoc" holds the data of the cURL request
*/
var newDoc Couchdb
err1 := json.Unmarshal(respbody, &newDoc)
if err1 != nil {
alert := " Error converting the OUTBOX response to JSON String \n" + err1.Error()
alertAdmin(alert)
}
/**
* if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0
* and we send newEmail Document as JSONstring through the channel
*
* else: send "ko"
*/
if newDoc.TotalRows > 0 {
/** for 1 == 1 {
* get Document ID and Revision ID if childs < 4 {
* @type {[type]} childs++
*/ //time.Sleep(time.Second * 1)
newMailID := newDoc.Rows[0].ID
newMailRev := newDoc.Rows[0].Value.Rev
/** /**
* Now we get the documents' email data from the "OUTBOX" DB * make cURL request to read "OUTBOX" DB for documents
* cURL reuest to get the document * the following cURL request returns the oldest document of the DB
*
*/ */
req, reqErr := http.NewRequest(config.Requests.Get, config.Curl.GetOutboxMail+newMailID, nil) req, reqErr := http.NewRequest(config.Requests.Get, config.Curl.CheckMail, nil)
req.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson) //req, reqErr := http.NewRequest("GET", "http://root:root@0.0.0.0:5984/outbox/_all_docs/?limit=1", nil)
resp, respErr := http.DefaultClient.Do(req)
if reqErr != nil { if reqErr != nil {
alert := " Error requesting the OUTBOX mail data \n" + reqErr.Error() alert := " Error creating request to OUTBOX DB \n" + reqErr.Error()
alertAdmin(alert) alertAdmin(alert)
panic(reqErr)
} }
req.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil { if respErr != nil {
alert := " Error getting the OUTBOX mail data \n" + respErr.Error() alert := " Error in getting email from the OUTBOX DB \n" + respErr.Error()
alertAdmin(alert) alertAdmin(alert)
panic(respErr)
} }
defer resp.Body.Close() defer resp.Body.Close()
respbody, _ := ioutil.ReadAll(resp.Body) respbody, _ := ioutil.ReadAll(resp.Body)
newMail := string(respbody) //fmt.Println(string(respbody))
readTimeOutbox := time.Now().String()
// mailReadTime := mailReadTime.String()
/** /**
* structuring the email document * struct the response of the of the cURL request
* * after "Unmarshal", the "newDoc" holds the data of the cURL request
*/ */
var mail MailBody var newDoc Couchdb
mailErr := json.Unmarshal([]byte(newMail), &mail) err1 := json.Unmarshal(respbody, &newDoc)
if mailErr != nil { if err1 != nil {
alert := " Error converting OUTBOX DB mail data into JSON string \n" + mailErr.Error() alert := " Error converting the OUTBOX response to JSON String \n" + err1.Error()
alertAdmin(alert) alertAdmin(alert)
} }
/** /**
* Send email document as JSONstring * if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0
* and we send newEmail Document as JSONstring through the channel
*
* else: send "ko"
*/ */
mailData <- (newMail) if newDoc.TotalRows > 0 {
outboxReadTime <- (readTimeOutbox)
// data <- ("Worker " + strconv.Itoa(childs)) /**
* get Document ID and Revision ID
* @type {[type]}
*/
newMailID := newDoc.Rows[0].ID
newMailRev := newDoc.Rows[0].Value.Rev
/**
* Now we get the documents' email data from the "OUTBOX" DB
* cURL reuest to get the document
*/
req, reqErr := http.NewRequest(config.Requests.Get, config.Curl.GetOutboxMail+newMailID, nil)
req.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
resp, respErr := http.DefaultClient.Do(req)
if reqErr != nil {
alert := " Error requesting the OUTBOX mail data \n" + reqErr.Error()
alertAdmin(alert)
}
if respErr != nil {
alert := " Error getting the OUTBOX mail data \n" + respErr.Error()
alertAdmin(alert)
}
defer resp.Body.Close()
respbody, _ := ioutil.ReadAll(resp.Body)
newMail := string(respbody)
readTimeOutbox := time.Now().String()
// mailReadTime := mailReadTime.String()
/**
* structuring the email document
*
*/
var mail MailBody
mailErr := json.Unmarshal([]byte(newMail), &mail)
if mailErr != nil {
alert := " Error converting OUTBOX DB mail data into JSON string \n" + mailErr.Error()
alertAdmin(alert)
}
/**
* Send email document as JSONstring
*/
mailData <- (newMail)
outboxReadTime <- (readTimeOutbox)
// data <- ("Worker " + strconv.Itoa(childs))
/**
* Delete the document from the "OUTBOX"
*/
delReq, delReqerr := http.NewRequest(config.Requests.Delete, config.Curl.DeleteOutboxMail+newMailID+"?rev="+newMailRev, nil)
if delReqerr != nil {
alert := " Error requesting to delete mail from OUTBOX \n" + delReqerr.Error()
alertAdmin(alert)
}
delReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
delResp, delResperr := http.DefaultClient.Do(delReq)
if delResperr != nil {
alert := " Error deleting mail from OUTBOX DB \n" + delResperr.Error()
alertAdmin(alert)
}
defer delResp.Body.Close()
ioutil.ReadAll(delResp.Body)
deleteTimeOutbox := time.Now().String()
outboxDeleteTime <- (deleteTimeOutbox)
} else {
mailData <- "ko"
outboxReadTime <- ""
outboxDeleteTime <- ""
/**
* Delete the document from the "OUTBOX"
*/
delReq, delReqerr := http.NewRequest(config.Requests.Delete, config.Curl.DeleteOutboxMail+newMailID+"?rev="+newMailRev, nil)
if delReqerr != nil {
alert := " Error requesting to delete mail from OUTBOX \n" + delReqerr.Error()
alertAdmin(alert)
}
delReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
delResp, delResperr := http.DefaultClient.Do(delReq)
if delResperr != nil {
alert := " Error deleting mail from OUTBOX DB \n" + delResperr.Error()
alertAdmin(alert)
} }
defer delResp.Body.Close()
ioutil.ReadAll(delResp.Body)
deleteTimeOutbox := time.Now().String()
outboxDeleteTime <- (deleteTimeOutbox)
} else {
mailData <- "ko"
outboxReadTime <- ""
outboxDeleteTime <- ""
} }
} }
}
close(mailData) close(mailData)
close(outboxReadTime) close(outboxReadTime)
close(outboxDeleteTime) close(outboxDeleteTime)
waitGroup.Wait() waitGroup.Wait()
}
} }
func worker(i int) { func worker(i int) {
...@@ -465,6 +472,9 @@ func getFile(fileName, url string) { ...@@ -465,6 +472,9 @@ func getFile(fileName, url string) {
// Write the body to file // Write the body to file
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment