1
1
import { Base } from "../base/index.js" ;
2
-
3
2
import { YjsConnector } from "../yjsConnector/index.js" ;
4
3
4
+ /**
5
+ * @class Broker
6
+ * @classdesc Classe pour gérer un Broker dans l'application
7
+ * @author scenaristeur
8
+ */
5
9
export class Broker extends Base {
10
+ /**
11
+ * Constructeur de la classe Broker
12
+ * @param {Object } options Options pour initialiser le Broker
13
+ */
6
14
constructor ( options = { } ) {
7
15
super ( options ) ;
16
+ /**
17
+ * Type du Broker
18
+ * @type {String }
19
+ */
8
20
this . options . type = "broker" ;
21
+ /**
22
+ * Style de la console pour les logs du Broker
23
+ * @type {String }
24
+ */
9
25
this . options . style = "normal" ;
26
+ /**
27
+ * Flag pour les logs du Broker
28
+ * @type {String }
29
+ */
10
30
this . flag = "[BROKER][" + this . options . name + "]" ;
31
+ /**
32
+ * Couleur pour les logs du Broker
33
+ * @type {Function }
34
+ */
11
35
this . chalk = this . chalk . blue ;
36
+ /**
37
+ * Instance de YjsConnector pour communiquer avec les autres agents
38
+ * @type {YjsConnector }
39
+ */
12
40
this . yjs = new YjsConnector ( this . options ) ;
41
+ /**
42
+ * Méthode pour écouter les changements d'état dans l'awareness
43
+ */
13
44
this . listenAwareness ( ) ;
45
+ /**
46
+ * Méthode pour mettre à jour l'état de l'awareness
47
+ */
14
48
this . updateAwareness ( ) ;
49
+ /**
50
+ * Map de todos pour les tâches en attente
51
+ * @type {Y.Map<String, Task> }
52
+ */
15
53
this . todos = this . yjs . doc . getMap ( "todos" ) ;
54
+ /**
55
+ * Map de prepared pour les tâches prêtes à être exécutées
56
+ * @type {Y.Map<String, Task> }
57
+ */
16
58
this . prepared = this . yjs . doc . getMap ( "prepared" ) ;
17
- this . listenTodos ( )
59
+ /**
60
+ * Méthode pour écouter les changements sur la map todos
61
+ */
62
+ this . listenTodos ( ) ;
18
63
}
64
+ /**
65
+ * Méthode pour écouter les changements sur la map todos
66
+ */
19
67
listenTodos ( ) {
20
-
21
68
this . todos . observeDeep ( ( events , transaction ) => {
22
- // console.log("events", events, transaction)
23
- this . prepare ( ) ;
24
-
25
- } )
69
+ // console.log("events", events, transaction)
70
+ this . prepare ( ) ;
71
+ } ) ;
26
72
}
27
73
28
-
29
- prepare ( ) {
30
- if ( this . activeBroker . get ( "active" ) == this . id ) { //if this broker is the active broker
31
-
32
- let todos = Array . from ( this . todos . values ( ) )
33
- console . log ( "TODOS tasks" , todos . length , todos )
34
-
35
-
36
- todos . forEach ( ( todo ) => {
37
- let job = this . todos . get ( todo . id ) ;
38
- console . log ( "job" , job )
39
- let workers = Array . from ( this . yjs . awareness . getStates ( ) . values ( ) ) . filter (
40
- ( a ) => {
41
- return a . agent . type == job . type && a . agent . state == 'ready' ;
74
+ /**
75
+ * Méthode pour préparer les tâches en attente
76
+ */
77
+ prepare ( ) {
78
+ if ( this . activeBroker . get ( "active" ) == this . id ) {
79
+ //if this broker is the active broker
80
+ let todos = Array . from ( this . todos . values ( ) ) ;
81
+ console . log ( "TODOS tasks" , todos . length , todos ) ;
82
+
83
+ todos . forEach ( ( todo ) => {
84
+ let job = this . todos . get ( todo . id ) ;
85
+ console . log ( "job" , job ) ;
86
+ let workers = Array . from (
87
+ this . yjs . awareness . getStates ( ) . values ( )
88
+ ) . filter ( ( a ) => {
89
+ return a . agent . type == job . type && a . agent . state == "ready" ;
90
+ } ) ;
91
+ console . log ( "workers" , workers . length , workers ) ;
92
+ if ( workers . length > 0 ) {
93
+ job . worker = workers [ 0 ] . agent . id ;
94
+ job . state = "prepared" ;
95
+ job . worker = workers [ 0 ] . agent . id ;
96
+ job . attemps = 1 ;
97
+ job . start = Date . now ( ) ;
98
+ this . prepared . set ( job . id , job ) ;
99
+ this . todos . delete ( job . id ) ;
100
+ console . log ( job ) ;
101
+ this . log ( "prepare job" , job . id , "for worker " , workers [ 0 ] . agent . id ) ;
102
+ } else {
103
+ this . log ( "no workers for job" , job . id ) ;
42
104
}
43
- )
44
- console . log ( "workers" , workers . length , workers )
45
- if ( workers . length > 0 ) {
46
-
47
-
48
- job . worker = workers [ 0 ] . agent . id ;
49
- job . state = "prepared" ;
50
- job . worker = workers [ 0 ] . agent . id ;
51
- job . attemps = 1 ;
52
- job . start = Date . now ( ) ;
53
- this . prepared . set ( job . id , job ) ;
54
- this . todos . delete ( job . id ) ;
55
- console . log ( job )
56
- this . log ( "prepare job" , job . id , "for worker " , workers [ 0 ] . agent . id )
57
- } else {
58
- this . log ( "no workers for job" , job . id )
105
+ } ) ;
59
106
}
60
-
61
- } )
62
-
63
107
}
64
108
65
- }
66
-
67
-
68
- // prepare1() {
69
- // console.log("prepare TODOS")
70
- // console.log((this.activeBroker.get("active")==this.id))
71
- // if(this.activeBroker.get("active")==this.id){ //if this broker is the active broker
72
- // console.log(Array.from(this.todos.keys()))
73
- // let task_id = Array.from(this.todos.keys())[0];
74
- // let task = this.todos.get(task_id);
75
- // console.log("currenttask", task);
76
- // if (task != undefined){
77
-
78
-
79
- // let workers = Array.from(this.yjs.awareness.getStates().values()).filter(
80
- // (a) => {
81
- // return a.agent.type == task.type && a.agent.state=='ready';
82
- // }
83
- // )
84
- // console.log("workers", workers.length, workers)
85
- // if(workers.length>0){
86
- // task.state = "doing";
87
- // task.worker = workers[0].agent.id;
88
- // task.attemps = 1;
89
- // task.start = Date.now();
90
- // this.doing.set(task_id, task);
91
- // this.todos.delete(task_id);
92
- // console.log(task)
93
- // this.log("prepare task", task_id, "for worker ", workers[0].agent.id)
94
- // }else{
95
- // this.log("no workers available for task", task)
96
- // }
97
- // }
98
- // }
99
-
100
-
101
-
102
- // }
103
-
109
+ /**
110
+ * Méthode pour écouter les changements d'état des agents
111
+ */
104
112
listenAwareness ( ) {
105
- this . activeBroker = this . yjs . doc . getMap ( "activeBroker" ) ;
113
+ this . activeBroker = this . yjs . doc . getMap ( "activeBroker" ) ;
106
114
let awareness = this . yjs . awareness ;
107
115
awareness . on ( "change" , ( changes ) => {
116
+ console . log ( changes )
108
117
let agents = Array . from ( awareness . getStates ( ) . values ( ) ) ;
109
- console . log ( "######BROKER AWARENESS" , agents . length ) ;
118
+ this . log ( "######BROKER AWARENESS" , agents . length , "agents" ) ;
110
119
let brokers = [ ] ;
111
120
agents . forEach ( ( a ) => {
112
121
try {
113
122
this . log (
114
123
a . agent . name ,
115
124
a . agent . type ,
116
125
// a.agent.type,
117
- a . agent . state ,
126
+ a . agent . state
118
127
// a.agent.name,
119
128
// a.agent.id,
120
129
// a.agent.style
@@ -133,16 +142,20 @@ prepare() {
133
142
}
134
143
} ) ;
135
144
brokers = brokers . sort ( ( a , b ) => a . date - b . date ) ;
136
- // console.log("brokers", brokers);
145
+ // console.log("brokers", brokers);
137
146
let active = brokers [ 0 ] . id ;
138
- console . log ( "active" , brokers [ 0 ] ) ;
147
+
139
148
if ( this . activeBroker . get ( "active" ) != active ) {
140
149
this . activeBroker . set ( "active" , active ) ;
141
150
}
142
- this . log ( "activeBroker " , JSON . stringify ( this . activeBroker . toJSON ( ) ) ) ;
143
- console . log ( "#####" , agents . length ) ;
151
+ this . log ( "active broker " , brokers [ 0 ] . id + " " + brokers [ 0 ] . name ) ;
152
+ this . log ( "######BROKER AWARENESS " , brokers . length , "brokers" ) ;
144
153
} ) ;
145
154
}
155
+
156
+ /**
157
+ * Méthode pour mettre à jour l'état local de l'agent dans l'awareness
158
+ */
146
159
updateAwareness ( ) {
147
160
this . yjs . awareness . setLocalStateField ( "agent" , {
148
161
id : this . id ,
@@ -153,3 +166,5 @@ prepare() {
153
166
} ) ;
154
167
}
155
168
}
169
+
170
+
0 commit comments