Skip to content

Commit

Permalink
Minor fix to log and plan file location
Browse files Browse the repository at this point in the history
  • Loading branch information
zezha-msft committed Aug 29, 2018
1 parent 6271dde commit e601cd8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 39 deletions.
9 changes: 4 additions & 5 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package cmd

import (
"context"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -30,7 +30,7 @@ import (
"net/url"
"os"
"strings"
"time"
"time"

"github.com/Azure/azure-storage-azcopy/common"
"github.com/Azure/azure-storage-azcopy/ste"
Expand Down Expand Up @@ -680,8 +680,7 @@ func (cca *cookedCopyCmdArgs) processCopyJobPartOrders() (err error) {
func (cca *cookedCopyCmdArgs) waitUntilJobCompletion(blocking bool) {
// print initial message to indicate that the job is starting
glcm.Info("\nJob " + cca.jobID.String() + " has started\n")
currentDir, _ := os.Getwd()
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, currentDir))
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, azcopyAppPathFolder))

// initialize the times necessary to track progress
cca.jobStartTime = time.Now()
Expand Down Expand Up @@ -750,7 +749,7 @@ func (cca *cookedCopyCmdArgs) ReportProgressOrExit(lcm common.LifecycleMgr) {
exitCode = common.EExitCode.Error()
}
lcm.Exit(fmt.Sprintf(
"\n\nJob %s summary\nElapsed Time (Minutes): %v\nTotal Number Of Transfers: %v\nNumber of Transfers Completed: %v\nNumber of Transfers Failed: %v\n Number of Transfers Skipped: %v\n Final Job Status: %v\n TotalBytesTransferred: %v\n",
"\n\nJob %s summary\nElapsed Time (Minutes): %v\nTotal Number Of Transfers: %v\nNumber of Transfers Completed: %v\nNumber of Transfers Failed: %v\nNumber of Transfers Skipped: %v\nFinal Job Status: %v\nTotalBytesTransferred: %v\n",
summary.JobID.String(),
ste.ToFixed(duration.Minutes(), 4),
summary.TotalTransfers,
Expand Down
5 changes: 1 addition & 4 deletions cmd/jobsResume.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"strings"
"time"

"os"

"github.com/Azure/azure-storage-azcopy/common"
"github.com/Azure/azure-storage-azcopy/ste"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,8 +54,7 @@ type resumeJobController struct {
func (cca *resumeJobController) waitUntilJobCompletion(blocking bool) {
// print initial message to indicate that the job is starting
glcm.Info("\nJob " + cca.jobID.String() + " has started\n")
currentDir, _ := os.Getwd()
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, currentDir))
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, azcopyAppPathFolder))
// initialize the times necessary to track progress
cca.jobStartTime = time.Now()
cca.intervalStartTime = time.Now()
Expand Down
2 changes: 1 addition & 1 deletion cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func init() {
Aliases: []string{"mk", "mkdir"},
SuggestFor: []string{"mak", "makeCmd"},
Short: "Create a container/share/filesystem",
Long: `Create a container/share/filesystem represented by the given resource URL.`,
Long: `Create a container/share/filesystem represented by the given resource URL.`,
Example: `
- azcopy make "https://[account-name].[blob,file,dfs].core.windows.net/[top-level-resource-name]"
`,
Expand Down
3 changes: 1 addition & 2 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ type cookedSyncCmdArgs struct {
func (cca *cookedSyncCmdArgs) waitUntilJobCompletion(blocking bool) {
// print initial message to indicate that the job is starting
glcm.Info("\nJob " + cca.jobID.String() + " has started\n")
currentDir, _ := os.Getwd()
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, currentDir))
glcm.Info(fmt.Sprintf("%s.log file created in %s", cca.jobID, azcopyAppPathFolder))

// initialize the times necessary to track progress
cca.jobStartTime = time.Now()
Expand Down
38 changes: 22 additions & 16 deletions common/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os"
"runtime"

"path"

"github.com/Azure/azure-pipeline-go/pipeline"
)

