cf_light_api 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cf_light_api/worker.rb +61 -57
  3. metadata +18 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e022501ee1125531178e1d883f0ca616f0877c2
4
- data.tar.gz: 36785e5ffd9c69455b6333895ae8811c2d22261f
3
+ metadata.gz: c72fd921339d1874c9b1086404e38afee9f88d5f
4
+ data.tar.gz: e3f5e8773925d7515b6ecbb917054159fde095d4
5
5
  SHA512:
6
- metadata.gz: 7a7d27982b5759dba905703c308cc03aa4d754d3945e537143cd7b28df82d96383f58b6d8a35ee587b6fc0bcb2bee6a22b6baa6c25738efb45155f1adb71f85d
7
- data.tar.gz: 153f4bca6ed48fe0772b1e4f4fee2e723fa3be7c8d58ce9d38c70cc32d5d53995bd4418888dddf2d29fbb4f1fdc0363aacc9f62dcc4801d90ee55048e690088c
6
+ metadata.gz: 55bb2150f1c2d22ba2d483ddaea27b26e7649433c097531e9997478147ca06b938e0aa2fbeff2d8a45c20ef755f3e45e8cbbfd4f33e1b18623ca471c079b210b
7
+ data.tar.gz: 5b30c97ec7f672fc1f50577dbdaab07cdfac22bfdbb5b8dbf2497af72803ab83df76762e2c36c31714a08a74fe05461b7d280908591e32cd0e8b361c55fbd2e5
@@ -2,84 +2,89 @@ require 'cfoundry'
2
2
  require 'json'
3
3
  require 'rufus-scheduler'
4
4
  require 'parallel'
5
+ require 'redlock'
6
+ require 'logger'
7
+
8
+ @logger = Logger.new(STDOUT)
9
+ @logger.formatter = proc do |severity, datetime, progname, msg|
10
+ "#{datetime} [cf_light_api:worker]: #{msg}\n"
11
+ end
5
12
 
6
13
  ['CF_API', 'CF_USER', 'CF_PASSWORD'].each do |env|
7
- puts "[cf_light_api:worker] Error: please set the '#{env}' environment variable." unless ENV[env]
14
+ @logger.info "Error: please set the '#{env}' environment variable." unless ENV[env]
8
15
  next
9
16
  end
10
17
 
18
+ lock_manager = Redlock::Client.new([ENV['REDIS_URI']])
11
19
  scheduler = Rufus::Scheduler.new
12
20
  scheduler.every '5m', :first_in => '5s', :overlap => false, :timeout => '5m' do
13
21
  begin
14
- if locked?
15
- puts "[cf_light_api:worker] Data update is already running in another worker, skipping..."
16
- next
17
- end
18
-
19
- lock
20
- start_time = Time.now
22
+ lock_manager.lock("#{ENV['REDIS_KEY_PREFIX']}:lock", 5*60*1000) do |lock|
23
+ if lock
24
+ start_time = Time.now
21
25
 
22
- puts "[cf_light_api:worker] Updating data..."
26
+ @logger.info "Updating data..."
23
27
 
24
- cf_client = get_client()
28
+ cf_client = get_client()
25
29
 
26
- org_data = []
27
- app_data = []
28
- org_data = Parallel.map( cf_client.organizations, :in_processes => 4) do |org|
29
- # The CFoundry client returns memory_limit in MB, so we need to normalise to Bytes to match the Apps.
30
- {
31
- :name => org.name,
32
- :quota => {
33
- :total_services => org.quota_definition.total_services,
34
- :memory_limit => org.quota_definition.memory_limit * 1024 * 1024
35
- }
36
- }
37
- end.flatten
38
-
39
- app_data = Parallel.map(cf_client.organizations, :in_processes => 4) do |org|
40
- Parallel.map(org.spaces, :in_processes => 4) do |space|
41
- Parallel.map(space.apps, :in_processes => 4) do |app|
42
- begin
43
- # It's possible for an app to have been terminated before this stage is reached.
44
- format_app_data(app, org.name, space.name)
45
- rescue CFoundry::AppNotFound
46
- next
47
- end
48
- end
49
- end
50
- end.flatten
30
+ org_data = get_org_data(cf_client)
31
+ app_data = get_app_data(cf_client)
51
32
 
52
- unlock
53
-
54
- put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:orgs", org_data
55
- put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:apps", app_data
56
-
57
- puts "[cf_light_api:worker] Update completed in #{format_duration(Time.now.to_f - start_time.to_f)}..."
33
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:orgs", org_data
34
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:apps", app_data
58
35
 
