Module: Cuboid::Rest::Server::Routes::Instances

Defined in:
lib/cuboid/rest/server/routes/instances.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cuboid/rest/server/routes/instances.rb', line 8

def self.registered( app )

    # List instances.
    app.get '/instances' do
        update_from_scheduler

        json instances.keys.inject({}){ |h, k| h.merge! k => {} }
    end

    # Create
    app.post '/instances' do
        max_utilization! if !agent && System.max_utilization?

        options = ::JSON.load( request.body.read ) || {}

        instance = get_instance
        max_utilization! if !instance

        handle_error proc { instance.shutdown } do
            instance.run( options )
        end

        instances[instance.token] = instance

        json id: instance.token
    end

    app.post '/instances/restore' do
        max_utilization! if !agent && System.max_utilization?

        options = ::JSON.load( request.body.read ) || {}

        instance = get_instance
        max_utilization! if !instance

        handle_error proc { instance.shutdown } do
            instance.restore!( options['session'] )
        end

        instances[instance.token] = instance

        json id: instance.token
    end

    # Progress
    app.get '/instances/:instance' do
        ensure_instance!

        session[params[:instance]] ||= {
            seen_errors:  0,
        }

        data = instance_for( params[:instance] ) do |instance|
            instance.progress(
                with:    [
                             errors:  session[params[:instance]][:seen_errors],
                         ]
            )
        end

        session[params[:instance]][:seen_errors] += data[:errors].size

        json data
    end

    app.put '/instances/:instance/scheduler' do |instance|
        ensure_scheduler!
        ensure_instance!

        handle_error do
            instance = instances.delete( instance )
            instance.close

            json scheduler.attach( instance.url, instance.token )
        end
    end

    app.get '/instances/:instance/summary' do
        ensure_instance!

        instance_for( params[:instance] ) do |instance|
            json instance.progress
        end
    end

    app.get '/instances/:instance/report.crf' do
        ensure_instance!
        content_type 'application/octet-stream'

        instance_for( params[:instance] ) do |instance|
            instance.generate_report.to_crf
        end
    end

    app.get '/instances/:instance/report.json' do
        ensure_instance!

        instance_for( params[:instance] ) do |instance|
            instance.generate_report.to_rpc_data.to_json
        end
    end

    app.put '/instances/:instance/pause' do
        ensure_instance!

        instance_for( params[:instance] ) do |instance|
            json instance.pause!
        end
    end

    app.put '/instances/:instance/resume' do
        ensure_instance!

        instance_for( params[:instance] ) do |instance|
            json instance.resume!
        end
    end

    app.put '/instances/:instance/abort' do
        ensure_instance!

        instance_for( params[:instance] ) do |instance|
            json instance.abort!
        end
    end

    # Abort/shutdown
    app.delete '/instances/:instance' do
        ensure_instance!
        id = params[:instance]

        instance = instances[id]
        handle_error { instance.shutdown }

        instances.delete( id ).close

        session.delete params[:instance]

        json nil
    end

end