Expand All @@ -47,15 +49,15 @@ type ILoggerResetable interface {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

func NewAppLogger(minimumLevelToLog pipeline.LogLevel) ILoggerCloser {
func NewAppLogger(minimumLevelToLog pipeline.LogLevel, logFileFolder string) ILoggerCloser {
// TODO: Put start date time in file name
// TODO: log life time management.
appLogFile, err := os.OpenFile("azcopy.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) // TODO: Make constant for 0666
PanicIfErr(err)
//appLogFile, err := os.OpenFile(path.Join(logFileFolder, "azcopy.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) // TODO: Make constant for 0666
//PanicIfErr(err)
return &appLogger{
minimumLevelToLog: minimumLevelToLog,
file: appLogFile,
logger: log.New(appLogFile, "", log.LstdFlags|log.LUTC),
//file: appLogFile,
//logger: log.New(appLogFile, "", log.LstdFlags|log.LUTC),
}
}

Expand All @@ -75,19 +77,22 @@ func (al *appLogger) ShouldLog(level pipeline.LogLevel) bool {
}

func (al *appLogger) CloseLog() {
al.logger.Println("Closing Log")
err := al.file.Close()
PanicIfErr(err)
// TODO consider delete completely to get rid of app logger
//al.logger.Println("Closing Log")
//err := al.file.Close()
//PanicIfErr(err)
}

func (al *appLogger) Log(loglevel pipeline.LogLevel, msg string) {
if al.ShouldLog(loglevel) {
al.logger.Println(msg)
}
// TODO consider delete completely to get rid of app logger
//if al.ShouldLog(loglevel) {
// al.logger.Println(msg)
//}
}

func (al *appLogger) Panic(err error) {
al.logger.Panic(err)
// TODO consider delete completely to get rid of app logger
//al.logger.Panic(err)
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -98,11 +103,12 @@ type jobLogger struct {
jobID JobID
minimumLevelToLog pipeline.LogLevel // The maximum customer-desired log level for this job
file *os.File // The job's log file
logFileFolder string // The log file's parent folder, needed for opening the file at the right place
logger *log.Logger // The Job's logger
appLogger ILogger
}

func NewJobLogger(jobID JobID, minimumLevelToLog LogLevel, appLogger ILogger) ILoggerResetable {
func NewJobLogger(jobID JobID, minimumLevelToLog LogLevel, appLogger ILogger, logFileFolder string) ILoggerResetable {
if appLogger == nil {
panic("You must pass a appLogger when creating a JobLogger")
}
Expand All @@ -111,13 +117,13 @@ func NewJobLogger(jobID JobID, minimumLevelToLog LogLevel, appLogger ILogger) IL
jobID: jobID,
appLogger: appLogger, // Panics are recorded in the job log AND in the app log
minimumLevelToLog: minimumLevelToLog.ToPipelineLogLevel(),
//file: jobLogFile,
//logger: log.New(jobLogFile, "", log.LstdFlags|log.LUTC),
logFileFolder: logFileFolder,
}
}

func (jl *jobLogger) OpenLog() {
file, err := os.OpenFile(jl.jobID.String()+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) // TODO: Make constant for 0666
file, err := os.OpenFile(path.Join(jl.logFileFolder, jl.jobID.String()+".log"),
os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) // TODO: Make constant for 0666
PanicIfErr(err)

jl.file = file
Expand Down
2 changes: 1 addition & 1 deletion main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ProcessOSSpecificInitialization() (uint64, error) {
// Azcopy folder in local appdata contains all the files created by azcopy locally.
func GetAzCopyAppPath() string {
localAppData := os.Getenv("HOME")
azcopyAppDataFolder := path.Join(localAppData, "/.azcopy")
azcopyAppDataFolder := path.Join(localAppData, ".azcopy")
if err := os.Mkdir(azcopyAppDataFolder, os.ModeDir|os.ModePerm); err != nil && !os.IsExist(err) {
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package main

import (
"fmt"
"os"
"os/exec"
"path"
"syscall"
)

Expand All @@ -46,8 +46,8 @@ func ProcessOSSpecificInitialization() (int, error) {

// GetAzCopyAppPath returns the path of Azcopy in local appdata.
func GetAzCopyAppPath() string {
localAppData := os.Getenv("LOCALAPPDATA")
azcopyAppDataFolder := fmt.Sprintf("%s%s%s", localAppData, string(os.PathSeparator), "Azcopy")
userProfile := os.Getenv("USERPROFILE")
azcopyAppDataFolder := path.Join(userProfile, ".azcopy")
if err := os.Mkdir(azcopyAppDataFolder, os.ModeDir); err != nil && !os.IsExist(err) {
return ""
}
Expand Down
18 changes: 15 additions & 3 deletions ste/JobsAdmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sync/atomic"
"time"

"path"

"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/Azure/azure-storage-azcopy/common"
)
Expand Down Expand Up @@ -119,10 +121,16 @@ func initJobsAdmin(appCtx context.Context, concurrentConnections int, targetRate
// Create suicide channel which is used to scale back on the number of workers
suicideCh := make(chan SuicideJob, concurrentConnections)

planDir := path.Join(azcopyAppPathFolder, "plans")
if err := os.Mkdir(planDir, os.ModeDir|os.ModePerm); err != nil && !os.IsExist(err) {
common.PanicIfErr(err)
}

ja := &jobsAdmin{
logger: common.NewAppLogger(pipeline.LogInfo),
logger: common.NewAppLogger(pipeline.LogInfo, azcopyAppPathFolder),
jobIDToJobMgr: newJobIDToJobMgr(),
planDir: azcopyAppPathFolder,
logDir: azcopyAppPathFolder,
planDir: planDir,
pacer: newPacer(targetRateInMBps * 1024 * 1024),
appCtx: appCtx,
coordinatorChannels: CoordinatorChannels{
Expand Down Expand Up @@ -236,6 +244,7 @@ type jobsAdmin struct {
logger common.ILoggerCloser
jobIDToJobMgr jobIDToJobMgr // Thread-safe map from each JobID to its JobInfo
// Other global state can be stored in more fields here...
logDir string // Where log files are stored
planDir string // Initialize to directory where Job Part Plans are stored
coordinatorChannels CoordinatorChannels
xferChannels XferChannels
Expand Down Expand Up @@ -296,7 +305,10 @@ func (ja *jobsAdmin) JobMgrEnsureExists(jobID common.JobID,
level common.LogLevel, commandString string) IJobMgr {

return ja.jobIDToJobMgr.EnsureExists(jobID,
func() IJobMgr { return newJobMgr(ja.logger, jobID, ja.appCtx, level, commandString) }) // Return existing or new IJobMgr to caller
func() IJobMgr {
// Return existing or new IJobMgr to caller
return newJobMgr(ja.logger, jobID, ja.appCtx, level, commandString, ja.logDir)
})
}

func (ja *jobsAdmin) ScheduleTransfer(priority common.JobPriority, jptm IJobPartTransferMgr) {
Expand Down
4 changes: 2 additions & 2 deletions ste/mgr-JobMgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ type IJobMgr interface {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

func newJobMgr(appLogger common.ILogger, jobID common.JobID, appCtx context.Context, level common.LogLevel, commandString string) IJobMgr {
func newJobMgr(appLogger common.ILogger, jobID common.JobID, appCtx context.Context, level common.LogLevel, commandString string, logFileFolder string) IJobMgr {
// atomicAllTransfersScheduled is set to 1 since this api is also called when new job part is ordered.
jm := jobMgr{jobID: jobID, jobPartMgrs: newJobPartToJobPartMgr(), include: map[string]int{}, exclude: map[string]int{},
logger: common.NewJobLogger(jobID, level, appLogger) /*Other fields remain zero-value until this job is scheduled */}
logger: common.NewJobLogger(jobID, level, appLogger, logFileFolder) /*Other fields remain zero-value until this job is scheduled */}
jm.reset(appCtx, commandString)
return &jm
}
Expand Down
4 changes: 2 additions & 2 deletions testSuite/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"
)
"github.com/spf13/cobra"
)

var cfgFile string

Expand Down

0 comments on commit e601cd8

Please sign in to comment.