Skip to content

Commit

Permalink
3 carga de modificações segundo pytrobot
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuscorteletti-wmtrading committed Oct 11, 2024
1 parent 0b7baf6 commit b5d4a45
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 54 deletions.
61 changes: 61 additions & 0 deletions src/core/strategy/state/baseState.js
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;
18 changes: 0 additions & 18 deletions src/core/strategy/state/index copy 2.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/core/strategy/state/index copy.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/core/strategy/state/index.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/core/strategy/state/manager.js
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;
32 changes: 32 additions & 0 deletions src/core/strategy/state/privateStates.js
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 };
37 changes: 37 additions & 0 deletions src/core/strategy/state/registry.js
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;
26 changes: 26 additions & 0 deletions src/core/strategy/state/strategy.js
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;

0 comments on commit b5d4a45

Please sign in to comment.