-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move towards non global logger p1 #249
Open
cviecco
wants to merge
2
commits into
Cloud-Foundations:master
Choose a base branch
from
cviecco:move-towards-non-global-logger-p1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
|
||
"github.com/Cloud-Foundations/golib/pkg/awsutil/metadata" | ||
"github.com/Cloud-Foundations/golib/pkg/awsutil/secretsmgr" | ||
"github.com/Cloud-Foundations/golib/pkg/log" | ||
_ "github.com/lib/pq" | ||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
@@ -82,38 +83,39 @@ func (state *RuntimeState) expandStorageUrl() error { | |
} | ||
|
||
func initDB(state *RuntimeState) (err error) { | ||
logger.Debugf(3, "Top of initDB") | ||
state.logger.Debugf(3, "Top of initDB") | ||
state.Config.ProfileStorage.setSyncLimits() | ||
//open/create cache DB first | ||
cacheDBFilename := filepath.Join(state.Config.Base.DataDirectory, | ||
cachedDBFilename) | ||
state.cacheDB, err = initFileDBSQLite(cacheDBFilename, state.cacheDB) | ||
if err != nil { | ||
logger.Printf("Failure on creation of cacheDB") | ||
state.logger.Printf("Failure on creation of cacheDB") | ||
return err | ||
} | ||
logger.Debugf(3, "storage=%s", state.Config.ProfileStorage.StorageUrl) | ||
state.logger.Debugf(3, "storage=%s", state.Config.ProfileStorage.StorageUrl) | ||
storageURL := state.Config.ProfileStorage.StorageUrl | ||
if storageURL == "" { | ||
storageURL = "sqlite:" | ||
} | ||
splitString := strings.SplitN(storageURL, ":", 2) | ||
if len(splitString) < 1 { | ||
logger.Printf("invalid string") | ||
state.logger.Printf("invalid string") | ||
err := errors.New("Bad storage url string") | ||
return err | ||
} | ||
state.remoteDBQueryTimeout = time.Second * 2 | ||
go state.BackgroundDBCopy(state.Config.ProfileStorage.SyncDelay) | ||
state.dbDone = make(chan struct{}) | ||
go state.BackgroundDBCopy(state.Config.ProfileStorage.SyncDelay, state.dbDone, state.logger) | ||
switch splitString[0] { | ||
case "sqlite": | ||
logger.Printf("doing sqlite") | ||
state.logger.Printf("doing sqlite") | ||
return initDBSQlite(state) | ||
case "postgresql": | ||
logger.Printf("doing postgres") | ||
state.logger.Printf("doing postgres") | ||
return initDBPostgres(state) | ||
default: | ||
logger.Printf("invalid storage url string") | ||
state.logger.Printf("invalid storage url string") | ||
err := errors.New("Bad storage url string") | ||
return err | ||
} | ||
|
@@ -130,13 +132,13 @@ func initDBPostgres(state *RuntimeState) (err error) { | |
sqlStmt := `create table if not exists user_profile (id serial not null primary key, username text unique, profile_data bytea);` | ||
_, err = state.db.Exec(sqlStmt) | ||
if err != nil { | ||
logger.Printf("init postgres err: %s: %q\n", err, sqlStmt) | ||
state.logger.Printf("init postgres err: %s: %q\n", err, sqlStmt) | ||
return err | ||
} | ||
sqlStmt = `create table if not exists expiring_signed_user_data(id serial not null primary key, username text not null, jws_data text not null, type integer not null, expiration_epoch integer not null, update_epoch integer not null, UNIQUE(username,type));` | ||
_, err = state.db.Exec(sqlStmt) | ||
if err != nil { | ||
logger.Printf("init postgres err: %s: %q\n", err, sqlStmt) | ||
state.logger.Printf("init postgres err: %s: %q\n", err, sqlStmt) | ||
return err | ||
} | ||
} | ||
|
@@ -207,8 +209,16 @@ func initFileDBSQLite(dbFilename string, currentDB *sql.DB) (*sql.DB, error) { | |
return currentDB, nil | ||
} | ||
|
||
func (state *RuntimeState) BackgroundDBCopy(initialSleep time.Duration) { | ||
time.Sleep(initialSleep) | ||
func (state *RuntimeState) BackgroundDBCopy(initialSleep time.Duration, done chan struct{}, logger log.DebugLogger) { | ||
select { | ||
case <-done: | ||
//fmt.Println("Received:", v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete commented code. |
||
logger.Debugf(0, "Cancelled before first copy") | ||
return | ||
case <-time.After(initialSleep): | ||
logger.Debugf(1, "BackgroundDBCopy, initial sleep done") | ||
|
||
} | ||
for { | ||
logger.Debugf(0, "starting db copy") | ||
err := copyDBIntoSQLite(state.db, state.cacheDB, "sqlite") | ||
|
@@ -219,7 +229,17 @@ func (state *RuntimeState) BackgroundDBCopy(initialSleep time.Duration) { | |
} | ||
cleanupDBData(state.db) | ||
cleanupDBData(state.cacheDB) | ||
time.Sleep(state.Config.ProfileStorage.SyncInterval) | ||
|
||
select { | ||
case <-done: | ||
//fmt.Println("Received:", v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete commented code. |
||
logger.Debugf(0, "Cancelled after copy") | ||
return | ||
case <-time.After(state.Config.ProfileStorage.SyncInterval): | ||
logger.Debugf(1, "BackgroundDBCopy, sleep complete sleep done") | ||
|
||
} | ||
//time.Sleep(state.Config.ProfileStorage.SyncInterval) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider changing the type to
chan <- struct{}
so that you can enforce only one place is able to read from the chanel.