36
+ @logger.info "Update completed in #{format_duration(Time.now.to_f - start_time.to_f)}..."
37
+ lock_manager.unlock(lock)
38
+ cf_client.logout
39
+ else
40
+ @logger.info "Update already running in another thread!"
41
+ end
42
+ end
59
43
  rescue Rufus::Scheduler::TimeoutError
60
- puts '[cf_light_api:worker] Data update took too long and was aborted...'
44
+ @logger.info 'Data update took too long and was aborted...'
45
+ cf_client.logout
61
46
  end
62
47
  end
63
48
 
64
- def locked?
65
- REDIS.get("#{ENV['REDIS_KEY_PREFIX']}:lock") ? true : false
66
- end
67
-
68
- def lock
69
- REDIS.set "#{ENV['REDIS_KEY_PREFIX']}:lock", true
70
- REDIS.expire "#{ENV['REDIS_KEY_PREFIX']}:lock", 900
71
- end
72
-
73
- def unlock
74
- REDIS.del "#{ENV['REDIS_KEY_PREFIX']}:lock"
75
- end
76
-
77
49
  def get_client(cf_api=ENV['CF_API'], cf_user=ENV['CF_USER'], cf_password=ENV['CF_PASSWORD'])
78
50
  client = CFoundry::Client.get(cf_api)
79
51
  client.login({:username => cf_user, :password => cf_password})
80
52
  client
81
53
  end
82
54
 
55
+ def get_app_data(cf_client)
56
+ Parallel.map(cf_client.organizations, :in_processes => 4) do |org|
57
+ org_name = org.name
58
+ Parallel.map(org.spaces, :in_processes => 4) do |space|
59
+ space_name = space.name
60
+ @logger.info "Getting app data for apps in #{org_name}:#{space_name}..."
61
+ Parallel.map(space.apps, :in_processes => 4) do |app|
62
+ begin
63
+ # It's possible for an app to have been terminated before this stage is reached.
64
+ format_app_data(app, org_name, space_name)
65
+ rescue CFoundry::AppNotFound
66
+ next
67
+ end
68
+ end
69
+ end
70
+ end.flatten.compact
71
+ end
72
+
73
+ def get_org_data(cf_client)
74
+ Parallel.map( cf_client.organizations, :in_processes => 4) do |org|
75
+ org_name = org.name
76
+ @logger.info "Getting org data for #{org_name}..."
77
+ # The CFoundry client returns memory_limit in MB, so we need to normalise to Bytes to match the Apps.
78
+ {
79
+ :name => org_name,
80
+ :quota => {
81
+ :total_services => org.quota_definition.total_services,
82
+ :memory_limit => org.quota_definition.memory_limit * 1024 * 1024
83
+ }
84
+ }
85
+ end.flatten.compact
86
+ end
87
+
83
88
  def format_app_data(app, org_name, space_name)
84
89
  base_data = {
85
90
  :guid => app.guid,
@@ -99,7 +104,7 @@ def format_app_data(app, org_name, space_name)
99
104
  :error => nil
100
105
  }
101
106
  rescue => e
102
- puts "[cf_light_api:worker] #{org_name} #{space_name}: '#{app.name}'' error: #{e.message}"
107
+ @logger.info " #{org_name} #{space_name}: '#{app.name}'' error: #{e.message}"
103
108
  additional_data = {
104
109
  :running => 'error',
105
110
  :instances => [],
@@ -111,7 +116,6 @@ def format_app_data(app, org_name, space_name)
111
116
  end
112
117
 
113
118
  def put_in_redis(key, data)
114
- puts "[cf_light_api:worker] Putting data #{data} into redis key #{key}"
115
119
  REDIS.set key, data.to_json
116
120
  end
117
121
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cf_light_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Springer Platform Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-28 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cfoundry
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.4.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: redlock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.1
83
97
  description: A super lightweight API for reading App and Org data from CloudFoundry,
84
98
  cached in Redis.
85
99
  email: ''
@@ -88,9 +102,9 @@ executables:
88
102
  extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
- - ./lib/sinatra/cf_light_api.rb
92
105
  - ./lib/cf_light_api/redis.rb
93
106
  - ./lib/cf_light_api/worker.rb
107
+ - ./lib/sinatra/cf_light_api.rb
94
108
  - bin/cf_light_api
95
109
  homepage: https://github.com/springerpe/cf-light-api
96
110
  licenses:
@@ -112,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
126
  version: '0'
113
127
  requirements: []
114
128
  rubyforge_project:
115
- rubygems_version: 2.0.14
129
+ rubygems_version: 2.4.6
116
130
  signing_key:
117
131
  specification_version: 4
118
132
  summary: A super lightweight API for reading App and Org data from CloudFoundry, cached