mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-06-16 02:44:33 +08:00
[Feature] Re-try mechanism for CodePush Rollbacks (#1467)
This commit is contained in:
committed by
Alexander Goncharov
parent
ac5472ee2a
commit
693b769ba6
@@ -27,4 +27,8 @@ public class CodePushConstants {
|
||||
public static final String UNZIPPED_FOLDER_NAME = "unzipped";
|
||||
public static final String CODE_PUSH_APK_BUILD_TIME_KEY = "CODE_PUSH_APK_BUILD_TIME";
|
||||
public static final String BUNDLE_JWT_FILE = ".codepushrelease";
|
||||
public static final String LATEST_ROLLBACK_INFO_KEY = "LATEST_ROLLBACK_INFO";
|
||||
public static final String LATEST_ROLLBACK_PACKAGE_HASH_KEY = "packageHash";
|
||||
public static final String LATEST_ROLLBACK_TIME_KEY = "time";
|
||||
public static final String LATEST_ROLLBACK_COUNT_KEY = "count";
|
||||
}
|
||||
|
||||
@@ -510,7 +510,33 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule {
|
||||
public void isFailedUpdate(String packageHash, Promise promise) {
|
||||
try {
|
||||
promise.resolve(mSettingsManager.isFailedHash(packageHash));
|
||||
} catch(CodePushUnknownException e) {
|
||||
} catch (CodePushUnknownException e) {
|
||||
CodePushUtils.log(e);
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getLatestRollbackInfo(Promise promise) {
|
||||
try {
|
||||
JSONObject latestRollbackInfo = mSettingsManager.getLatestRollbackInfo();
|
||||
if (latestRollbackInfo != null) {
|
||||
promise.resolve(CodePushUtils.convertJsonObjectToWritable(latestRollbackInfo));
|
||||
} else {
|
||||
promise.resolve(null);
|
||||
}
|
||||
} catch (CodePushUnknownException e) {
|
||||
CodePushUtils.log(e);
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void setLatestRollbackInfo(String packageHash, Promise promise) {
|
||||
try {
|
||||
mSettingsManager.setLatestRollbackInfo(packageHash);
|
||||
promise.resolve(null);
|
||||
} catch (CodePushUnknownException e) {
|
||||
CodePushUtils.log(e);
|
||||
promise.reject(e);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,8 @@ public class CodePushUtils {
|
||||
map.putString(key, (String) obj);
|
||||
else if (obj instanceof Double)
|
||||
map.putDouble(key, (Double) obj);
|
||||
else if (obj instanceof Long)
|
||||
map.putDouble(key, ((Long) obj).doubleValue());
|
||||
else if (obj instanceof Integer)
|
||||
map.putInt(key, (Integer) obj);
|
||||
else if (obj instanceof Boolean)
|
||||
|
||||
@@ -74,8 +74,7 @@ public class SettingsManager {
|
||||
return pendingUpdate != null &&
|
||||
!pendingUpdate.getBoolean(CodePushConstants.PENDING_UPDATE_IS_LOADING_KEY) &&
|
||||
(packageHash == null || pendingUpdate.getString(CodePushConstants.PENDING_UPDATE_HASH_KEY).equals(packageHash));
|
||||
}
|
||||
catch (JSONException e) {
|
||||
} catch (JSONException e) {
|
||||
throw new CodePushUnknownException("Unable to read pending update metadata in isPendingUpdate.", e);
|
||||
}
|
||||
}
|
||||
@@ -89,6 +88,15 @@ public class SettingsManager {
|
||||
}
|
||||
|
||||
public void saveFailedUpdate(JSONObject failedPackage) {
|
||||
try {
|
||||
if (isFailedHash(failedPackage.getString(CodePushConstants.PACKAGE_HASH_KEY))) {
|
||||
// Do not need to add the package if it is already in the failedUpdates.
|
||||
return;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
throw new CodePushUnknownException("Unable to read package hash from package.", e);
|
||||
}
|
||||
|
||||
String failedUpdatesString = mSettings.getString(CodePushConstants.FAILED_UPDATES_KEY, null);
|
||||
JSONArray failedUpdates;
|
||||
if (failedUpdatesString == null) {
|
||||
@@ -107,6 +115,49 @@ public class SettingsManager {
|
||||
mSettings.edit().putString(CodePushConstants.FAILED_UPDATES_KEY, failedUpdates.toString()).commit();
|
||||
}
|
||||
|
||||
public JSONObject getLatestRollbackInfo() {
|
||||
String latestRollbackInfoString = mSettings.getString(CodePushConstants.LATEST_ROLLBACK_INFO_KEY, null);
|
||||
if (latestRollbackInfoString == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new JSONObject(latestRollbackInfoString);
|
||||
} catch (JSONException e) {
|
||||
// Should not happen.
|
||||
CodePushUtils.log("Unable to parse latest rollback metadata " + latestRollbackInfoString +
|
||||
" stored in SharedPreferences");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setLatestRollbackInfo(String packageHash) {
|
||||
JSONObject latestRollbackInfo = getLatestRollbackInfo();
|
||||
int count = 0;
|
||||
|
||||
if (latestRollbackInfo != null) {
|
||||
try {
|
||||
String latestRollbackPackageHash = latestRollbackInfo.getString(CodePushConstants.LATEST_ROLLBACK_PACKAGE_HASH_KEY);
|
||||
if (latestRollbackPackageHash.equals(packageHash)) {
|
||||
count = latestRollbackInfo.getInt(CodePushConstants.LATEST_ROLLBACK_COUNT_KEY);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
CodePushUtils.log("Unable to parse latest rollback info.");
|
||||
}
|
||||
} else {
|
||||
latestRollbackInfo = new JSONObject();
|
||||
}
|
||||
|
||||
try {
|
||||
latestRollbackInfo.put(CodePushConstants.LATEST_ROLLBACK_PACKAGE_HASH_KEY, packageHash);
|
||||
latestRollbackInfo.put(CodePushConstants.LATEST_ROLLBACK_TIME_KEY, System.currentTimeMillis());
|
||||
latestRollbackInfo.put(CodePushConstants.LATEST_ROLLBACK_COUNT_KEY, count + 1);
|
||||
mSettings.edit().putString(CodePushConstants.LATEST_ROLLBACK_INFO_KEY, latestRollbackInfo.toString()).commit();
|
||||
} catch (JSONException e) {
|
||||
throw new CodePushUnknownException("Unable to save latest rollback info.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void savePendingUpdate(String packageHash, boolean isLoading) {
|
||||
JSONObject pendingUpdate = new JSONObject();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user