Merge pull request 'stale process detection on android' (#63) from rrgtj into trunk
continuous-integration/drone/push Build is passing Details

Reviewed-on: #63
This commit is contained in:
Sarah Jamie Lewis 2021-06-21 17:51:49 -07:00
commit c864bccbb9
2 changed files with 256 additions and 225 deletions

36
lib.go
View File

@ -68,9 +68,10 @@ func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) int8 {
// message: CwtchStarted when start up is complete and app is safe to use
// CwtchStartError message when start up fails (includes event.Error data field)
func StartCwtch(appDir string, torPath string) int {
eventHandler = utils.NewEventHandler()
log.SetLevel(log.LevelInfo)
log.Infof("StartCwtch(...)")
// Quick hack check that we're being called with the correct params
// On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker
if runtime.GOOS == "android" {
@ -87,7 +88,16 @@ func StartCwtch(appDir string, torPath string) int {
}
func _startCwtch(appDir string, torPath string) {
// Exclude Tapir wire Messages (We need a TRACE level)
log.Infof("application: %v eventHandler: %v acn: %v", application, eventHandler, globalACN)
if application != nil {
log.Infof("_startCwtch detected existing application; resuming instead of relaunching")
ReconnectCwtchForeground()
return
}
// Exclude Tapir wire Messages
//(We need a TRACE level)
log.ExcludeFromPattern("service.go")
// Ensure that the application directory exists...and then initialize settings..
@ -107,6 +117,9 @@ func _startCwtch(appDir string, torPath string) {
panic(err)
}
log.Infof("Creating new EventHandler()")
eventHandler = utils.NewEventHandler()
log.Infof("making directory %v", appDir)
os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700)
tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc"))
@ -162,6 +175,7 @@ func c_ReconnectCwtchForeground() {
// Like StartCwtch, but StartCwtch has already been called so we don't need to restart Tor etc (probably)
// Do need to re-send initial state tho, eg profiles that are already loaded
func ReconnectCwtchForeground() {
log.Infof("Reconnecting cwtchforeground")
if application == nil {
log.Errorf("ReconnectCwtchForeground: Application is nil, presuming stale thread, EXITING Reconnect\n")
return
@ -333,10 +347,18 @@ func c_GetAppBusEvent() *C.char {
// GetAppBusEvent blocks until an event
func GetAppBusEvent() string {
log.Infof("appbusevent called")
for eventHandler == nil {
log.Infof("waiting for eventHandler != nil")
time.Sleep(time.Second)
}
var json = ""
for json == "" {
log.Infof("waiting for json != ''")
json = eventHandler.GetNextEvent()
}
log.Infof("appbusevent: %v", json)
return json
}
@ -700,11 +722,15 @@ func ShutdownCwtch() {
eventHandler.Push(event.NewEvent(event.Shutdown, map[event.Field]string{}))
// Allow for the shutdown events to go through and then purge everything else...
log.Debugf("Shutting Down Application...")
log.Infof("Shutting Down Application...")
application.Shutdown()
log.Debugf("Shutting Down ACN...")
log.Infof("Shutting Down ACN...")
globalACN.Close()
log.Debugf("Library Shutdown Complete!")
log.Infof("Library Shutdown Complete!")
// do not remove - important for state checks elsewhere
application = nil
globalACN = nil
eventHandler = nil
}
}

View File

@ -65,6 +65,7 @@ func (eh *EventHandler) GetNextEvent() string {
// handleAppBusEvent enriches AppBus events so they are usable with out further data fetches
func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
log.Debugf("New AppBus Event to Handle: %v", e)
if eh.app != nil {
switch e.EventType {
case event.ACNStatus:
if e.Data[event.Progress] == "100" {
@ -209,6 +210,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
log.Infof("contactsJson %v", e.Data["ContactsJson"])
}
}
json, _ := json.Marshal(e)
return string(json)
@ -216,7 +218,9 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
// handleProfileEvent enriches Profile events so they are usable with out further data fetches
func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
if eh.app == nil {
log.Errorf("eh.app == nil in handleProfileEvent... this shouldnt happen?")
} else {
peer := eh.app.GetPeer(ev.Profile)
ph := NewPeerHelper(peer)
log.Debugf("New Profile Event to Handle: %v", ev)
@ -304,6 +308,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
}
}
}
}
json, _ := json.Marshal(unwrap(ev))
return string(json)