mirror of
https://github.com/postmannen/ctrl.git
synced 2024-12-14 12:37:31 +00:00
restructured req to file methods to use the same file write function
This commit is contained in:
parent
e50d906be3
commit
16ec026453
3 changed files with 129 additions and 69 deletions
41
methodREQToFile.go
Normal file
41
methodREQToFile.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
func (m methodREQToFile) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
|
||||
// If it was a request type message we want to check what the initial messages
|
||||
// method, so we can use that in creating the file name to store the data.
|
||||
fileName, folderTree := selectFileNaming(message, proc)
|
||||
|
||||
// Check if folder structure exist, if not create it.
|
||||
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderTree, 0770)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile failed to create toFile directory tree: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return nil, er
|
||||
}
|
||||
|
||||
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
||||
proc.errorKernel.logDebug(er, proc.configuration)
|
||||
}
|
||||
|
||||
// Open file and write data.
|
||||
file := filepath.Join(folderTree, fileName)
|
||||
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile.handler: failed to open file, check that you've specified a value for fileName in the message: directory: %v, fileName: %v, %v", message.Directory, message.FileName, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(message.Data)
|
||||
f.Sync()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file: file: %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
|
||||
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
||||
return ackMsg, nil
|
||||
}
|
38
methodREQToFileAppend.go
Normal file
38
methodREQToFileAppend.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
func (m methodREQToFileAppend) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
|
||||
// If it was a request type message we want to check what the initial messages
|
||||
// method, so we can use that in creating the file name to store the data.
|
||||
fileName, folderTree := selectFileNaming(message, proc)
|
||||
|
||||
// Check if folder structure exist, if not create it.
|
||||
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderTree, 0770)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFileAppend: failed to create toFileAppend directory tree:%v, subject: %v, %v", folderTree, proc.subject, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
|
||||
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
||||
proc.errorKernel.logDebug(er, proc.configuration)
|
||||
}
|
||||
|
||||
// Open file and write data.
|
||||
file := filepath.Join(folderTree, fileName)
|
||||
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFileAppend.handler: failed to open file: %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(message.Data)
|
||||
f.Sync()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file : %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
|
||||
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
||||
return ackMsg, nil
|
||||
}
|
|
@ -9,6 +9,54 @@ import (
|
|||
"github.com/hpcloud/tail"
|
||||
)
|
||||
|
||||
func reqWriteFile(isAppend bool, proc process, message Message) error {
|
||||
// If it was a request type message we want to check what the initial messages
|
||||
// method, so we can use that in creating the file name to store the data.
|
||||
fileName, folderTree := selectFileNaming(message, proc)
|
||||
|
||||
// Check if folder structure exist, if not create it.
|
||||
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderTree, 0770)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile/Append failed to create toFile directory tree: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return er
|
||||
}
|
||||
|
||||
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
||||
proc.errorKernel.logDebug(er, proc.configuration)
|
||||
}
|
||||
|
||||
var fileFlag int
|
||||
switch isAppend {
|
||||
case true:
|
||||
fileFlag = os.O_APPEND | os.O_RDWR | os.O_CREATE | os.O_SYNC
|
||||
case false:
|
||||
fileFlag = os.O_CREATE | os.O_RDWR | os.O_TRUNC
|
||||
}
|
||||
|
||||
// Open file and write data.
|
||||
file := filepath.Join(folderTree, fileName)
|
||||
f, err := os.OpenFile(file, fileFlag, 0755)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile/Append: failed to open file, check that you've specified a value for fileName in the message: directory: %v, fileName: %v, %v", message.Directory, message.FileName, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(message.Data)
|
||||
f.Sync()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile/Append: failed to write to file: file: %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type methodREQToFileAppend struct {
|
||||
event Event
|
||||
}
|
||||
|
@ -19,39 +67,7 @@ func (m methodREQToFileAppend) getKind() Event {
|
|||
|
||||
// Handle appending data to file.
|
||||
func (m methodREQToFileAppend) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
|
||||
// If it was a request type message we want to check what the initial messages
|
||||
// method, so we can use that in creating the file name to store the data.
|
||||
fileName, folderTree := selectFileNaming(message, proc)
|
||||
|
||||
// Check if folder structure exist, if not create it.
|
||||
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderTree, 0770)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFileAppend: failed to create toFileAppend directory tree:%v, subject: %v, %v", folderTree, proc.subject, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
|
||||
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
||||
proc.errorKernel.logDebug(er, proc.configuration)
|
||||
}
|
||||
|
||||
// Open file and write data.
|
||||
file := filepath.Join(folderTree, fileName)
|
||||
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFileAppend.handler: failed to open file: %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(message.Data)
|
||||
f.Sync()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file : %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
reqWriteFile(true, proc, message)
|
||||
|
||||
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
||||
return ackMsg, nil
|
||||
|
@ -70,42 +86,7 @@ func (m methodREQToFile) getKind() Event {
|
|||
// Handle writing to a file. Will truncate any existing data if the file did already
|
||||
// exist.
|
||||
func (m methodREQToFile) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
|
||||
// If it was a request type message we want to check what the initial messages
|
||||
// method, so we can use that in creating the file name to store the data.
|
||||
fileName, folderTree := selectFileNaming(message, proc)
|
||||
|
||||
// Check if folder structure exist, if not create it.
|
||||
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderTree, 0770)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile failed to create toFile directory tree: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return nil, er
|
||||
}
|
||||
|
||||
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
||||
proc.errorKernel.logDebug(er, proc.configuration)
|
||||
}
|
||||
|
||||
// Open file and write data.
|
||||
file := filepath.Join(folderTree, fileName)
|
||||
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodREQToFile.handler: failed to open file, check that you've specified a value for fileName in the message: directory: %v, fileName: %v, %v", message.Directory, message.FileName, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(message.Data)
|
||||
f.Sync()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file: file: %v, %v", file, err)
|
||||
proc.errorKernel.errSend(proc, message, er, logWarning)
|
||||
}
|
||||
reqWriteFile(false, proc, message)
|
||||
|
||||
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
||||
return ackMsg, nil
|
||||
|
|
Loading…
Reference in a new issue