15
15
:variant =" ['error', 'cancelled'].includes(data.item.status) ? 'danger' : 'primary'"
16
16
>{{ data.item.status }}</b-badge >
17
17
</template >
18
+
18
19
<template v-slot :cell (progress)="data">
19
20
<b-progress
20
21
v-if =" data.item.status === 'encoding'"
49
50
</template >
50
51
51
52
<template v-slot :row-details =" row " >
52
- <b-row class =" mb-2" >
53
- <b-col sm =" 2" class =" text-sm-right" ><b >Input:</b ></b-col >
54
- <b-col >{{ row.item.input }}</b-col >
55
- </b-row >
56
-
57
- <b-row class =" mb-2" >
58
- <b-col sm =" 2" class =" text-sm-right" ><b >Output:</b ></b-col >
59
- <b-col >
60
- <a :href =" videoUrl(row.item.output)" target =" _blank" >{{ row.item.output }}</a >
61
- </b-col >
62
- </b-row >
63
-
64
- <b-row class =" mb-2" >
65
- <b-col sm =" 2" class =" text-sm-right" ><b >Payload:</b ></b-col >
66
- <b-col >
53
+ <b-row class =" mb-2" >
54
+ <b-col sm =" 2" class =" text-sm-right" ><b >Input:</b ></b-col >
55
+ <b-col >{{ row.item.input }}</b-col >
56
+ </b-row >
57
+
58
+ <b-row class =" mb-2" >
59
+ <b-col sm =" 2" class =" text-sm-right" ><b >Output:</b ></b-col >
60
+ <b-col >
61
+ <a :href =" videoUrl(row.item.output)" target =" _blank" >{{ row.item.output }}</a >
62
+ </b-col >
63
+ </b-row >
64
+
65
+ <b-row class =" mb-2" >
66
+ <b-col sm =" 2" class =" text-sm-right" ><b >Payload:</b ></b-col >
67
+ <b-col >
68
+ <b-form-textarea
69
+ class =" code"
70
+ rows =" 6"
71
+ size =" sm"
72
+ readonly
73
+ :value =" JSON.stringify(row.item.payload, null, 2)"
74
+ ></b-form-textarea >
75
+ </b-col >
76
+ </b-row >
77
+
78
+ <b-row class =" mb-2" v-if =" row.item.status === 'completed'" >
79
+ <b-col sm =" 2" class =" text-sm-right" ><b >Preview:</b ></b-col >
80
+ <b-col >
81
+ <video :src =" videoUrl(row.item.output)" width =" 70%" controls muted />
82
+ </b-col >
83
+ </b-row >
84
+
85
+ <!-- Display error if recieved from message -->
86
+ <b-row class =" mb-2" v-if =" row.item.error" >
87
+ <b-col sm =" 2" class =" text-sm-right" ><b >Error:</b ></b-col >
88
+ <b-col >
89
+ <div class =" code" >
67
90
<b-form-textarea
68
- class =" code"
69
- rows =" 6"
70
- size =" sm"
71
- readonly
72
- :value =" JSON.stringify(row.item.payload, null, 2)"
91
+ rows =" 3"
92
+ max-rows =" 3"
93
+ :value =" JSON.stringify(row.item.error)"
73
94
></b-form-textarea >
74
- </b-col >
75
- </b-row >
76
-
77
- <b-row class =" mb-2" v-if =" row.item.status === 'completed'" >
78
- <b-col sm =" 2" class =" text-sm-right" ><b >Preview:</b ></b-col >
79
- <b-col >
80
- <video :src =" videoUrl(row.item.output)" width =" 70%" controls muted />
81
- </b-col >
82
- </b-row >
83
-
84
- <!-- Display error if recieved from message -->
85
- <b-row class =" mb-2" v-if =" row.item.error" >
86
- <b-col sm =" 2" class =" text-sm-right" ><b >Error:</b ></b-col >
87
- <b-col >
88
- <div class =" code" >
89
- <b-form-textarea
90
- rows =" 3"
91
- max-rows =" 3"
92
- :value =" JSON.stringify(row.item.error)"
93
- ></b-form-textarea >
94
- </div >
95
- </b-col >
96
- </b-row >
97
-
95
+ </div >
96
+ </b-col >
97
+ </b-row >
98
98
</template >
99
99
</b-table >
100
- <h2 class =" text-center" v-if =" items.length === 0" >No Jobs Found </h2 >
100
+ <h2 class =" text-center" v-if =" items.length === 0" >No Jobs in Queue </h2 >
101
101
</div >
102
102
</template >
103
103
@@ -106,6 +106,7 @@ import storage from '@/storage';
106
106
import store from ' @/store' ;
107
107
108
108
const WS_INTERVAL = 5000 ;
109
+ const DEFAULT_VIDEO_HOST = ' http://127.0.0.1:8080' ;
109
110
const Status = {
110
111
QUEUED : ' queued' ,
111
112
ENCODING : ' encoding' ,
@@ -117,8 +118,11 @@ const Status = {
117
118
export default {
118
119
name: ' Queue' ,
119
120
created () {
121
+ // Get items in queue store.
120
122
this .getQueue ();
121
- this .onMessage ();
123
+
124
+ // Initialize websocket connecction.
125
+ this .initWS ();
122
126
},
123
127
data () {
124
128
return {
@@ -142,66 +146,76 @@ export default {
142
146
};
143
147
},
144
148
methods: {
145
- onMessage () {
149
+ initWS () {
150
+ // Check if the websocket connection is ready. If so, start queue and listen for messages.
146
151
setInterval (() => {
147
152
if (this .$ws .conn && this .$ws .conn .readyState === WebSocket .OPEN ) {
148
- this .$ws .conn .onmessage = (event ) => {
149
- const data = JSON .parse (event .data );
150
- this .percent = data .percent ;
151
- this .speed = data .speed ;
152
- this .fps = data .fps ;
153
- this .err = data .err ;
154
-
155
- if (this .percent === 100 ) {
156
- this .setJobStatus (this .job .id , Status .COMPLETED );
157
- store .setIsEncoding (false );
158
- }
159
-
160
- if (this .err ) {
161
- this .setJobStatus (this .job .id , Status .ERROR );
162
- store .setIsEncoding (false );
163
- storage .set (' queue' , this .job .id , ' error' , this .err );
164
- }
165
- };
166
- this .$ws .conn .onerror = () => {
167
- if (this .job .status !== Status .COMPLETED ) {
168
- this .setJobStatus (this .job .id , Status .ERROR );
169
- }
170
- store .setIsEncoding (false );
171
- };
153
+ this .$ws .conn .onmessage = this .onWSMessage ;
154
+ this .$ws .conn .onerror = this .onWSError ;
172
155
this .startQueue ();
173
156
}
174
157
}, WS_INTERVAL );
175
158
},
159
+
160
+ onWSMessage (event ) {
161
+ // On websocket message, update the progress details (or error).
162
+ const data = JSON .parse (event .data );
163
+ this .percent = data .percent ;
164
+ this .speed = data .speed ;
165
+ this .fps = data .fps ;
166
+ this .err = data .err ;
167
+
168
+ // Set completed job.
169
+ if (this .percent === 100 ) {
170
+ this .setJobStatus (this .job .id , Status .COMPLETED );
171
+ store .setIsEncoding (false );
172
+ }
173
+
174
+ // Set errored job.
175
+ if (this .err ) {
176
+ this .setJobStatus (this .job .id , Status .ERROR );
177
+ store .setIsEncoding (false );
178
+ storage .set (' queue' , this .job .id , ' error' , this .err );
179
+ }
180
+ },
181
+
182
+ onWSError () {
183
+ if (this .job .status !== Status .COMPLETED ) {
184
+ this .setJobStatus (this .job .id , Status .ERROR );
185
+ }
186
+ store .setIsEncoding (false );
187
+ },
188
+
176
189
startQueue () {
177
190
this .getQueue ();
178
191
179
192
setInterval (() => {
180
193
this .getQueue ();
181
194
195
+ // Pass if job is already encoding.
182
196
if (this .job .status && this .job .status === Status .ENCODING ) {
183
197
store .setIsEncoding (true );
184
198
return ;
185
199
}
186
200
187
- const job = this .getNextJob ();
188
- if (! job .id ) {
189
- return ;
190
- }
191
- this .job = job;
201
+ // Get next queued job if available.
202
+ this .getNextJob ();
192
203
193
- this .sendEncode ();
194
- }, 5000 );
204
+ if ( this . job . id ) this .sendEncode ();
205
+ }, WS_INTERVAL );
195
206
},
207
+
196
208
getQueue () {
197
209
this .items = storage .getAll (' queue' );
198
210
},
211
+
199
212
getNextJob () {
200
213
const filtered = this .items .filter (
201
214
(item ) => item .status === Status .QUEUED || item .status === Status .ENCODING ,
202
215
);
203
- return filtered[0 ] || {};
216
+ this . job = filtered[0 ] || {};
204
217
},
218
+
205
219
sendEncode () {
206
220
this .$ws .conn .send (JSON .stringify ({
207
221
type: ' encode' ,
@@ -211,31 +225,33 @@ export default {
211
225
}));
212
226
this .setJobStatus (this .job .id , Status .ENCODING );
213
227
},
228
+
214
229
setJobStatus (id , status ) {
215
230
this .job .status = status;
216
231
storage .updateStatus (' queue' , id, status);
232
+ this .getQueue ();
217
233
},
234
+
218
235
clearJobs () {
219
236
storage .deleteAll (' queue' );
220
237
this .getQueue ();
221
238
},
239
+
222
240
onClickCancel (id ) {
223
- this .job .status = Status .CANCELLED ;
224
241
this .setJobStatus (id, Status .CANCELLED );
225
- this .getQueue ();
226
242
},
243
+
227
244
onClickRestart (id ) {
228
- this .job .status = Status .QUEUED ;
229
245
this .setJobStatus (id, Status .QUEUED );
230
- this .getQueue ();
231
246
},
247
+
232
248
toggleDetails (id , enabled ) {
233
- // eslint-disable-next-line no-underscore-dangle
234
249
storage .toggleDetails (' queue' , id, ! enabled);
235
250
this .getQueue ();
236
251
},
252
+
237
253
videoUrl (output ) {
238
- return ` http://127.0.0.1:8080 /${ output} ` ;
254
+ return ` ${ DEFAULT_VIDEO_HOST } /${ output} ` ;
239
255
},
240
256
},
241
257
};
0 commit comments