kyklos 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +121 -0
- data/Rakefile +10 -0
- data/bin/kyklos +26 -0
- data/examples/simple.rb +13 -0
- data/kyklos.gemspec +27 -0
- data/lib/kyklos.rb +17 -0
- data/lib/kyklos/adapters.rb +7 -0
- data/lib/kyklos/adapters/base_adapter.rb +21 -0
- data/lib/kyklos/adapters/shoryuken_adapter.rb +181 -0
- data/lib/kyklos/cli.rb +122 -0
- data/lib/kyklos/job.rb +8 -0
- data/lib/kyklos/job/base.rb +24 -0
- data/lib/kyklos/job/cron.rb +8 -0
- data/lib/kyklos/job/rate.rb +8 -0
- data/lib/kyklos/job_list.rb +51 -0
- data/lib/kyklos/version.rb +4 -0
- data/test/test_helper.rb +12 -0
- data/test/unit/kyklos/cli_test.rb +32 -0
- data/test/unit/kyklos/job/base_test.rb +15 -0
- data/test/unit/kyklos/job_list_test.rb +78 -0
- data/test/unit/kyklos/task_test.rb +6 -0
- data/test/unit/kyklos_test.rb +35 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f870d6a2b3a6d353b324b572bcd4c22e708eb711
|
4
|
+
data.tar.gz: 453611e97f7d6dc3c03b33ee1813c3677088d782
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 319d5f62e36281a8697b43b3bd7e3d77c069a68f5c56d910bd370ee261fb0f1c7bcc6d23eceec5dd554761b6a28ed1909695c7f37694326d00294c4646773ee7
|
7
|
+
data.tar.gz: ba617a19340a3ced8079ddfe63f11647d4f432026d5732fabc3d793100e284647429f8fad9a236821c7084b8b1056b8c17c5bceedb2ece359402b44cde89b424
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 TODO: Write your email address. 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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yuichi Takeuchi
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# Kyklos
|
2
|
+
|
3
|
+
You can use the Amazon CloudWatch Events to schedule jobs on Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'kyklos'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
$ gem install kyklos
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### 1. Write your config/schedule.rb
|
28
|
+
|
29
|
+
```sh
|
30
|
+
$ vi config/schedule.rb
|
31
|
+
```
|
32
|
+
|
33
|
+
#### Example schedule.rb file
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
cron '0/10 * * * ? *' do
|
37
|
+
Entry.publish_all
|
38
|
+
Entry.unpublish_all
|
39
|
+
end
|
40
|
+
|
41
|
+
rate '10 minutes' do
|
42
|
+
Temporary.destroy_all
|
43
|
+
end
|
44
|
+
|
45
|
+
rate '1 day', as: :recontraction do
|
46
|
+
Subscription.recontract_all
|
47
|
+
end
|
48
|
+
|
49
|
+
rate '1 day', as: :journalize_events do
|
50
|
+
Event.journalize
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
### 2. Deploy
|
55
|
+
|
56
|
+
```sh
|
57
|
+
bundle exec ruby bin/kyklos -c config/schedule.rb --adapter shoryuken --adapter_args=https://sqs.ap-northeast-1.amazonaws.com/accountid/queue_name
|
58
|
+
```
|
59
|
+
|
60
|
+
#### AWS認証情報について
|
61
|
+
|
62
|
+
##### 環境変数として渡す
|
63
|
+
|
64
|
+
```sh
|
65
|
+
$ AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxx \
|
66
|
+
> AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
|
67
|
+
> AWS_REGION=ap-northeast-1 \
|
68
|
+
> kyklos -c config/schedule.rb
|
69
|
+
```
|
70
|
+
|
71
|
+
##### `aws configure`を使う
|
72
|
+
|
73
|
+
```sh
|
74
|
+
$ aws configure
|
75
|
+
AWS Access Key ID [None]: xxxxxxxxxxxxxxxxxxxx
|
76
|
+
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
77
|
+
Default region name [None]: ap-northeast-1
|
78
|
+
Default output format [None]: json
|
79
|
+
$ ls ~/.aws
|
80
|
+
config credentials
|
81
|
+
$ kyklos -c config/schedule.rb
|
82
|
+
```
|
83
|
+
|
84
|
+
### 3. Shoryuken
|
85
|
+
|
86
|
+
#### 設定
|
87
|
+
|
88
|
+
```sh
|
89
|
+
$ vi kyklos_worker.rb
|
90
|
+
```
|
91
|
+
```ruby
|
92
|
+
require 'kyklos'
|
93
|
+
Kyklos::Adapters::ShoryukenAdapter::Worker.config_path = 'config/schedule.rb'
|
94
|
+
Shoryuken.register_worker 'queue_name', Kyklos::Adapters::ShoryukenAdapter::Worker
|
95
|
+
```
|
96
|
+
|
97
|
+
```sh
|
98
|
+
$ vi shoryuken.yml
|
99
|
+
```
|
100
|
+
```yaml
|
101
|
+
concurrency: 25
|
102
|
+
delay: 25
|
103
|
+
queues:
|
104
|
+
- [queue_name, 1]
|
105
|
+
```
|
106
|
+
|
107
|
+
#### ワーカーを開始
|
108
|
+
|
109
|
+
```sh
|
110
|
+
$ bundle exec shoryuken -r ./kyklos_worker.rb -C shoryuken.yml
|
111
|
+
```
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/takeyuweb/kyklos. 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.
|
116
|
+
|
117
|
+
|
118
|
+
## License
|
119
|
+
|
120
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
121
|
+
|
data/Rakefile
ADDED
data/bin/kyklos
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'optparse'
|
5
|
+
require 'kyklos'
|
6
|
+
require 'kyklos/version'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opt|
|
10
|
+
opt.version = Kyklos::VERSION
|
11
|
+
opt.on('-c', '--config CONFIG') do |config|
|
12
|
+
options[:config_path] = config
|
13
|
+
end
|
14
|
+
opt.on('--adapter ADAPTER_NAME') do |adapter|
|
15
|
+
options[:adapter] = adapter
|
16
|
+
end
|
17
|
+
opt.on('--adapter_args arg1,args,arg3...', Array) do |adapter_args|
|
18
|
+
options[:adapter_args] = adapter_args
|
19
|
+
end
|
20
|
+
opt.on('--identifier IDENTIFIER') do |identifier|
|
21
|
+
options[:identifier] = identifier
|
22
|
+
end
|
23
|
+
opt.parse!(ARGV)
|
24
|
+
end
|
25
|
+
|
26
|
+
Kyklos::CLI.new(options).deploy
|
data/examples/simple.rb
ADDED
data/kyklos.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kyklos/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'kyklos'
|
8
|
+
spec.version = Kyklos::VERSION
|
9
|
+
spec.authors = ['Yuichi Takeuchi']
|
10
|
+
spec.email = ['info@takeyu-web.com']
|
11
|
+
|
12
|
+
spec.summary = %q{You can use the Amazon CloudWatch Events to schedule jobs on Ruby.}
|
13
|
+
spec.homepage = 'https://github.com/takeyuweb/kyklos/'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split("\n")
|
17
|
+
spec.test_files = `git ls-files -- test/unit/*`.split("\n")
|
18
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'aws-sdk'
|
22
|
+
spec.add_dependency 'shoryuken'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'test-unit', '~> 3.2'
|
27
|
+
end
|
data/lib/kyklos.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'kyklos/job_list'
|
3
|
+
require 'kyklos/job'
|
4
|
+
require 'kyklos/adapters'
|
5
|
+
require 'kyklos/adapters/shoryuken_adapter'
|
6
|
+
require 'kyklos/cli'
|
7
|
+
require 'kyklos/version'
|
8
|
+
|
9
|
+
module Kyklos
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
def parse(code)
|
14
|
+
Kyklos::JobList.new(code)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kyklos
|
4
|
+
module Adapters
|
5
|
+
class BaseAdapter
|
6
|
+
|
7
|
+
def initialize(*args)
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
def assign_cloudwatchevents(job_id:, rule:)
|
12
|
+
raise NotImplementedError
|
13
|
+
end
|
14
|
+
|
15
|
+
def unassign_cloudwatchevents(rule:)
|
16
|
+
raise NotImplementedError
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# $ vi kyklos_worker.rb
|
4
|
+
# require 'kyklos'
|
5
|
+
# Kyklos::Adapters::ShoryukenAdapter::Worker.config_path = 'config/schedule.rb'
|
6
|
+
# Shoryuken.register_worker 'queue_name', Kyklos::Adapters::ShoryukenAdapter::Worker
|
7
|
+
#
|
8
|
+
# $ vi shoryuken.yml
|
9
|
+
# concurrency: 25
|
10
|
+
# delay: 25
|
11
|
+
# queues:
|
12
|
+
# - [queue_name, 1]
|
13
|
+
#
|
14
|
+
# $ bundle exec shoryuken -r ./kyklos_worker.rb -C shoryuken.yml
|
15
|
+
|
16
|
+
# TODO: Aws SDKのラッパークラスを書いてテストしやすくする
|
17
|
+
# TODO: ShoryukenAdapter をgemに分離
|
18
|
+
|
19
|
+
require 'json'
|
20
|
+
require 'aws-sdk'
|
21
|
+
require 'digest/md5'
|
22
|
+
require 'shoryuken'
|
23
|
+
|
24
|
+
module Kyklos
|
25
|
+
module Adapters
|
26
|
+
class ShoryukenAdapter < BaseAdapter
|
27
|
+
|
28
|
+
attr_reader :queue_url
|
29
|
+
|
30
|
+
def initialize(*args)
|
31
|
+
@queue_url = args[0]
|
32
|
+
end
|
33
|
+
|
34
|
+
def assign_cloudwatchevents(job_id:, rule:)
|
35
|
+
assign_queue_policy(job_id, rule.arn)
|
36
|
+
[
|
37
|
+
{
|
38
|
+
id: target_id(job_id),
|
39
|
+
arn: target_arn,
|
40
|
+
input: {
|
41
|
+
job_id: job_id
|
42
|
+
}.to_json
|
43
|
+
}
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
def unassign_cloudwatchevents(rule:)
|
48
|
+
unassign_queue_policy(rule.arn)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def target_id(job_id)
|
54
|
+
Digest::MD5.hexdigest(job_id.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def target_arn
|
58
|
+
resp = sqs.get_queue_attributes(
|
59
|
+
queue_url: @queue_url,
|
60
|
+
attribute_names: ['QueueArn']
|
61
|
+
)
|
62
|
+
resp.attributes['QueueArn']
|
63
|
+
end
|
64
|
+
|
65
|
+
def assign_queue_policy(job_id, rule_arn)
|
66
|
+
policy = get_queue_policy
|
67
|
+
new_statement = {
|
68
|
+
'Sid' => job_id.to_s,
|
69
|
+
'Effect' => 'Allow',
|
70
|
+
'Principal' => {
|
71
|
+
"AWS" => '*'
|
72
|
+
},
|
73
|
+
'Action' => 'sqs:SendMessage',
|
74
|
+
'Resource' => target_arn,
|
75
|
+
'Condition' => {
|
76
|
+
'ArnEquals' => {
|
77
|
+
'aws:SourceArn' => rule_arn
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
policy['Statement'] = add_statement(policy['Statement'], new_statement)
|
82
|
+
|
83
|
+
sqs.set_queue_attributes(
|
84
|
+
queue_url: queue_url,
|
85
|
+
attributes: {
|
86
|
+
'Policy' => policy.to_json,
|
87
|
+
},
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
def get_queue_policy
|
92
|
+
policy_str = sqs.get_queue_attributes(
|
93
|
+
queue_url: queue_url,
|
94
|
+
attribute_names: ['Policy']
|
95
|
+
).attributes['Policy']
|
96
|
+
|
97
|
+
policy = JSON.load(policy_str)
|
98
|
+
unless policy
|
99
|
+
policy = {
|
100
|
+
'Version' => '2012-10-17',
|
101
|
+
'Id' => "#{target_arn}/SQSDefaultPolicy",
|
102
|
+
'Statement'=> []
|
103
|
+
}
|
104
|
+
end
|
105
|
+
policy
|
106
|
+
end
|
107
|
+
|
108
|
+
def add_statement(statements, new_sttement)
|
109
|
+
index = statements.find_index{|statement| statement['Sid'] == new_sttement['Sid'] }
|
110
|
+
if index
|
111
|
+
statements[index] = new_sttement
|
112
|
+
else
|
113
|
+
statements.push(new_sttement)
|
114
|
+
end
|
115
|
+
statements
|
116
|
+
end
|
117
|
+
|
118
|
+
def unassign_queue_policy(rule_arn)
|
119
|
+
policy = get_queue_policy
|
120
|
+
policy['Statement'] = policy['Statement'].reject do |statement|
|
121
|
+
statement['Effect'] == 'Allow' &&
|
122
|
+
statement['Action'] == 'sqs:SendMessage' &&
|
123
|
+
statement['Resource'] == target_arn &&
|
124
|
+
statement['Condition'] &&
|
125
|
+
statement['Condition']['ArnEquals'] &&
|
126
|
+
statement['Condition']['ArnEquals']['aws:SourceArn'] == rule_arn
|
127
|
+
end
|
128
|
+
|
129
|
+
if policy['Statement'].empty?
|
130
|
+
sqs.set_queue_attributes(
|
131
|
+
queue_url: queue_url,
|
132
|
+
attributes: {
|
133
|
+
'Policy' => '',
|
134
|
+
},
|
135
|
+
)
|
136
|
+
else
|
137
|
+
sqs.set_queue_attributes(
|
138
|
+
queue_url: queue_url,
|
139
|
+
attributes: {
|
140
|
+
'Policy' => policy.to_json,
|
141
|
+
},
|
142
|
+
)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def sqs
|
147
|
+
Aws::SQS::Client.new
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
class Worker
|
152
|
+
include Shoryuken::Worker
|
153
|
+
shoryuken_options auto_delete: true
|
154
|
+
|
155
|
+
def self.config_path=(config_path)
|
156
|
+
@@config_path = config_path
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.config_path
|
160
|
+
@@config_path
|
161
|
+
end
|
162
|
+
|
163
|
+
def perform(sqs_msg, body)
|
164
|
+
data = JSON.load(body)
|
165
|
+
job_id =data['job_id']
|
166
|
+
if job_id
|
167
|
+
kyklos.run(job_id)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
def kyklos
|
174
|
+
Kyklos.parse(File.read(self.class.config_path))
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
data/lib/kyklos/cli.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws-sdk'
|
4
|
+
require 'digest/md5'
|
5
|
+
|
6
|
+
# TODO: Aws SDKのラッパークラスを書いてテストしやすくする
|
7
|
+
|
8
|
+
module Kyklos
|
9
|
+
class CLI
|
10
|
+
attr_reader :config_path, :job_list
|
11
|
+
|
12
|
+
def initialize(config_path:, adapter:, adapter_args: [], identifier: 'default')
|
13
|
+
@config_path = config_path.to_s
|
14
|
+
@adapter = adapter
|
15
|
+
@adapter_args = adapter_args
|
16
|
+
@identifier = identifier
|
17
|
+
@job_list = Kyklos.parse(File.read(config_path))
|
18
|
+
end
|
19
|
+
|
20
|
+
def deploy
|
21
|
+
cleanup
|
22
|
+
put_jobs
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def cloudwatchevents
|
28
|
+
@cloudwatchevents ||= Aws::CloudWatchEvents::Client.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def cleanup
|
32
|
+
names = eventnames
|
33
|
+
find_rules.each do |rule|
|
34
|
+
unless names.include?(rule.name)
|
35
|
+
remove_job(rule)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def put_jobs
|
41
|
+
job_list.each do |job_id, job|
|
42
|
+
put_job(job_id, job)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def put_job(job_id, job)
|
47
|
+
rule_params = {
|
48
|
+
name: eventname(job_id),
|
49
|
+
event_pattern: '',
|
50
|
+
state: 'DISABLED',
|
51
|
+
description: [@identifier, job.description].compact.join(': '),
|
52
|
+
schedule_expression: job.schedule_expression,
|
53
|
+
}
|
54
|
+
cloudwatchevents.put_rule(rule_params)
|
55
|
+
rule = cloudwatchevents.describe_rule(name: rule_params[:name])
|
56
|
+
targets = adapter.assign_cloudwatchevents(
|
57
|
+
job_id: job_id,
|
58
|
+
rule: rule)
|
59
|
+
cloudwatchevents.put_targets(
|
60
|
+
rule: rule.name,
|
61
|
+
targets: targets
|
62
|
+
)
|
63
|
+
cloudwatchevents.enable_rule(name: rule.name)
|
64
|
+
end
|
65
|
+
|
66
|
+
def remove_job(rule)
|
67
|
+
adapter.unassign_cloudwatchevents(rule: rule)
|
68
|
+
|
69
|
+
target_ids = find_targets(rule.name).map(&:id)
|
70
|
+
unless target_ids.empty?
|
71
|
+
cloudwatchevents.remove_targets(rule: rule.name, ids: target_ids)
|
72
|
+
end
|
73
|
+
cloudwatchevents.delete_rule(name: rule.name)
|
74
|
+
end
|
75
|
+
|
76
|
+
def prefix
|
77
|
+
"kyslos-#{"#{@identifier}/#{Digest::MD5.hexdigest(@identifier)}".sum}-"
|
78
|
+
end
|
79
|
+
|
80
|
+
def eventname(job_id)
|
81
|
+
"#{prefix}#{Digest::MD5.hexdigest(job_id.to_s)}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def eventnames
|
85
|
+
job_list.each.map { |job_id, _job| eventname(job_id) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def adapter
|
89
|
+
adapter_klass = Kyklos::Adapters.const_get("#{@adapter.capitalize}Adapter")
|
90
|
+
adapter_klass.new(*@adapter_args)
|
91
|
+
end
|
92
|
+
|
93
|
+
def find_rules
|
94
|
+
list_rules = ->(next_token) do
|
95
|
+
resp = cloudwatchevents.
|
96
|
+
list_rules(name_prefix: prefix,
|
97
|
+
next_token: next_token)
|
98
|
+
if resp.next_token
|
99
|
+
resp.rules + list_rules.call(next_token)
|
100
|
+
else
|
101
|
+
resp.rules
|
102
|
+
end
|
103
|
+
end
|
104
|
+
list_rules.call(nil)
|
105
|
+
end
|
106
|
+
|
107
|
+
def find_targets(rule_name)
|
108
|
+
list_targets = ->(next_token) do
|
109
|
+
resp = cloudwatchevents.
|
110
|
+
list_targets_by_rule(rule: rule_name,
|
111
|
+
next_token: next_token)
|
112
|
+
if resp.next_token
|
113
|
+
resp.targets + list_targets.call(next_token)
|
114
|
+
else
|
115
|
+
resp.targets
|
116
|
+
end
|
117
|
+
end
|
118
|
+
list_targets.call(nil)
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
data/lib/kyklos/job.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Kyklos
|
3
|
+
module Job
|
4
|
+
class Base
|
5
|
+
|
6
|
+
attr_reader :expression, :description
|
7
|
+
|
8
|
+
def initialize(expression, closure, description: nil)
|
9
|
+
@expression = expression
|
10
|
+
@description = description
|
11
|
+
@closure = closure
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
@closure.call
|
16
|
+
end
|
17
|
+
|
18
|
+
def schedule_expression
|
19
|
+
"#{self.class.name.split('::').last.downcase}(#{expression})"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Kyklos
|
3
|
+
class JobList
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :jobs
|
7
|
+
|
8
|
+
def initialize(code = nil)
|
9
|
+
@jobs = {}
|
10
|
+
instance_eval(code) unless code.nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](id)
|
14
|
+
@jobs[normalize_id(id)]
|
15
|
+
end
|
16
|
+
|
17
|
+
def each(&block)
|
18
|
+
@jobs.each(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def run(id)
|
22
|
+
self[id].run
|
23
|
+
end
|
24
|
+
|
25
|
+
def rate(expression, as: nil, desc: nil, &block)
|
26
|
+
add_job(Kyklos::Job::Rate, expression, block, as: as, desc: desc)
|
27
|
+
end
|
28
|
+
|
29
|
+
def cron(expression, as: nil, desc: nil, &block)
|
30
|
+
add_job(Kyklos::Job::Cron, expression, block, as: as, desc: desc)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def add_job(klass, expression, block, as:, desc:)
|
36
|
+
id = as || job_id(klass, expression)
|
37
|
+
normalized_id = normalize_id(id)
|
38
|
+
@jobs[normalized_id] = klass.new(expression, block, description: desc)
|
39
|
+
normalized_id
|
40
|
+
end
|
41
|
+
|
42
|
+
def job_id(klass, expression)
|
43
|
+
[klass, expression].map(&:to_s).join('#')
|
44
|
+
end
|
45
|
+
|
46
|
+
def normalize_id(id)
|
47
|
+
id.to_s.to_sym
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require 'kyklos'
|
4
|
+
|
5
|
+
require 'aws-sdk'
|
6
|
+
|
7
|
+
Aws.config.update(
|
8
|
+
region: 'us-west-2',
|
9
|
+
credentials: Aws::Credentials.new('DUMMY', 'DUMMY')
|
10
|
+
)
|
11
|
+
|
12
|
+
require 'test/unit'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
# TODO: リソース操作のテスト
|
6
|
+
|
7
|
+
class CLITest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
setup do
|
10
|
+
@config = Tempfile.open(%W(schedule .rb))
|
11
|
+
@config.write <<'CODE'
|
12
|
+
# Do nothing
|
13
|
+
CODE
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_initialize
|
17
|
+
assert_instance_of(Kyklos::CLI, Kyklos::CLI.new(config_path: @config.path, adapter: 'shoryuken'))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_deploy
|
21
|
+
cli = Kyklos::CLI.new(config_path: @config.path, adapter: 'shoryuken')
|
22
|
+
Aws.config[:cloudwatchevents] = {
|
23
|
+
stub_responses: {
|
24
|
+
list_rules: {
|
25
|
+
rules: []
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
cli.deploy
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class BaseTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_run
|
7
|
+
processed = false
|
8
|
+
closure = ->() { processed = true }
|
9
|
+
job = Kyklos::Job::Base.new('filds', closure)
|
10
|
+
|
11
|
+
job.run
|
12
|
+
assert(processed)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class JobListTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
setup do
|
7
|
+
@schedule = <<'CODE'
|
8
|
+
rate '1 minutes' do
|
9
|
+
puts 'Rate'
|
10
|
+
end
|
11
|
+
cron '* * * * * *' do
|
12
|
+
puts 'Cron'
|
13
|
+
end
|
14
|
+
CODE
|
15
|
+
end
|
16
|
+
|
17
|
+
teardown do
|
18
|
+
# Do nothing
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_initialize_without_code
|
22
|
+
assert_instance_of(Kyklos::JobList, Kyklos::JobList.new)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_initialize_with_code
|
26
|
+
assert_instance_of(Kyklos::JobList, Kyklos::JobList.new(@schedule))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_parse_jobs
|
30
|
+
job_list = Kyklos::JobList.new(@schedule)
|
31
|
+
assert_equal(2, job_list.jobs.values.size)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_cron
|
35
|
+
job_list = Kyklos::JobList.new
|
36
|
+
job_list.cron('* * * * * *'){ }
|
37
|
+
assert_instance_of(Kyklos::Job::Cron, job_list.jobs.values.last)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_rate
|
41
|
+
job_list = Kyklos::JobList.new
|
42
|
+
job_list.rate('1 minutes'){ }
|
43
|
+
assert_instance_of(Kyklos::Job::Rate, job_list.jobs.values.last)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_cron_returns_job_id
|
47
|
+
job_list = Kyklos::JobList.new
|
48
|
+
job_id = job_list.rate('1 minutes'){ }
|
49
|
+
assert_instance_of(Kyklos::Job::Rate, job_list.jobs[job_id])
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_as_option
|
53
|
+
job_list = Kyklos::JobList.new
|
54
|
+
job_list.cron('* * * * * *', as: 'cron_a'){ }
|
55
|
+
assert_instance_of(Kyklos::Job::Cron, job_list.jobs[:cron_a])
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_desc_option
|
59
|
+
job_list = Kyklos::JobList.new
|
60
|
+
job_id = job_list.cron('* * * * * *', desc: 'DESCRIPTION'){ }
|
61
|
+
assert_equal('DESCRIPTION', job_list.jobs[job_id].description)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_bracket
|
65
|
+
job_list = Kyklos::JobList.new
|
66
|
+
job_id = job_list.rate('1 minutes'){ }
|
67
|
+
assert_equal(job_list[job_id], job_list.jobs[job_id])
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_run
|
71
|
+
job_list = Kyklos::JobList.new
|
72
|
+
processed = false
|
73
|
+
job_list.cron('* * * * * *', as: :cron_a){ processed = true }
|
74
|
+
job_list.run(:cron_a)
|
75
|
+
assert(processed)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class KyklosTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Called before every test method runs. Can be used
|
7
|
+
# to set up fixture information.
|
8
|
+
def setup
|
9
|
+
# Do nothing
|
10
|
+
end
|
11
|
+
|
12
|
+
# Called after every test method runs. Can be used to tear
|
13
|
+
# down fixture information.
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
# Do nothing
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_that_it_has_a_version_number
|
20
|
+
refute_nil Kyklos::VERSION
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_parse
|
24
|
+
schedule = <<'CODE'
|
25
|
+
rate '1 minutes' do
|
26
|
+
puts 'Rate'
|
27
|
+
end
|
28
|
+
cron '* * * * * *' do
|
29
|
+
puts 'Cron'
|
30
|
+
end
|
31
|
+
CODE
|
32
|
+
assert_instance_of(Kyklos::JobList, Kyklos.parse(schedule))
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kyklos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuichi Takeuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: shoryuken
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- info@takeyu-web.com
|
86
|
+
executables:
|
87
|
+
- kyklos
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/kyklos
|
99
|
+
- examples/simple.rb
|
100
|
+
- kyklos.gemspec
|
101
|
+
- lib/kyklos.rb
|
102
|
+
- lib/kyklos/adapters.rb
|
103
|
+
- lib/kyklos/adapters/base_adapter.rb
|
104
|
+
- lib/kyklos/adapters/shoryuken_adapter.rb
|
105
|
+
- lib/kyklos/cli.rb
|
106
|
+
- lib/kyklos/job.rb
|
107
|
+
- lib/kyklos/job/base.rb
|
108
|
+
- lib/kyklos/job/cron.rb
|
109
|
+
- lib/kyklos/job/rate.rb
|
110
|
+
- lib/kyklos/job_list.rb
|
111
|
+
- lib/kyklos/version.rb
|
112
|
+
- test/test_helper.rb
|
113
|
+
- test/unit/kyklos/cli_test.rb
|
114
|
+
- test/unit/kyklos/job/base_test.rb
|
115
|
+
- test/unit/kyklos/job_list_test.rb
|
116
|
+
- test/unit/kyklos/task_test.rb
|
117
|
+
- test/unit/kyklos_test.rb
|
118
|
+
homepage: https://github.com/takeyuweb/kyklos/
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.4.5.1
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: You can use the Amazon CloudWatch Events to schedule jobs on Ruby.
|
142
|
+
test_files:
|
143
|
+
- test/unit/kyklos/cli_test.rb
|
144
|
+
- test/unit/kyklos/job/base_test.rb
|
145
|
+
- test/unit/kyklos/job_list_test.rb
|
146
|
+
- test/unit/kyklos/task_test.rb
|
147
|
+
- test/unit/kyklos_test.rb
|