Add restart mechanism on error for bridge.
This commit is contained in:
parent
41da29f669
commit
ca0fd6a529
@ -16,6 +16,7 @@ import com.programaker.api.ProgramakerApi;
|
|||||||
public class ProgramakerBridgeService extends Service {
|
public class ProgramakerBridgeService extends Service {
|
||||||
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
||||||
public static final CharSequence BridgeUserNotificationChannelName = "User notifications";
|
public static final CharSequence BridgeUserNotificationChannelName = "User notifications";
|
||||||
|
private static final long WAIT_TIME_BEFORE_RESTART_MILLIS = 10000; // 10s
|
||||||
private ProgramakerAndroidBridge bridge = null;
|
private ProgramakerAndroidBridge bridge = null;
|
||||||
private static final String LogTag = "PM BridgeService";
|
private static final String LogTag = "PM BridgeService";
|
||||||
|
|
||||||
@ -25,7 +26,14 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
Toast.makeText(this, "Starting bridge", Toast.LENGTH_SHORT).show();
|
||||||
|
connectBridge();
|
||||||
|
|
||||||
|
// If we get killed, after returning from here, restart
|
||||||
|
return START_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connectBridge() {
|
||||||
ConfigManager config = new ConfigManager(this);
|
ConfigManager config = new ConfigManager(this);
|
||||||
|
|
||||||
String token = config.getToken();
|
String token = config.getToken();
|
||||||
@ -75,6 +83,7 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
},
|
},
|
||||||
ex -> {
|
ex -> {
|
||||||
Log.e(LogTag, "Error establishing bridge connection: " + ex, ex);
|
Log.e(LogTag, "Error establishing bridge connection: " + ex, ex);
|
||||||
|
ProgramakerBridgeService.this.stopSelf();
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -82,8 +91,7 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
},
|
},
|
||||||
() -> { // On completed
|
() -> { // On completed
|
||||||
ProgramakerBridgeService.this.bridge = null;
|
ProgramakerBridgeService.this.bridge = null;
|
||||||
Log.e(LogTag, "Bridge stopped, stopping service");
|
onBridgeFailedAfterConnected();
|
||||||
ProgramakerBridgeService.this.stopSelf();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
@ -93,10 +101,16 @@ public class ProgramakerBridgeService extends Service {
|
|||||||
}, "ServiceStartArguments");
|
}, "ServiceStartArguments");
|
||||||
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
thread.start();
|
thread.start();
|
||||||
Toast.makeText(this, "Starting bridge", Toast.LENGTH_SHORT).show();
|
}
|
||||||
|
|
||||||
// If we get killed, after returning from here, restart
|
private void onBridgeFailedAfterConnected() {
|
||||||
return START_STICKY;
|
Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting");
|
||||||
|
try {
|
||||||
|
Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
connectBridge();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user