Skip to content

Commit

Permalink
- Fix: Race condition situation to local-environment setup process by…
Browse files Browse the repository at this point in the history
… replacing queued execution (reference #547)
  • Loading branch information
rat-moonshine committed May 23, 2019
1 parent bba84ea commit c22e511
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ package actionScripts.utils
import flash.events.IOErrorEvent;
import flash.events.NativeProcessExitEvent;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;
import flash.filesystem.File;
import flash.utils.Timer;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;

import mx.controls.Alert;

import actionScripts.locator.IDEModel;
import actionScripts.plugins.git.model.MethodDescriptor;
import actionScripts.valueObjects.ConstantsCoreVO;
import actionScripts.valueObjects.SDKReferenceVO;
import actionScripts.valueObjects.SDKTypes;
Expand All @@ -44,12 +43,13 @@ package actionScripts.utils
private var customProcess:NativeProcess;
private var customInfo:NativeProcessStartupInfo;
private var isErrorClose:Boolean;
private var watchTimer:Timer;
private var windowsBatchFile:File;
private var externalCallCompletionHandler:Function;
private var executeWithCommands:Array;
private var customSDKPath:String;
private var isDelayRunInProcess:Boolean;
private var processQueus:Array = [];
private var isSingleProcessRunning:Boolean;

public static function getInstance():EnvironmentSetupUtils
{
Expand All @@ -59,52 +59,69 @@ package actionScripts.utils

public function updateToCurrentEnvironmentVariable():void
{
// don't execute in a race condition
if (watchTimer && watchTimer.running) return;
if (!watchTimer)
if (isSingleProcessRunning)
{
watchTimer = new Timer(2000, 1);
watchTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onWatchTimerCompletes);
watchTimer.start();
processQueus.push("executeSetCommand");
return;
}

/*
* @local
*/
function onWatchTimerCompletes(event:TimerEvent):void
{
watchTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, onWatchTimerCompletes);
watchTimer.stop();
watchTimer = null;

cleanUp();
execute();
}
executeSetCommand();
}

public function initCommandGenerationToSetLocalEnvironment(completion:Function, customSDK:String=null, withCommands:Array=null):void
{
cleanUp();
externalCallCompletionHandler = completion;
executeWithCommands = withCommands;
customSDKPath = customSDK;
execute();
if (isSingleProcessRunning)
{
// we'll call the method in our way later
processQueus.push(new MethodDescriptor(null, null, completion, customSDK, withCommands));
return;
}

executeCommandWithSetLocalEnvironment(completion, customSDK, withCommands);
}

private function cleanUp():void
private function flush():void
{
externalCallCompletionHandler = null;
executeWithCommands = null;
windowsBatchFile = null;
customSDKPath = null;

if (processQueus.length != 0)
{
var tmpElement:Object = processQueus.shift();
if (tmpElement is String)
{
executeSetCommand();
}
else if (tmpElement is MethodDescriptor)
{
// we're not going to use methodDescriptor.callMethod()
// as the said method will require the calling method
// to be public; here we call the private one
executeCommandWithSetLocalEnvironment.apply(null, (tmpElement as MethodDescriptor).parameters);
}
}
}

private function execute():void
private function executeSetCommand():void
{
isSingleProcessRunning = true;

if (ConstantsCoreVO.IS_MACOS) executeOSX();
else executeWindows();
}

private function executeCommandWithSetLocalEnvironment(completion:Function, customSDK:String=null, withCommands:Array=null):void
{
isSingleProcessRunning = true;

externalCallCompletionHandler = completion;
executeWithCommands = withCommands;
customSDKPath = customSDK;
executeSetCommand();
}

private function executeWindows():void
{
var setCommand:String = getPlatformCommand();
Expand All @@ -113,6 +130,7 @@ package actionScripts.utils
if (!setCommand)
{
if (externalCallCompletionHandler != null) externalCallCompletionHandler(null);
flush();
return;
}

Expand All @@ -128,6 +146,9 @@ package actionScripts.utils
if (!setCommand)
{
if (externalCallCompletionHandler != null) externalCallCompletionHandler(null);

isSingleProcessRunning = false;
flush();
return;
}

Expand All @@ -137,7 +158,8 @@ package actionScripts.utils
// bash script file path return the full command
// to execute by caller's own nativeProcess process
externalCallCompletionHandler(setCommand);
cleanUp();
isSingleProcessRunning = false;
flush();
}
else
{
Expand Down Expand Up @@ -261,7 +283,9 @@ package actionScripts.utils
// returns batch file path to be
// executed by the caller's nativeProcess process
if (windowsBatchFile) externalCallCompletionHandler(windowsBatchFile.nativePath);
cleanUp();

isSingleProcessRunning = false;
flush();
}
else if (windowsBatchFile)
{
Expand All @@ -285,6 +309,8 @@ package actionScripts.utils
private function onBatchFileWriteError(value:String):void
{
Alert.show("Local environment setup failed[1]!\n"+ value, "Error!");
isSingleProcessRunning = false;
flush();
}

private function startShell(start:Boolean):void
Expand All @@ -310,6 +336,9 @@ package actionScripts.utils
customProcess.removeEventListener(NativeProcessExitEvent.EXIT, shellExit);
customProcess = null;
isErrorClose = false;

isSingleProcessRunning = false;
flush();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ package actionScripts.controllers
protected function onApplicationClosing():void
{
ConstantsCoreVO.IS_APPLICATION_CLOSING = true;
FlexGlobals.topLevelApplication.stage.mouseChildren = false;

if(!timedOutClosingLanguageServers && model.languageServerCore.connectedProjectCount > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
height="100%"
backgroundColor="0x444444"
verticalGap="0" xmlns:project="actionScripts.ui.project.*"
preinitialize="onCreationCompletes()" remove="onRemoval()" xmlns:utils="actionScripts.utils.*"
mouseChildren="{!ConstantsCoreVO.IS_APPLICATION_CLOSING}">
preinitialize="onCreationCompletes()" remove="onRemoval()" xmlns:utils="actionScripts.utils.*">

<mx:Script>
<![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
label="Home"
width="100%"
height="100%" xmlns:splashscreen="components.views.splashscreen.*"
creationComplete="onSplashScreenCreationComplete(event)" removedFromStage="onTabRemoved(event)" horizontalScrollPolicy="off" verticalScrollPolicy="off"
mouseChildren="{!ConstantsCoreVO.IS_APPLICATION_CLOSING}">
creationComplete="onSplashScreenCreationComplete(event)" removedFromStage="onTabRemoved(event)" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
Expand Down

0 comments on commit c22e511

Please sign in to comment.