Skip to content

Commit 10b8dac

Browse files
committed
Bug fixing, improved live test
1 parent 2762803 commit 10b8dac

File tree

8 files changed

+56
-8
lines changed

8 files changed

+56
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
22
*.pyc
3+
server.cfg

PySiQ.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ def remove_task(self, task_id):
203203
try:
204204
self.lock.acquire() # LOCK CACHE
205205
if self.tasks.has_key(task_id):
206+
logging.debug("Removing task " + str(task_id))
206207
self.queue.remove(self.tasks.get(task_id))
207208
del self.tasks[task_id]
209+
else:
210+
logging.debug("Failed removing task " + str(task_id) + ". Not found.")
208211
finally:
209212
self.lock.release() #UNLOCK CACHE
210213

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* [Using PySiQ as a self-contained service](#using-pysiq-as-a-self-contained-service)
1010

1111
<!-- Added by: rhernandez, at: 2018-04-18T21:53+02:00 -->
12-
1312
<!--te-->
1413

1514
# Introduction

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.3 APRIL 2018 PySiQ, a Python Simple Queue system
1+
v0.4 August 2018 PySiQ, a Python Simple Queue system

runserver.py

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
try:
24
from server import Application
35
except Exception as ex:
File renamed without changes.

server.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import json
1919
from imp import load_source
2020
from inspect import getmembers, isfunction
21+
from shutil import copyfile
2122

2223
class Application(object):
2324
#******************************************************************************************************************
@@ -68,17 +69,28 @@ def enqueue():
6869
def check_status(task_id):
6970
if request.method == "OPTIONS":
7071
return jsonify(True)
71-
return jsonify({'success': True, 'status': self.queue_instance.check_status(task_id).name})
72+
try: # Fix issue #2 reported by @jeremydouglass
73+
return jsonify({'success': True, 'status': self.queue_instance.check_status(task_id).name}) # This may work depending on the version of python
74+
except Exception as e:
75+
return jsonify({'success': True, 'status': self.queue_instance.check_status(task_id)})
7276

7377
@self.app.route(self.settings.get("SERVER_SUBDOMAIN", "") + '/api/result/<path:task_id>', methods=['OPTIONS', 'GET'])
7478
def get_result(task_id):
7579
if request.method == "OPTIONS":
7680
return jsonify(True)
77-
result = self.queue_instance.get_result(task_id)
81+
remove = (request.args.get('remove', default=0) == 0)
82+
result = self.queue_instance.get_result(task_id, remove)
7883
if isinstance(result, TaskStatus):
7984
return jsonify({'success': False, 'message': "Task couldn't be found in queue"})
8085
return jsonify({'success': True, 'result': result})
8186

87+
@self.app.route(self.settings.get("SERVER_SUBDOMAIN", "") + '/api/remove/<path:task_id>', methods=['OPTIONS', 'DELETE'])
88+
def remove_task(task_id):
89+
if request.method == "OPTIONS":
90+
return jsonify(True)
91+
self.queue_instance.remove_task(task_id)
92+
return jsonify({'success': True})
93+
8294
def load_functions(self):
8395
functions = {}
8496
for functions_source in self.settings.get("FUNCTIONS_DIRS", []):
@@ -114,6 +126,10 @@ def log(self, message, type="info"):
114126

115127
def read_settings_file():
116128
conf_path = os.path.dirname(os.path.realpath(__file__)) + "/server.cfg"
129+
# Copy the default settings
130+
if not os.path.isfile(conf_path):
131+
copyfile(os.path.dirname(os.path.realpath(__file__)) + "/server.default.cfg", conf_path)
132+
117133
settings = {}
118134
if os.path.isfile(conf_path):
119135
config = json.load(open(conf_path))

test/test_server.html

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
if (response.success) {
5454
task.result = response.result;
5555
} else {
56-
task.status = "UNABLE TO GET THE RESULT.";
56+
task.result = "UNABLE TO GET THE RESULT.";
5757
}
5858
sessionStorage.tasks = JSON.stringify($scope.tasks);
5959
$scope.$apply();
@@ -81,11 +81,13 @@
8181
depend: depend,
8282
incompatible: incompatible,
8383
status: "UNKNOWN",
84-
result: "-"
84+
result: "-",
85+
elapsed_time: 0
8586
})
8687
$scope.$apply();
8788
sessionStorage.tasks = JSON.stringify($scope.tasks);
8889
alert("New task was succesfully enqueued!");
90+
$scope.initNewTask();
8991
} else {
9092
alert("Oops! Something went wrong when trying to enqueue your new task!");
9193
}
@@ -98,6 +100,29 @@
98100
$scope.tasks = [];
99101
sessionStorage.tasks = JSON.stringify($scope.tasks);
100102
}
103+
104+
$scope.initNewTask = function() {
105+
$scope.task_name = "Task " + ($scope.tasks.length + 1);
106+
$scope.n_seconds = Math.floor(Math.random() * 19) + 1;
107+
$scope.task_result = "Task " + ($scope.tasks.length + 1) + " is done!";
108+
}
109+
110+
111+
$scope.updateElapsedTime = function() {
112+
for (var i in $scope.tasks) {
113+
if ($scope.tasks[i].status !== "FINISHED" && $scope.tasks[i].status !== "FAILED") {
114+
$scope.tasks[i].elapsed_time += 1
115+
}
116+
}
117+
};
118+
119+
$scope.initNewTask();
120+
121+
//Launch the intervar
122+
setInterval(function() {
123+
$scope.updateElapsedTime();
124+
}, 1000);
125+
101126
//Launch the intervar
102127
setInterval(function() {
103128
for (var i in $scope.tasks) {
@@ -130,19 +155,20 @@ <h1>PySiQ server example</h1>
130155
<label for="task_name">A message to show when task ends:</label>
131156
<input type="text" class="form-control" id="task_result" ng-model="task_result">
132157
</div>
133-
<button class="btn btn-default" ng-click="addNewTask(task_name, n_seconds, task_result, [], [])">Run it!</button>
158+
<button class="btn btn-primary" ng-click="addNewTask(task_name, n_seconds, task_result, [], [])">Run it!</button>
134159
</div>
135160
</div>
136161
</div>
137162
<div class="panel panel-default">
138-
<div class="panel-heading">Queue content <a style="margin-top:-5px;"class="btn btn-danger btn-sm pull-right" ng-click="clearTasks()"><span class="glyphicon glyphicon-trash"></span> Clear tasks</a></div>
163+
<div class="panel-heading">Queue content (2 workers) <a style="margin-top:-5px;" class="btn btn-danger btn-sm pull-right" ng-click="clearTasks()"><span class="glyphicon glyphicon-trash"></span> Clear tasks</a></div>
139164
<div class="panel-body">
140165
<table class="table">
141166
<thead>
142167
<tr>
143168
<th>Task name</th>
144169
<th>Task ID</th>
145170
<th>Status</th>
171+
<th>Elapsed time</th>
146172
<th>Result</th>
147173
</tr>
148174
</thead>
@@ -157,6 +183,7 @@ <h1>PySiQ server example</h1>
157183
<span ng-if="['FAILED'].indexOf(task.status) > -1" class="label label-danger">{{task.status}}</span>
158184
<span ng-if="['QUEUED', 'STARTED', 'FINISHED', 'FAILED'].indexOf(task.status) === -1" class="label label-default">{{task.status}}</span>
159185
</td>
186+
<td>{{task.elapsed_time}}</td>
160187
<td>{{task.result}}</td>
161188
</tr>
162189
</tbody>

0 commit comments

Comments
 (0)