1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
require 'rubygems'
gem 'opentox-ruby-api-wrapper', '= 1.2.7'
require 'opentox-ruby-api-wrapper'
require "dm-is-tree"
class Task
include DataMapper::Resource
property :id, Serial
property :parent_id, Integer
property :pid, Integer
property :uri, String, :length => 255
property :resource, String, :length => 255
property :status, String, :default => "created"
property :created_at, DateTime
property :finished_at, DateTime
is :tree, :order => :created_at
end
DataMapper.auto_upgrade!
get '/?' do
response['Content-Type'] = 'text/uri-list'
Task.all.collect{|t| t.uri}.join("\n")
end
get '/:id/?' do
response['Content-Type'] = 'application/x-yaml'
task = Task.get(params[:id])
task.to_yaml
end
# dynamic access to Task properties
get '/:id/:property/?' do
response['Content-Type'] = 'text/plain'
task = Task.get(params[:id])
eval("task.#{params[:property]}").to_s
end
post '/?' do
LOGGER.debug "Creating new task ..."
task = Task.new
task.save # needed to create id
task.uri = url_for("/#{task.id}", :full)
raise "could not save" unless task.save
response['Content-Type'] = 'text/uri-list'
task.uri
end
put '/:id/:status/?' do
task = Task.get(params[:id])
task.status = params[:status] unless /pid|parent/ =~ params[:status]
case params[:status]
when "completed"
task.resource = params[:resource]
task.finished_at = DateTime.now
task.pid = nil
when "pid"
task.pid = params[:pid]
when "parent"
task.parent = Task.first(:uri => params[:uri])
when /cancelled|failed/
Process.kill(9,task.pid) unless task.pid.nil?
task.pid = nil
RestClient.put url_for("/#{task.parent.id}/#{params[:status]}"), {} unless task.parent.nil? # recursevly kill parent tasks
end
task.save
end
delete '/:id/?' do
task = Task.get(params[:id])
begin
Process.kill(9,task.pid) unless task.pid.nil?
rescue
"Cannot kill task with pid #{task.pid}"
end
task.destroy!
response['Content-Type'] = 'text/plain'
"Task #{params[:id]} deleted."
end
delete '/?' do
Task.all.each do |task|
begin
Process.kill(9,task.pid) unless task.pid.nil?
rescue
"Cannot kill task with pid #{task.pid}"
end
#task.destroy!
end
Task.auto_migrate!
response['Content-Type'] = 'text/plain'
"All tasks deleted."
end
|