Skip to content

Commit e1881d9

Browse files
committed
Improve waiting for livesync
1 parent 76f9b76 commit e1881d9

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

test-app/app/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.lang.reflect.Field;
1919
import java.lang.reflect.InvocationTargetException;
2020
import java.lang.reflect.Method;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
24+
import java.nio.file.attribute.BasicFileAttributes;
2125
import java.util.TimeZone;
2226

2327
public final class RuntimeHelper {
@@ -194,16 +198,7 @@ public static Runtime initRuntime(Application app) {
194198
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
195199
initLiveSync(runtime, logger, app);
196200

197-
// CLI will create this file when initial sync is needed and then will remove it after syncing the fails and restarting the app
198-
File liveSyncFile = new File("/data/local/tmp/" + app.getPackageName() + "-livesync-in-progress");
199-
if(liveSyncFile.exists()) {
200-
try {
201-
// wait for the livesync to complete
202-
Thread.sleep(30000);
203-
} catch (Exception ex) {
204-
205-
}
206-
}
201+
waitForLiveSync(app);
207202
}
208203

209204
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
@@ -239,6 +234,32 @@ public static Runtime initRuntime(Application app) {
239234
}
240235
}
241236

237+
private static void waitForLiveSync(Application app) {
238+
boolean needToWait = false;
239+
240+
// CLI will create this file when initial sync is needed and then will remove it after syncing the fails and restarting the app
241+
File liveSyncFile = new File("/data/local/tmp/" + app.getPackageName() + "-livesync-in-progress");
242+
if(liveSyncFile.exists()) {
243+
needToWait = true;
244+
Long lastModified = liveSyncFile.lastModified();
245+
// we check for lastModified == 0 as this might happen if we cannot get the actual modified date
246+
if(lastModified > 0) {
247+
Long fileCreatedBeforeMillis = System.currentTimeMillis() - lastModified;
248+
// if last modified date is more than a minute before the current time discard the file as most probably this is a leftover
249+
if (fileCreatedBeforeMillis > 60000) {
250+
needToWait = false;
251+
}
252+
}
253+
}
254+
255+
if(needToWait) {
256+
try {
257+
// wait for the livesync to complete and it should restart the app after deleting the livesync-in-progress file
258+
Thread.sleep(30000);
259+
} catch (Exception ex) { }
260+
}
261+
}
262+
242263
private static void registerTimezoneChangedListener(Context context, final Runtime runtime) {
243264
IntentFilter timezoneFilter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
244265

0 commit comments

Comments
 (0)