Module: CPEE::Worklist

Defined in:
lib/cpee-worklist/user.rb,
lib/cpee-worklist/utils.rb,
lib/cpee-worklist/activities.rb,
lib/cpee-worklist/controller.rb,
lib/cpee-worklist/implementation.rb

Defined Under Namespace

Modules: User Classes: Activities, ActivityHappens, AssignTask, Controller, GetOrgModels, GetStatus, SetStatus, ShowTasks, ShowUserTasks, TaskDel, TaskDetails, TaskGiveBack, TaskTake

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'implementation.xml'))

Class Method Summary collapse

Class Method Details

.cleanup_services(watchdog_start_off) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cpee-worklist/utils.rb', line 39

def self::cleanup_services(watchdog_start_off)
  return if watchdog_start_off
  Dir[File.join(__dir__,'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

}}}



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/cpee-worklist/implementation.rb', line 336

def self::implementation(opts)
  opts[:ORG_SCHEMA] = ::File.join(__dir__, 'organisation.rng')
  opts[:topics] = ::File.join(__dir__, 'topics.xml')

  opts[:top] ||= ::File.join(__dir__, 'data')

  opts[:watchdog_frequency]         ||= 7
  opts[:watchdog_start_off]         ||= false

  ### 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

  controller = CPEE::Worklist::Controller.new(opts)

  CPEE::redis_connect opts, 'Server Main'

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

  opts[:finalize_frequency]         ||= 10

  CPEE::Message::set_workers(1)

  Proc.new do
    parallel do
      CPEE::Worklist::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::Worklist::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
      EM.add_periodic_timer(opts[:finalize_frequency]) do
        controller.activities.each_with_index do |activity,index|
          begin
            if activity['collect'] && activity['collected'] && activity['deadline'] && activity['collected'] >= activity['collect'] && Time.parse(activity['deadline'].to_s) < Time.now
              activity = controller.activities.delete_at(index)
              controller.activities.serialize
              controller.notify('user/finish', :callback_id => activity['id'], :instance_uuid => activity['uuid'], :cpee_callback => activity['url'], :cpee_instance => activity['cpee_instance'], :cpee_base => activity['cpee_base'], :cpee_label => activity['label'], :cpee_activity => activity['cpee_activity_id'], :orgmodel => activity['orgmodel'])
              Riddl::Client.new(activity['url']).put
            end
          rescue => e
            puts e.message
            puts e.backtrace
          end
        end
      end
    end

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

    interface 'main' do
      run ActivityHappens,controller if post 'activityhappens'
      run ShowTasks,controller if get
      on resource 'callbacks' do
        use CPEE::Callbacks::implementation(opts)
      end
      on resource 'orgmodels' do
        run GetOrgModels,controller if get
      end
      on resource 'tasks' do
        on resource do
          run AssignTask,controller if put 'uid'
          run TaskDel,controller if delete
        end
      end
      on resource do
        run SetStatus, controller, opts[:top] if put 'status'
        run GetStatus, opts[:top] if get
        on resource 'tasks' do
          run ShowUserTasks,controller if get
          on resource do |r|
            run TaskDetails,controller if get
            run TaskTake,controller if put 'take'
            run TaskGiveBack,controller if put 'giveback'
            run TaskDel,controller if delete
          end
        end
      end
    end

    interface 'notifications' do |r|
      use CPEE::Notifications::implementation('worklist',opts)
    end

  end
end

.watch_services(watchdog_start_off, url, path, db) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cpee-worklist/utils.rb', line 20

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