flow_org_patron 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd7677e9ad2a183e2588a8446d96b8462714b070
4
+ data.tar.gz: cbc04a13a42a2e46ffc527c5475d4ec980d8c545
5
+ SHA512:
6
+ metadata.gz: 574992bf37c3a34bc5c3ea3f65091b652299c9bf14f6297f6bec6e19efac3cb3cd484404166e011d29eae83efc1284dbf524b188262521afc9924a26c764b2fd
7
+ data.tar.gz: 4e763b1830ca5e0078cdc1b24d6c5831599a8e059e78fa10e4328f51a5bca2794fdb18a39851d5b3a338c4b78f2e9fd300c20815724366ff1b8994fad808a35c
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .tags*
14
+ .byebug*
15
+ coverage
16
+ flow_node_patron-*.gem
17
+ flow_node_patron.log
18
+ .vscode
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,30 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-04-23 13:22:26 +0800 using RuboCop version 0.48.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 5
10
+ # Configuration parameters: CountComments, ExcludedMethods.
11
+ AllCops:
12
+ Exclude:
13
+ - 'spec/*'
14
+ Metrics/BlockLength:
15
+ Max: 40
16
+ Style/AsciiComments:
17
+ Enabled: false
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Metrics/LineLength:
22
+ Max: 120
23
+
24
+ Metrics/AbcSize:
25
+ Max: 20
26
+
27
+ # Offense count: 1
28
+ # Configuration parameters: CountComments.
29
+ Metrics/MethodLength:
30
+ Max: 40
@@ -0,0 +1,11 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-05-03 17:34:16 +0800 using RuboCop version 0.48.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Metrics/AbcSize:
11
+ Max: 20
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at atpking@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://gems.ruby-china.org'
2
+
3
+ gem 'simplecov', require: false, group: :test
4
+ # Specify your gem's dependencies in flow_node_patron.gemspec
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 atpking
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # FlowNodePatron
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/flow_node_patron`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'flow_node_patron'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install flow_node_patron
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flow_node_patron. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'flow_node_patron'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/flow_node_patron'
3
+
4
+ puts '初始化....'
5
+ puts '使用了以下配置'
6
+ puts '*' * 40
7
+ FlowNodePatron::Config.constants.each do |constant|
8
+ puts "#{constant} = #{FlowNodePatron::Config.const_get(constant)}"
9
+ end
10
+ puts '*' * 40
11
+
12
+ puts '这些配置都可以通过环境变量指定替换掉'
13
+
14
+
15
+ if File.file?('/tmp/clean_success.txt')
16
+ puts '检测到存在/tmp/clean_success.txt,清理脚本没有正确退出?输入 y 删除该脚本且继续执行,否则退出'
17
+ inputs = gets
18
+ if inputs.casecmp('y').zero?
19
+ File.delete('/tmp/clean_success.txt')
20
+ puts '退出。。。。。'
21
+ exit
22
+ end
23
+ end
24
+
25
+ # 指定日志
26
+ FlowNodePatron.logger.level = Logger.const_get(FlowNodePatron::Config::PATRON_LOG_LEVEL)
27
+ RestClient.log = FlowNodePatron.logger if FlowNodePatron::Config::PATRON_LOG_LEVEL == 'DEBUG'
28
+ FlowNodePatron::Server.new.init do
29
+ FlowNodePatron::FlowPromisedLandService.checkin
30
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'flow_node_patron/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'flow_org_patron'
9
+ spec.version = FlowNodePatron::VERSION
10
+ spec.authors = ['atpking']
11
+ spec.email = ['atpking@gmail.com']
12
+
13
+ spec.summary = 'flow org agent patron '
14
+ spec.description = 'flow org agent patron '
15
+ spec.homepage = 'https://www.flow.ci'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.14'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'awesome_print', '~> 1.7'
29
+ spec.add_development_dependency 'byebug'
30
+
31
+ spec.add_development_dependency 'rubocop'
32
+
33
+ spec.add_dependency 'byebug'
34
+
35
+ spec.add_dependency 'rest-client', '>= 2.0'
36
+ spec.add_dependency 'thor', '>= 0.19'
37
+ spec.add_dependency 'oj', '>= 2.18'
38
+ spec.add_dependency 'aasm', '>= 4.12'
39
+ end
@@ -0,0 +1,11 @@
1
+ require 'rest-client'
2
+ require 'thor'
3
+ require_relative './flow_node_patron/version'
4
+ require_relative './flow_node_patron/util'
5
+ require_relative './flow_node_patron/config'
6
+ require_relative './flow_node_patron/flow_promised_land_service'
7
+ require_relative './flow_node_patron/server'
8
+
9
+ module FlowNodePatron
10
+ include Utils::Logger
11
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ require 'socket'
2
+ require_relative './version'
3
+
4
+ module FlowNodePatron
5
+ module Config
6
+ PROMISED_LAND_ORG_PATRON_TOKEN = ENV['PROMISED_LAND_ORG_PATRON_TOKEN']
7
+
8
+ HTTP_PROTOCOL = ENV['HTTP_PROTOCOL'] || 'http'
9
+ PROMISED_LAND_URL = ENV['PROMISED_LAND_URL'] || "#{HTTP_PROTOCOL}://lyon.flow.ci:6690"
10
+
11
+ # 获取patron 的名称, 以后严格的时候不给名称不让启动,或者不匹配成功不让启动
12
+ PATRON_LOG_LEVEL = ENV['PATRON_LOG_LEVEL'] || 'INFO'
13
+ PATRON_LOOP_RUN_CHECK_SECONDS = ENV['PATRON_LOOP_RUN_CHECK_SECONDS'].nil? ? 5 : ENV['PATRON_LOOP_RUN_CHECK_SECONDS'].to_i
14
+ PATRON_AUTO_REPORT_SECONDS = ENV['PATRON_AUTO_REPORT_SECONDS'].nil? ? 60 : ENV['PATRON_AUTO_REPORT_SECONDS'].to_i
15
+
16
+ PATRON_LOCAL_IP = ENV['PATRON_LOCAL_IP'] ||
17
+ Socket.ip_address_list.map(&:ip_address).find { |p| p.start_with?('192', '172') }
18
+ PATRON_RUNNING_SCRIPT_FILE = ENV['PATRON_RUNNING_SCRIPT_URI'] || '~/patron_running_script.sh'
19
+ PATRON_CLEAN_SCRIPT_FILE = ENV['PATRON_CLEAN_SCRIPT_URI'] || '~/patron_clean_script.sh'
20
+ PATRON_RESET_SCRIPT_FILE = ENV['PATRON_RESET_SCRIPT_URI'] || '~/patron_reset_script.sh'
21
+ PATRON_VERSION = ::FlowNodePatron::VERSION
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module FlowNodePatron
2
+ class FlowPromisedLandRest < Utils::LocalServiceRest
3
+ class << self
4
+ def basic_url
5
+ Config::PROMISED_LAND_URL
6
+ end
7
+
8
+ def add_those_to_params
9
+ {
10
+ org_patron_token: Config::PROMISED_LAND_ORG_PATRON_TOKEN
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require_relative './flow_promised_land_rest'
2
+ require_relative './operations/flow_promised_land_box_operation'
3
+ require_relative './operations/flow_promised_land_patron_operation'
4
+ module FlowNodePatron
5
+ class FlowPromisedLandService
6
+ extend FlowPromisedLandBoxOperation
7
+ extend FlowPromisedLandPatronOperation
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../flow_promised_land_rest'
2
+
3
+ module FlowNodePatron
4
+ module FlowPromisedLandBoxOperation
5
+ # 去promised_land 拿出队的box info
6
+ def fetch_outqueue_box
7
+ answer = FlowPromisedLandRest.get "/#{specific_org_patrons_url}/fetch_outqueue_box", {}
8
+ return nil if answer.dig(:json, :queue) == 'empty'
9
+ answer
10
+ end
11
+
12
+ def get_box_info(box_id)
13
+ FlowPromisedLandRest.get "/#{specific_org_patrons_url}/get_box_info", box_id: box_id
14
+ end
15
+
16
+ def closed_box?(box_id)
17
+ answer = get_box_info(box_id)
18
+ answer[:status] == 'closed'
19
+ end
20
+
21
+ def delete_box(id)
22
+ FlowPromisedLandRest.post("/#{specific_org_patrons_url}/delete_box", box_id: id)
23
+ end
24
+
25
+ # 主要给测试用的函数,平时不要乱用
26
+ def push_a_test_box
27
+ FlowPromisedLandRest.post("/#{specific_org_patrons_url}/push_a_test_box", {})
28
+ end
29
+
30
+ def queue_list
31
+ FlowPromisedLandRest.get("/#{specific_org_patrons_url}/queue_box_list")
32
+ end
33
+
34
+ private
35
+
36
+ def specific_org_patrons_url
37
+ "org_patrons/#{::FlowNodePatron::Config::PROMISED_LAND_ORG_PATRON_TOKEN}"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ module FlowNodePatron
2
+ module FlowPromisedLandPatronOperation
3
+ def checkin
4
+ logger.info 'checkin ... '
5
+ FlowPromisedLandRest.post '/org_patrons/checkin',
6
+ org_patron_token: ::FlowNodePatron::Config::PROMISED_LAND_ORG_PATRON_TOKEN,
7
+ version: ::FlowNodePatron::Config::PATRON_VERSION
8
+ end
9
+
10
+ def logout
11
+ logger.info 'logout ...'
12
+ FlowPromisedLandRest.post "/org_patrons/#{::FlowNodePatron::Config::PROMISED_LAND_ORG_PATRON_TOKEN}/logout", {}
13
+ end
14
+
15
+ def update_report(patron_running_status, service_box_id = nil)
16
+ logger.info "report patron_running_status #{patron_running_status} run box #{service_box_id}"
17
+ FlowPromisedLandRest.post "/org_patrons/#{::FlowNodePatron::Config::PROMISED_LAND_ORG_PATRON_TOKEN}/update_report",
18
+ patron_running_status: patron_running_status,
19
+ service_box_id: service_box_id
20
+ end
21
+
22
+ def show
23
+ FlowPromisedLandRest.get "/org_patrons/#{::FlowNodePatron::Config::PROMISED_LAND_ORG_PATRON_TOKEN}"
24
+ end
25
+
26
+ alias show_patron_info show
27
+
28
+ # DEBUG USE
29
+ def list
30
+ FlowPromisedLandRest.get '/org_patrons'
31
+ end
32
+
33
+ private
34
+
35
+ def logger
36
+ FlowNodePatron.logger
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,102 @@
1
+ require 'aasm'
2
+ require 'thread'
3
+ require_relative './flow_promised_land_service'
4
+ require_relative './server_thread'
5
+ STDOUT.sync
6
+
7
+ module FlowNodePatron
8
+ class Server
9
+ include AASM
10
+ include ServerThread
11
+ attr_accessor :current_box_id
12
+
13
+ aasm do
14
+ state :idle, initial: true
15
+ state :running
16
+ state :cleaning
17
+
18
+ after_all_transitions :report_message
19
+
20
+ event :run do
21
+ transitions from: :idle, to: :running
22
+ end
23
+
24
+ event :clean do
25
+ transitions from: :running, to: :cleaning
26
+ end
27
+
28
+ event :reset do
29
+ transitions from: :cleaning, to: :idle
30
+ after do
31
+ self.current_box_id = nil
32
+ end
33
+ end
34
+
35
+ event :force_reset do
36
+ transitions to: :idle
37
+ after do
38
+ self.current_box_id = nil
39
+ File.delete('/tmp/clean_success.txt') if File.file?('/tmp/clean_success.txt')
40
+ end
41
+ end
42
+ end
43
+
44
+ def report_message
45
+ FlowPromisedLandService.update_report(aasm.to_state, current_box_id)
46
+ end
47
+
48
+ def run_box_check
49
+ # 当有执行的时候 直接返回
50
+ return false unless may_run?
51
+
52
+ answer = FlowPromisedLandService.fetch_outqueue_box
53
+ return false if answer.nil? || answer[:id].nil? # 没拿到值
54
+ self.current_box_id = answer[:id]
55
+ run
56
+ if block_given?
57
+ yield(answer)
58
+ else
59
+ puts "拿到了出队的 box 的值#{answer}"
60
+ true
61
+ end
62
+ end
63
+
64
+ def clean_box_check
65
+ return false unless may_clean?
66
+ box_info = FlowPromisedLandService.get_box_info(current_box_id)
67
+ return false unless box_info[:status] == 'closed'
68
+ clean
69
+ if block_given?
70
+ yield box_info
71
+ else
72
+ puts '检测到了 box 关闭,patron 开始运行打扫程序...'
73
+ true
74
+ end
75
+ end
76
+
77
+ def reset_check
78
+ tmp_id = current_box_id
79
+ return false unless may_reset?
80
+
81
+ while !File.file?('/tmp/clean_success.txt') && ENV['DEBUG'].nil?
82
+ puts '没找到 /tmp/clean_success.txt 代表清理脚本还没执行完, sleep 5'
83
+ sleep 5
84
+ end
85
+ puts 'clean 脚本执行完毕'
86
+ File.delete('/tmp/clean_success.txt') if File.file?('/tmp/clean_success.txt')
87
+ reset
88
+ self.current_box_id = nil
89
+ if block_given?
90
+ yield tmp_id
91
+ else
92
+ puts '检测到了reset 。。。。'
93
+ true
94
+ end
95
+ end
96
+
97
+ def report_server_state
98
+ puts "#{Time.now}: #{aasm_read_state}, current_box_id = #{current_box_id}"
99
+ FlowPromisedLandService.update_report(aasm_read_state, current_box_id)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,90 @@
1
+ module FlowNodePatron
2
+ module ServerThread
3
+ def init
4
+ yield if block_given?
5
+ threads = []
6
+ threads << build_run_box_check_thread
7
+ threads << build_clean_box_check_thread
8
+ threads << build_report_thread
9
+
10
+ begin
11
+ threads.each(&:join)
12
+ rescue Interrupt => _e
13
+ FlowPromisedLandService.logout
14
+ logger.warn 'user Interrupt, logout'
15
+ puts '退出...'
16
+ end
17
+ end
18
+
19
+ def logger
20
+ ::FlowNodePatron.logger
21
+ end
22
+
23
+ private
24
+
25
+ def build_report_thread
26
+ Thread.new do
27
+ loop_run(:report_server_state, ::FlowNodePatron::Config::PATRON_AUTO_REPORT_SECONDS)
28
+ end
29
+ end
30
+
31
+ def build_clean_box_check_thread
32
+ my_proc = proc do
33
+ begin
34
+ loop_run(:clean_box_check) do |box|
35
+ puts '检测到了关闭box, 运行清道夫。。。。'
36
+ logger.debug '检测到了关闭box, 运行清道夫。。。。'
37
+ system("bash #{::FlowNodePatron::Config::PATRON_CLEAN_SCRIPT_FILE} \
38
+ #{box[:id]} #{box[:job_id]} #{box[:job_hash]} #{box[:flow_id]} #{box[:project_id]} #{box[:org_id]} &")
39
+ reset_check do
40
+ system("bash #{::FlowNodePatron::Config::PATRON_RESET_SCRIPT_FILE} &")
41
+ end
42
+ end
43
+ rescue => e
44
+ puts "e #{e.message}"
45
+ logger.error "e #{e.message}"
46
+ end
47
+ end
48
+ if ENV['LOOP_RUN_DEBUG']
49
+ my_proc.call
50
+ else
51
+ Thread.new(&my_proc)
52
+ end
53
+ end
54
+
55
+ def build_run_box_check_thread
56
+ my_proc = proc do
57
+ loop_run(:run_box_check) do |box|
58
+ puts "进来了一个 box ,box id = #{box[:id]}"
59
+ logger.debug "进来了一个 box ,box id = #{box[:id]}"
60
+ system("bash #{::FlowNodePatron::Config::PATRON_RUNNING_SCRIPT_FILE} \
61
+ #{box[:id]} #{box[:job_id]} #{box[:job_hash]} #{box[:flow_id]} #{box[:project_id]} #{box[:org_id]} &")
62
+ end
63
+ end
64
+ if ENV['LOOP_RUN_DEBUG']
65
+ my_proc.call
66
+ else
67
+ Thread.new(&my_proc)
68
+ end
69
+ end
70
+
71
+ def loop_run(method_name, sleep_time = ::FlowNodePatron::Config::PATRON_LOOP_RUN_CHECK_SECONDS, &hook)
72
+ if sleep_time <= 0
73
+ logger.warn "method #{method_name} wants to loop run less sleeping 0 second, cancel it."
74
+ return
75
+ end
76
+ loop do
77
+ begin
78
+ send method_name, &hook
79
+ rescue => e
80
+ logger.error "e #{e.class} #{e.message}"
81
+ logger.error e.backtrace.join("\n")
82
+ raise e if ENV['DEBUG']
83
+ STDERR.puts e.backtrace.join("\n")
84
+ end
85
+ sleep sleep_time
86
+ break if ENV['LOOP_RUN_DEBUG']
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,4 @@
1
+ require_relative './utils/concern'
2
+ require_relative './utils/logger'
3
+ require_relative './utils/hash'
4
+ require_relative './utils/local_service_rest'
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+
3
+ module ActiveSupport
4
+ # A typical module looks like this:
5
+ #
6
+ # module M
7
+ # def self.included(base)
8
+ # base.extend ClassMethods
9
+ # base.class_eval do
10
+ # scope :disabled, -> { where(disabled: true) }
11
+ # end
12
+ # end
13
+ #
14
+ # module ClassMethods
15
+ # ...
16
+ # end
17
+ # end
18
+ #
19
+ # By using <tt>ActiveSupport::Concern</tt> the above module could instead be
20
+ # written as:
21
+ #
22
+ # require 'active_support/concern'
23
+ #
24
+ # module M
25
+ # extend ActiveSupport::Concern
26
+ #
27
+ # included do
28
+ # scope :disabled, -> { where(disabled: true) }
29
+ # end
30
+ #
31
+ # class_methods do
32
+ # ...
33
+ # end
34
+ # end
35
+ #
36
+ # Moreover, it gracefully handles module dependencies. Given a +Foo+ module
37
+ # and a +Bar+ module which depends on the former, we would typically write the
38
+ # following:
39
+ #
40
+ # module Foo
41
+ # def self.included(base)
42
+ # base.class_eval do
43
+ # def self.method_injected_by_foo
44
+ # ...
45
+ # end
46
+ # end
47
+ # end
48
+ # end
49
+ #
50
+ # module Bar
51
+ # def self.included(base)
52
+ # base.method_injected_by_foo
53
+ # end
54
+ # end
55
+ #
56
+ # class Host
57
+ # include Foo # We need to include this dependency for Bar
58
+ # include Bar # Bar is the module that Host really needs
59
+ # end
60
+ #
61
+ # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We
62
+ # could try to hide these from +Host+ directly including +Foo+ in +Bar+:
63
+ #
64
+ # module Bar
65
+ # include Foo
66
+ # def self.included(base)
67
+ # base.method_injected_by_foo
68
+ # end
69
+ # end
70
+ #
71
+ # class Host
72
+ # include Bar
73
+ # end
74
+ #
75
+ # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt>
76
+ # is the +Bar+ module, not the +Host+ class. With <tt>ActiveSupport::Concern</tt>,
77
+ # module dependencies are properly resolved:
78
+ #
79
+ # require 'active_support/concern'
80
+ #
81
+ # module Foo
82
+ # extend ActiveSupport::Concern
83
+ # included do
84
+ # def self.method_injected_by_foo
85
+ # ...
86
+ # end
87
+ # end
88
+ # end
89
+ #
90
+ # module Bar
91
+ # extend ActiveSupport::Concern
92
+ # include Foo
93
+ #
94
+ # included do
95
+ # self.method_injected_by_foo
96
+ # end
97
+ # end
98
+ #
99
+ # class Host
100
+ # include Bar # It works, now Bar takes care of its dependencies
101
+ # end
102
+ module Concern
103
+ class MultipleIncludedBlocks < StandardError #:nodoc:
104
+ def initialize
105
+ super "Cannot define multiple 'included' blocks for a Concern"
106
+ end
107
+ end
108
+
109
+ def self.extended(base) #:nodoc:
110
+ base.instance_variable_set(:@_dependencies, [])
111
+ end
112
+
113
+ def append_features(base)
114
+ if base.instance_variable_defined?(:@_dependencies)
115
+ base.instance_variable_get(:@_dependencies) << self
116
+ false
117
+ else
118
+ return false if base < self
119
+ @_dependencies.each { |dep| base.send(:include, dep) }
120
+ super
121
+ base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
122
+ base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
123
+ end
124
+ end
125
+
126
+ def included(base = nil, &block)
127
+ if base.nil?
128
+ raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block)
129
+
130
+ @_included_block = block
131
+ else
132
+ super
133
+ end
134
+ end
135
+
136
+ def class_methods(&class_methods_module_definition)
137
+ mod = if const_defined?(:ClassMethods, false)
138
+ const_get(:ClassMethods)
139
+ else
140
+ const_set(:ClassMethods, Module.new)
141
+ end
142
+
143
+ mod.module_eval(&class_methods_module_definition)
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ class Hash
4
+ # Returns a copy of self with all blank keys removed.
5
+ #
6
+ # hash = { name: 'Rob', age: '', title: nil }
7
+ #
8
+ # hash.compact
9
+ # # => { name: 'Rob' }
10
+ def compact
11
+ delete_if { |_, v| v.is_a?(FalseClass) ? false : v.blank? }
12
+ end
13
+
14
+ # Returns a new hash with all keys converted using the block operation.
15
+ #
16
+ # hash = { name: 'Rob', age: '28' }
17
+ #
18
+ # hash.transform_keys{ |key| key.to_s.upcase }
19
+ # # => {"NAME"=>"Rob", "AGE"=>"28"}
20
+ def transform_keys
21
+ return enum_for(:transform_keys) unless block_given?
22
+ result = self.class.new
23
+ each_key do |key|
24
+ result[yield(key)] = self[key]
25
+ end
26
+ result
27
+ end
28
+
29
+ # Returns a new hash with all keys converted to symbols, as long as
30
+ # they respond to +to_sym+.
31
+ #
32
+ # hash = { 'name' => 'Rob', 'age' => '28' }
33
+ #
34
+ # hash.symbolize_keys
35
+ # # => {:name=>"Rob", :age=>"28"}
36
+ def symbolize_keys
37
+ transform_keys do |key|
38
+ begin
39
+ key.to_sym
40
+ rescue
41
+ key
42
+ end
43
+ end
44
+ end
45
+
46
+ # Returns a new hash with all keys converted by the block operation.
47
+ # This includes the keys from the root hash and from all
48
+ # nested hashes and arrays.
49
+ #
50
+ # hash = { person: { name: 'Rob', age: '28' } }
51
+ #
52
+ # hash.deep_transform_keys{ |key| key.to_s.upcase }
53
+ # # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
54
+ def deep_transform_keys(&block)
55
+ _deep_transform_keys_in_object(self, &block)
56
+ end
57
+
58
+ # Returns a new hash with all keys converted to symbols, as long as
59
+ # they respond to +to_sym+. This includes the keys from the root hash
60
+ # and from all nested hashes and arrays.
61
+ #
62
+ # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
63
+ #
64
+ # hash.deep_symbolize_keys
65
+ # # => {:person=>{:name=>"Rob", :age=>"28"}}
66
+ def deep_symbolize_keys
67
+ deep_transform_keys do |key|
68
+ begin
69
+ key.to_sym
70
+ rescue
71
+ key
72
+ end
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ # support methods for deep transforming nested hashes and arrays
79
+ def _deep_transform_keys_in_object(object, &block)
80
+ case object
81
+ when Hash
82
+ object.each_with_object({}) do |(key, value), result|
83
+ result[yield(key)] = _deep_transform_keys_in_object(value, &block)
84
+ end
85
+ when Array
86
+ object.map { |e| _deep_transform_keys_in_object(e, &block) }
87
+ else
88
+ object
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,108 @@
1
+ require 'oj'
2
+ require 'json'
3
+ require 'rest-client'
4
+
5
+ module FlowNodePatron
6
+ module Utils
7
+ class LocalServiceRest
8
+ class Error < StandardError; end
9
+ class JSONParseError < Error; end
10
+ class << self
11
+ %w[get delete head].each do |word|
12
+ define_method word.to_sym do |url, params = {}, timeout = 5, retry_times = 5|
13
+ params.merge!(add_those_to_params) # 将默认参数添加到params 里
14
+ url = basic_url + url
15
+ exception = nil
16
+ retry_times.times do
17
+ begin
18
+ headers = json_content_type
19
+ headers[:params] = if headers[:params]
20
+ headers[:params].merge(params)
21
+ else
22
+ params
23
+ end
24
+ response = ::RestClient::Request.execute(
25
+ method: word,
26
+ url: url,
27
+ headers: headers,
28
+ timeout: timeout
29
+ )
30
+ return ::Oj.load(response.body, symbol_keys: true)
31
+ rescue ::Oj::Error
32
+ raise ::LocalServiceRest::JSONParseError('返回的内容JSON解析失败')
33
+ rescue ::RestClient::ExceptionWithResponse => ex # 有返回的错误
34
+ raise ex unless ex.http_code.to_i < 500 && ex.http_code.to_i >= 300 # 这里主要是服务器返回了500 了,没必要解析body
35
+ return { # 这里是服务器有返回的错误,都应该是json 格式的
36
+ status: false,
37
+ json: ::Oj.load(ex.response.body, symbol_keys: true),
38
+ response_body: ex.response.body,
39
+ message: ex.message
40
+ }
41
+ rescue RestClient::Exception => e
42
+ exception = e
43
+ next
44
+ end
45
+ end
46
+ raise exception
47
+ end
48
+ end
49
+
50
+ %w[post patch put].each do |word|
51
+ define_method word.to_sym do |url, payload, timeout = 5, retry_times = 5|
52
+ payload = add_something_to_payload(payload)
53
+ url = build_valid_url(url)
54
+ exception = nil
55
+ retry_times.times do
56
+ begin
57
+ request = ::RestClient::Request.execute(
58
+ method: word,
59
+ url: url,
60
+ payload: payload.to_json,
61
+ headers: json_content_type,
62
+ timeout: timeout
63
+ )
64
+ return ::Oj.load(request.body, symbol_keys: true)
65
+ rescue ::Oj::Error
66
+ raise ::LocalServiceRest::JSONParseError('返回的内容JSON解析失败')
67
+ rescue ::RestClient::ExceptionWithResponse => ex # 有返回的错误
68
+ raise ex unless ex.http_code < 500 && ex.http_code >= 300
69
+ return {
70
+ status: false,
71
+ json: ::Oj.load(ex.response.body, symbol_keys: true),
72
+ response_body: ex.response.body,
73
+ message: ex.message
74
+ }
75
+ rescue RestClient::Exception => e
76
+ exception = e
77
+ next
78
+ end
79
+ end
80
+ raise exception
81
+ end
82
+ end
83
+
84
+ def build_valid_url(url)
85
+ add_params = URI.escape(add_those_to_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
86
+ tmp = url.include?('?') ? '&' : '?'
87
+ basic_url + url + tmp + add_params
88
+ end
89
+
90
+ def add_something_to_payload(payload)
91
+ payload.merge(add_those_to_params)
92
+ end
93
+
94
+ def json_content_type
95
+ { content_type: :json, accept: :json }
96
+ end
97
+
98
+ def basic_url
99
+ 'http://www.example.com' # 子类中复写
100
+ end
101
+
102
+ def add_those_to_params
103
+ {} # 子类中复写
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,27 @@
1
+ module FlowNodePatron
2
+ module Utils
3
+ module Logger
4
+ extend ActiveSupport::Concern
5
+ class_methods do
6
+ def logger
7
+ return @log unless @log.nil?
8
+ create_log
9
+ end
10
+
11
+ def create_log(param = nil)
12
+ @log = case param
13
+ when 'stdout'
14
+ ::Logger.new IO::STDOUT
15
+ when 'stderr'
16
+ ::Logger.new IO::STDERR
17
+ else
18
+ file = File.new('flow_node_patron.log', 'a')
19
+ file.sync = true
20
+ logger = ::Logger.new file
21
+ logger
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module FlowNodePatron
2
+ VERSION = '0.1.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flow_org_patron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - atpking
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rest-client
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '2.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '2.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: thor
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0.19'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0.19'
139
+ - !ruby/object:Gem::Dependency
140
+ name: oj
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '2.18'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '2.18'
153
+ - !ruby/object:Gem::Dependency
154
+ name: aasm
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '4.12'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '4.12'
167
+ description: 'flow org agent patron '
168
+ email:
169
+ - atpking@gmail.com
170
+ executables:
171
+ - flow_patron
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".rubocop.yml"
178
+ - ".rubocop_todo.yml"
179
+ - ".travis.yml"
180
+ - CODE_OF_CONDUCT.md
181
+ - Gemfile
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - bin/console
186
+ - bin/setup
187
+ - exe/flow_patron
188
+ - flow_node_patron.gemspec
189
+ - lib/flow_node_patron.rb
190
+ - lib/flow_node_patron/cli.rb
191
+ - lib/flow_node_patron/config.rb
192
+ - lib/flow_node_patron/flow_promised_land_rest.rb
193
+ - lib/flow_node_patron/flow_promised_land_service.rb
194
+ - lib/flow_node_patron/operations/flow_promised_land_box_operation.rb
195
+ - lib/flow_node_patron/operations/flow_promised_land_patron_operation.rb
196
+ - lib/flow_node_patron/server.rb
197
+ - lib/flow_node_patron/server_thread.rb
198
+ - lib/flow_node_patron/util.rb
199
+ - lib/flow_node_patron/utils/concern.rb
200
+ - lib/flow_node_patron/utils/hash.rb
201
+ - lib/flow_node_patron/utils/local_service_rest.rb
202
+ - lib/flow_node_patron/utils/logger.rb
203
+ - lib/flow_node_patron/version.rb
204
+ homepage: https://www.flow.ci
205
+ licenses:
206
+ - MIT
207
+ metadata: {}
208
+ post_install_message:
209
+ rdoc_options: []
210
+ require_paths:
211
+ - lib
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ requirements: []
223
+ rubyforge_project:
224
+ rubygems_version: 2.6.12
225
+ signing_key:
226
+ specification_version: 4
227
+ summary: flow org agent patron
228
+ test_files: []