cf_light_api 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cf_light_api/worker.rb +48 -21
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6171776702205a88ec76125790efdcd5d2eac90b
4
- data.tar.gz: 54d28ca42d4d29077920fedd93fec0f8e0b1d28e
3
+ metadata.gz: 417316fd53dc02ae75d5173f4476810f1d859e20
4
+ data.tar.gz: 2352be1e3ef38ca27cfe76ce335d8da94f242976
5
5
  SHA512:
6
- metadata.gz: db84dbc5f76f63ad96823bb823c2b23549e8943e1e72315b603fccb9b65783566d2082be1d0907d76f6d9bc11e5a8694f34ed4255699656b47293a6efa1237f4
7
- data.tar.gz: ae1ad0da601d7365a393ce04d4263a25c42220f2ccea6329dcd1940bdc8e9bc86a1ef9185be8d9e47b038f15cf16dc178181e545e1726a936da552150046f474
6
+ metadata.gz: e140e367cfe2ca04c18fca383102e80fd26c54255da38477703157dd360ad444d58401b138bdb3150c0719b87b0cac7247fd1bd02e8cdbdaef4b645daf19809f
7
+ data.tar.gz: 2e253d06021d0b874d5d150d2e76d2b775b11ec9eef3cf3d0de38aa05527f62035a12c106af07e1a1fa5a40558f61b18df7f92a6a4be02f425519a63008fb643
@@ -8,34 +8,61 @@ require 'rufus-scheduler'
8
8
  end
9
9
 
10
10
  scheduler = Rufus::Scheduler.new
11
- scheduler.every '5m', :first_in => '5s', :overlap => false do
11
+ scheduler.every '5m', :first_in => '5s', :overlap => false, :timeout => '15m' do
12
+ begin
13
+ if locked?
14
+ puts "[cf_light_api:worker] Data update is already running in another worker, skipping..."
15
+ next
16
+ end
12
17
 
13
- puts "[cf_light_api:worker] Updating data..."
18
+ lock
14
19
 
15
- cf_client = get_client()
20
+ puts "[cf_light_api:worker] Updating data..."
16
21
 
17
- org_data = []
18
- app_data = []
19
- cf_client.organizations.each do |org|
20
- # The CFoundry client returns memory_limit in MB, so we need to normalise to Bytes to match the Apps.
22
+ cf_client = get_client()
21
23
 
22
- org_data << {
23
- :name => org.name,
24
- :quota => {
25
- :total_services => org.quota_definition.total_services,
26
- :memory_limit => org.quota_definition.memory_limit * 1024 * 1024
24
+ org_data = []
25
+ app_data = []
26
+ cf_client.organizations.each do |org|
27
+ # The CFoundry client returns memory_limit in MB, so we need to normalise to Bytes to match the Apps.
28
+ org_data << {
29
+ :name => org.name,
30
+ :quota => {
31
+ :total_services => org.quota_definition.total_services,
32
+ :memory_limit => org.quota_definition.memory_limit * 1024 * 1024
33
+ }
27
34
  }
28
- }
29
35
 
30
- org.spaces.each do |space|
31
- space.apps.each do |app|
32
- app_data << format_app_data(app, org.name, space.name)
36
+ org.spaces.each do |space|
37
+ puts "processing space #{space.name}..."
38
+ space.apps.each do |app|
39
+ puts "processing app #{app.name}..."
40
+ app_data << format_app_data(app, org.name, space.name)
41
+ end
33
42
  end
34
43
  end
44
+
45
+ unlock
46
+
47
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:orgs", org_data
48
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:apps", app_data
49
+
50
+ rescue Rufus::Scheduler::TimeoutError
51
+ puts '[cf_light_api:worker] Data update took too long and was aborted...'
35
52
  end
36
- put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:orgs", org_data
37
- put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:apps", app_data
53
+ end
54
+
55
+ def locked?
56
+ REDIS.get("#{ENV['REDIS_KEY_PREFIX']}:lock") ? true : false
57
+ end
58
+
59
+ def lock
60
+ REDIS.set "#{ENV['REDIS_KEY_PREFIX']}:lock", true
61
+ REDIS.expire "#{ENV['REDIS_KEY_PREFIX']}:lock", 900
62
+ end
38
63
 
64
+ def unlock
65
+ REDIS.del "#{ENV['REDIS_KEY_PREFIX']}:lock"
39
66
  end
40
67
 
41
68
  def get_client(cf_api=ENV['CF_API'], cf_user=ENV['CF_USER'], cf_password=ENV['CF_PASSWORD'])
@@ -57,9 +84,9 @@ def format_app_data(app, org_name, space_name)
57
84
  additional_data = {}
58
85
  begin
59
86
  additional_data = {
60
- :running => app.running?,
61
- :instances => app.running? ? app.stats.map{|key, value| value} : [],
62
- :error => nil
87
+ :running => app.running?,
88
+ :instances => app.running? ? app.stats.map{|key, value| value} : [],
89
+ :error => nil
63
90
  }
64
91
  rescue => e
65
92
  puts "[cf_light_api:worker] #{org_name} #{space_name}: '#{app.name}'' error: #{e.message}"
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.0.0
4
+ version: 1.1.0
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-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cfoundry
@@ -78,7 +78,7 @@ files:
78
78
  - ./lib/cf_light_api/redis.rb
79
79
  - ./lib/cf_light_api/worker.rb
80
80
  - bin/cf_light_api
81
- homepage: http://springerpe.github.io
81
+ homepage: https://github.com/springerpe/cf-light-api
82
82
  licenses:
83
83
  - MIT
84
84
  metadata: {}