Module: CPEE

Defined in:
lib/cpee/message.rb,
lib/cpee/controller.rb,
lib/cpee/persistence.rb,
lib/cpee/statemachine.rb,
lib/cpee/value_helper.rb,
lib/cpee/implementation.rb,
lib/cpee/implementation_callbacks.rb,
lib/cpee/implementation_properties.rb,
lib/cpee/implementation_notifications.rb

Overview

This file is part of CPEE.

CPEE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CPEE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CPEE (file COPYING in the main directory). If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Callbacks, Message, Notifications, Persistence, Properties Classes: Controller, DeleteInstance, Info, Instances, NewInstance, StateMachine, ValueHelper

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'..','cpee.xml'))
PROPERTIES_PATHS_FULL =
%w{
  /p:properties/p:handlerwrapper
  /p:properties/p:positions/p:*
  /p:properties/p:positions/p:*/@*
  /p:properties/p:dataelements/p:*
  /p:properties/p:endpoints/p:*
  /p:properties/p:attributes/p:*
  /p:properties/p:transformation/p:*
  /p:properties/p:transformation/p:*/@*
  /p:properties/p:description
  /p:properties/p:dslx
  /p:properties/p:dsl
  /p:properties/p:status/p:id
  /p:properties/p:status/p:message
  /p:properties/p:state/@changed
  /p:properties/p:state
}
PROPERTIES_PATHS_INDEX_UNORDERED =
%w{
  /p:properties/p:positions/p:*
}
PROPERTIES_PATHS_INDEX_ORDERED =
%w{
  /p:properties/p:dataelements/p:*
  /p:properties/p:endpoints/p:*
  /p:properties/p:attributes/p:*
}

Class Method Summary collapse

Class Method Details

.cleanup_services(watchdog_start_off) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cpee/implementation.rb', line 148

def self::cleanup_services(watchdog_start_off)
  return if watchdog_start_off
  Dir[File.join(__dir__,'..','..','server','routing','*.rb')].each do |s|
    s = s.sub(/\.rb$/,'')
    pid = (File.read(s + '.pid').to_i rescue nil)
    if !pid.nil? || (Process.kill(0, pid) rescue false)
      system "#{s}.rb stop 1>/dev/null 2>&1"
      puts "➡ Service #{File.basename(s,'.rb')} stopped ..."
    end
  end
end

.implementation(opts) ⇒ Object



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
# File 'lib/cpee/implementation.rb', line 54

def self::implementation(opts)
  opts[:instances]                  ||= File.expand_path(File.join(__dir__,'..','..','server','instances'))
  opts[:global_handlerwrappers]     ||= File.expand_path(File.join(__dir__,'..','..','server','handlerwrappers'))
  opts[:handlerwrappers]            ||= ''
  opts[:topics]                     ||= File.expand_path(File.join(__dir__,'..','..','server','resources','topics.xml'))
  opts[:properties_init]            ||= File.expand_path(File.join(__dir__,'..','..','server','resources','properties.init'))
  opts[:properties_empty]           ||= File.expand_path(File.join(__dir__,'..','..','server','resources','properties.empty'))
  opts[:transformation_dslx]        ||= File.expand_path(File.join(__dir__,'..','..','server','resources','transformation_dslx.xsl'))
  opts[:transformation_service]     ||= File.expand_path(File.join(__dir__,'..','..','server','resources','transformation.xml'))
  opts[:empty_dslx]                 ||= File.expand_path(File.join(__dir__,'..','..','server','resources','empty_dslx.xml'))
  opts[:notifications_init]         ||= File.expand_path(File.join(__dir__,'..','..','server','resources','notifications'))
  opts[:states]                     ||= File.expand_path(File.join(__dir__,'..','..','server','resources','states.xml'))
  opts[:backend_run]                ||= File.expand_path(File.join(__dir__,'..','..','server','resources','backend','run'))
  opts[:backend_template]           ||= File.expand_path(File.join(__dir__,'..','..','server','resources','backend','instance.template'))
  opts[:backend_opts]               ||= 'opts.yaml'
  opts[:watchdog_frequency]         ||= 7
  opts[:watchdog_start_off]         ||= false
  opts[:backend_instance]           ||= 'instance.rb'
  opts[:infinite_loop_stop]         ||= 10000
  opts[:redis_path]                 ||= '/tmp/redis.sock'
  opts[:redis_db]                   ||= 3
  opts[:sse_keepalive_frequency]    ||= 10

  opts[:sse_connections]            = {}
  opts[:redis]                      = Redis.new(path: opts[:redis_path], db: opts[:redis_db])
  opts[:statemachine]               = CPEE::StateMachine.new opts[:states], %w{running simulating replaying finishing stopping abandoned finished} do |id|
    opts[:redis].get("instance:#{id}/state")
  end

  opts[:runtime_cmds]               << [
    "startclean", "Delete instances before starting.", Proc.new { |status|
      Dir.glob(File.expand_path(File.join(opts[:instances],'*'))).each do |d|
        FileUtils.rm_r(d) if File.basename(d) =~ /^\d+$/
      end
    }
  ]

  Proc.new do
    parallel do
      CPEE::watch_services(opts[:watchdog_start_off])
      EM.add_periodic_timer(opts[:watchdog_frequency]) do ### start services
        CPEE::watch_services(opts[:watchdog_start_off])
      end
      EM.defer do ### catch all sse connections
        CPEE::Notifications::sse_distributor(opts)
      end
      EM.add_periodic_timer(opts[:sse_keepalive_frequency]) do
        CPEE::Notifications::sse_heartbeat(opts)
      end
    end

    cleanup do
      CPEE::cleanup_services(opts[:watchdog_start_off])
    end

    interface 'main' do
      run CPEE::Instances, opts if get '*'
      run CPEE::NewInstance, opts if post 'instance-new'
      on resource '\d+' do |r|
        run CPEE::Info, opts if get
        run CPEE::DeleteInstance, opts if delete
      end
    end

    interface 'properties' do |r|
      id = r[:h]['RIDDL_DECLARATION_PATH'].split('/')[1].to_i
      use CPEE::Properties::implementation(id.to_i, opts)
    end

    interface 'notifications' do |r|
      id = r[:h]['RIDDL_DECLARATION_PATH'].split('/')[1].to_i
      use CPEE::Notifications::implementation(id.to_i, opts)
    end

    interface 'callbacks' do |r|
      id = r[:h]['RIDDL_DECLARATION_PATH'].split('/')[1].to_i
      use CPEE::Callbacks::implementation(id.to_i, opts)
    end
  end
end

.watch_services(watchdog_start_off) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cpee/implementation.rb', line 135

def self::watch_services(watchdog_start_off)
  return if watchdog_start_off
   EM.defer do
     Dir[File.join(__dir__,'..','..','server','routing','*.rb')].each do |s|
       s = s.sub(/\.rb$/,'')
       pid = (File.read(s + '.pid').to_i rescue nil)
       if (pid.nil? || !(Process.kill(0, pid) rescue false)) && !File.exist?(s + '.lock')
         system "#{s}.rb restart 1>/dev/null 2>&1"
         puts "➡ Service #{File.basename(s,'.rb')} started ..."
       end
     end
  end
end