-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3 carga de modificações segundo pytrobot
- Loading branch information
1 parent
0b7baf6
commit b5d4a45
Showing
8 changed files
with
205 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// state/baseState.js | ||
const { Singleton } = require('../singleton'); | ||
const { logger } = require('../utility/log'); | ||
|
||
class BaseState extends Singleton { | ||
constructor(stateMachineOperator) { | ||
super(); | ||
this.logger = logger; | ||
this.stateMachineOperator = stateMachineOperator; | ||
this._status = null; | ||
this.retryCounter = 3; | ||
this.reset = false; | ||
} | ||
|
||
transition(onSuccess = null, onFailure = null) { | ||
this.stateMachineOperator({ onSuccess, onFailure }); | ||
} | ||
|
||
execute() { | ||
throw new Error('The "execute" method must be implemented by the subclass.'); | ||
} | ||
|
||
onEntry() { | ||
throw new Error('The "onEntry" method must be implemented by the subclass.'); | ||
} | ||
|
||
onExit() { | ||
throw new Error('The "onExit" method must be implemented by the subclass.'); | ||
} | ||
|
||
onError(error) { | ||
throw new Error('The "onError" method must be implemented by the subclass.'); | ||
} | ||
|
||
_execute() { | ||
this.execute(); | ||
} | ||
|
||
_onEntry() { | ||
this.onEntry(); | ||
} | ||
|
||
_onExit() { | ||
this._status = true; | ||
this.onExit(); | ||
} | ||
|
||
_onError(error) { | ||
this._status = false; | ||
if (this.retryCounter > 0) { | ||
this.retryCounter -= 1; | ||
this.logger.warn(`Attempt failed. ${this.retryCounter} attempts remaining.`); | ||
this.transition(this.constructor.name, null); | ||
} else { | ||
this.logger.warn("Maximum number of attempts reached."); | ||
} | ||
this.onError(error); | ||
} | ||
} | ||
|
||
module.exports = BaseState; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// state/stateManager.js | ||
const { logger } = require('../utility/log'); | ||
const StateRegistry = require('./stateRegistry'); | ||
|
||
class StateManager { | ||
constructor(stateRegistry) { | ||
this.stateRegistry = stateRegistry; | ||
this.stateRegistry.updateStateOperator(this.stateMachineOperator.bind(this)); | ||
this.logger = logger; | ||
const starterStateInfo = this.stateRegistry.getStateInfo('_StarterState'); | ||
this.currentState = starterStateInfo ? starterStateInfo.instance : null; | ||
this.nextStateOnSuccess = null; | ||
this.nextStateOnFailure = null; | ||
} | ||
|
||
stateMachineOperator({ onSuccess = null, onFailure = null }) { | ||
if (onSuccess) { | ||
const successStateInfo = this.stateRegistry.getStateInfo(onSuccess); | ||
if (!successStateInfo) { | ||
throw new Error(`State info for success '${onSuccess}' not found.`); | ||
} | ||
this.nextStateOnSuccess = successStateInfo.instance; | ||
} | ||
|
||
if (onFailure) { | ||
const failureStateInfo = this.stateRegistry.getStateInfo(onFailure); | ||
if (!failureStateInfo) { | ||
throw new Error(`State info for failure '${onFailure}' not found.`); | ||
} | ||
this.nextStateOnFailure = failureStateInfo.instance; | ||
} | ||
} | ||
|
||
run() { | ||
while (this.currentState) { | ||
try { | ||
this.currentState._onEntry(); | ||
this.currentState._execute(); | ||
this.currentState._onExit(); | ||
this.currentState = this.nextStateOnSuccess; | ||
} catch (error) { | ||
this.currentState._onError(error); | ||
this.currentState = this.nextStateOnFailure; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = StateManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// state/privateStates.js | ||
const BaseState = require('./baseState'); | ||
const MultithreadManager = require('../feature/multithreadManager'); | ||
const SubprocessManager = require('../feature/subprocessManager'); | ||
|
||
class FinisherState extends BaseState { | ||
execute() { } | ||
|
||
onEntry() { | ||
this.multithreadManager = new MultithreadManager(); | ||
this.subprocessManager = new SubprocessManager(); | ||
} | ||
|
||
onExit() { | ||
this.multithreadManager.listActiveThreads(); | ||
this.subprocessManager.listActiveProcesses(); | ||
process.exit(); | ||
} | ||
|
||
onError() { | ||
process.exit(1); | ||
} | ||
} | ||
|
||
class StarterState extends BaseState { | ||
execute() { } | ||
onEntry() { } | ||
onExit() { } | ||
onError() { } | ||
} | ||
|
||
module.exports = { FinisherState, StarterState }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// state/stateRegistry.js | ||
const { Singleton } = require('../singleton'); | ||
const BaseRegistry = require('../core/baseRegistry'); // Supondo que o BaseRegistry esteja no core | ||
|
||
class StateRegistry extends BaseRegistry { | ||
constructor() { | ||
super(); | ||
if (StateRegistry.instance) { | ||
return StateRegistry.instance; | ||
} | ||
this.states = {}; | ||
this.firstStateName = null; | ||
this.stateMachineOperator = null; | ||
StateRegistry.instance = this; // Singleton logic | ||
return this; | ||
} | ||
|
||
updateStateOperator(operatorFunc) { | ||
Object.values(this.states).forEach(stateInfo => { | ||
stateInfo.instance.stateMachineOperator = operatorFunc; | ||
}); | ||
} | ||
|
||
register(currentState, nextStateOnSuccess, nextStateOnFailure) { | ||
this.states[currentState.name] = { | ||
instance: new currentState(this.stateMachineOperator), | ||
successState: nextStateOnSuccess, | ||
failureState: nextStateOnFailure | ||
}; | ||
} | ||
|
||
getStateInfo(stateName) { | ||
return this.states[stateName]; | ||
} | ||
} | ||
|
||
module.exports = StateRegistry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// state/stateStrategy.js | ||
const ApplicationStrategy = require('../core/applicationStrategy'); | ||
const StateManager = require('./stateManager'); | ||
const StateRegistry = require('./stateRegistry'); | ||
|
||
class StateStrategy extends ApplicationStrategy { | ||
constructor() { | ||
super(); | ||
this.stateManager = null; | ||
} | ||
|
||
initialize() { | ||
const stateRegistry = new StateRegistry(); | ||
this.stateManager = new StateManager(stateRegistry); | ||
} | ||
|
||
start() { | ||
this.multithreadManager.newThread(() => this.stateManager.run()); | ||
} | ||
|
||
stop() { | ||
this.multithreadManager.stopThread(() => this.stateManager.run()); | ||
} | ||
} | ||
|
||
module.exports = StateStrategy; |