Method: CPEE.implementation

Defined in:
lib/cpee/implementation.rb

.implementation(opts) ⇒ Object



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

def self::implementation(opts)
  opts[:instances]                  ||= File.expand_path(File.join(__dir__,'..','..','server','instances'))
  opts[:global_executionhandlers]   ||= File.expand_path(File.join(__dir__,'..','..','server','executionhandlers'))
  opts[:executionhandlers]          ||= ''
  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_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[:watchdog_frequency]         ||= 7
  opts[:watchdog_start_off]         ||= false
  opts[:infinite_loop_stop]         ||= 10000

  ### set redis_cmd to nil if you want to do global
  ### at least redis_path or redis_url and redis_db have to be set if you do global
  opts[:redis_path]                 ||= 'redis.sock' # use e.g. /tmp/redis.sock for global stuff. Look it up in your redis config
  opts[:redis_db]                   ||= 0
  ### optional redis stuff
  opts[:redis_url]                  ||= nil
  opts[:redis_cmd]                  ||= 'redis-server --port 0 --unixsocket #redis_path# --unixsocketperm 600 --pidfile #redis_pid# --dir #redis_db_dir# --dbfilename #redis_db_name# --databases 1 --save 900 1 --save 300 10 --save 60 10000 --rdbcompression yes --daemonize yes'
  opts[:redis_pid]                  ||= 'redis.pid' # use e.g. /var/run/redis.pid if you do global. Look it up in your redis config
  opts[:redis_db_name]              ||= 'redis.rdb' # use e.g. /var/lib/redis.rdb for global stuff. Look it up in your redis config

  CPEE::redis_connect opts, 'Server Main'

  opts[:sse_keepalive_frequency]    ||= 10
  opts[:sse_connections]            = {}

  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
    Dir[File.join(opts[:global_executionhandlers],'*','execution.rb')].each do |h|
      require h
    end unless opts[:global_executionhandlers].nil? || opts[:global_executionhandlers].strip == ''
    Dir[File.join(opts[:executionhandlers],'*','execution.rb')].each do |h|
      require h
    end unless opts[:executionhandlers].nil? || opts[:executionhandlers].strip == ''

    parallel do
      CPEE::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
      EM.add_periodic_timer(opts[:watchdog_frequency]) do ### start services
        CPEE::watch_services(opts[:watchdog_start_off],opts[:redis_url],File.join(opts[:basepath],opts[:redis_path]),opts[:redis_db])
      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