Commit 0953dc47 authored by Pasan Mallawaarachchi's avatar Pasan Mallawaarachchi 💻

outbox read log

parent 86ca1c10
......@@ -9,7 +9,7 @@ curl:
delete_outbox_mail: http://root:root@couchdb_emanger:5984/outbox/
add_sent_mail: http://root:root@couchdb_emanger:5984/sentitems/
add_mail_log: http://root:root@couchdb_emanger:5984/mail_logs/
read_log: http://root:root@couchdb_emanger:5984/read_logs/
read_logs: http://root:root@couchdb_emanger:5984/read_logs
protocols:
method: http://
requests:
......
......@@ -41,7 +41,7 @@ type Config struct {
DeleteOutboxMail string `yaml:"delete_outbox_mail"`
AddSentMail string `yaml:"add_sent_mail"`
AddMailLog string `yaml:"add_mail_log"`
ReadLog string `yaml:"read_log"`
ReadLog string `yaml:"read_logs"`
} `yaml:"curl"`
Protocols struct {
Request string `yaml:"method"`
......@@ -107,6 +107,7 @@ var mailData chan string
var outboxReadTime chan string
var outboxDeleteTime chan string
var filename = ""
var loc, _ = time.LoadLocation("Asia/Colombo")
func main() {
// get configurations
......@@ -182,9 +183,10 @@ func main() {
alertAdmin(alert)
}
fmt.Println(string(respbody))
fmt.Printf(" %s, %d | ", resp.Status, newDoc.TotalRows)
// fmt.Println(string(respbody))
fmt.Printf(" %s, %s | ", resp.Status, strconv.Itoa(newDoc.TotalRows))
logOutbox(resp.Status, strconv.Itoa(newDoc.TotalRows), time.Now().In(loc).String() /*string(respbody)*/)
//fmt.Printf("%s, %s, %s", resp.Status, strconv.Itoa(newDoc.TotalRows), string(respbody))
/**
* if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0
......@@ -205,9 +207,9 @@ func main() {
* 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)
reqNew, reqErr := http.NewRequest(config.Requests.Get, config.Curl.GetOutboxMail+newMailID, nil)
reqNew.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
respNew, respErr := http.DefaultClient.Do(reqNew)
if reqErr != nil {
alert := " Error requesting the OUTBOX mail data \n" + reqErr.Error()
alertAdmin(alert)
......@@ -216,10 +218,10 @@ func main() {
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()
defer respNew.Body.Close()
respbodyNew, _ := ioutil.ReadAll(respNew.Body)
newMail := string(respbodyNew)
readTimeOutbox := time.Now().In(loc).String()
// mailReadTime := mailReadTime.String()
/**
......@@ -256,7 +258,7 @@ func main() {
}
defer delResp.Body.Close()
ioutil.ReadAll(delResp.Body)
deleteTimeOutbox := time.Now().String()
deleteTimeOutbox := time.Now().In(loc).String()
outboxDeleteTime <- (deleteTimeOutbox)
} else {
......@@ -266,7 +268,6 @@ func main() {
outboxDeleteTime <- ""
}
}
}
......@@ -414,7 +415,7 @@ func worker(i int) {
fmt.Printf("Email sent ID: %s Resp: %s\n", id, MailgunResp)
}
//sendMessage(mg, sender, subject, body, recipient, cc)
mailSentTimeMailgun := time.Now().String()
mailSentTimeMailgun := time.Now().In(loc).String()
/**
* add the email as new document in "SENT ITEMS" DB
*/
......@@ -434,7 +435,7 @@ func worker(i int) {
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
*/
mailAddedSentitems := time.Now().String()
mailAddedSentitems := time.Now().In(loc).String()
sentmail(subject, sender, recipient, cc, bcc, attachment, body, mail.ID)
logmail(subject, sender, recipient, cc, bcc, attachment, readTimeOutbox, mailSentTimeMailgun, id, MailgunResp, mailAddedSentitems, deleteTimeOutbox, mail.ID)
......@@ -590,34 +591,36 @@ func sentmail(subject, sender, recipient, cc, bcc, attachment, body, mailID stri
ioutil.ReadAll(logResp.Body)
}
func logOutbox(status, error, id string) {
func logOutbox(status, totalRows, time /*respBody*/ string) {
// get configurations
var config Config
source, sourceErr := ioutil.ReadFile("config.yaml")
if sourceErr != nil {
fmt.Printf("Logmail sourceErr : %s", sourceErr.Error())
fmt.Printf("Sentmail sourceErr : %s", sourceErr.Error())
// panic(sourceErr)
}
configErr := yaml.Unmarshal(source, &config)
if configErr != nil {
fmt.Printf("Logmail configErr : %s", configErr.Error())
fmt.Printf("Sentmail configErr : %s", configErr.Error())
// panic(configErr)
}
sentBody := strings.NewReader(`{"status":"` + status +
`","error":"` + error +
`","id":"` + id + `"}`)
logReq, logBodyerr := http.NewRequest(config.Requests.Put, config.Curl.ReadLog+id, sentBody)
`","row_count":"` + totalRows +
`","date_time":"` + time + `"}`)
logReq, logBodyerr := http.NewRequest(config.Requests.Post, config.Curl.ReadLog, sentBody)
if logBodyerr != nil {
alert := " Error requesting log mail \n" + logBodyerr.Error()
alert := " Error requesting sent mail \n" + logBodyerr.Error()
alertAdmin(alert)
}
logReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
logResp, respErr := http.DefaultClient.Do(logReq)
if respErr != nil {
alert := " Error adding data to LOGS DB \n" + respErr.Error()
alert := " Error adding data to SENT ITEMS DB \n" + respErr.Error()
alertAdmin(alert)
}
// aa, _ := ioutil.ReadAll(logResp.Body)
// fmt.Println(aa)
defer logResp.Body.Close()
ioutil.ReadAll(logResp.Body)
}
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