gcp_scheduler 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45a9e2f3c9ca228ac9fa5751a87c9beba2ec73b8e38e0c2b1ef513d766178fc5
4
- data.tar.gz: 255d18bd2a649d13fc24b1103c209aeaf2c29268f3f887e5720de376192059b8
3
+ metadata.gz: f8ac6e8722729145a8554094fd36c0f6fbca375ceee2dbb22dabb8000da4fff5
4
+ data.tar.gz: dac2f9c3a07478d3eb8e234dad0c1f0049a7ae022689150c1e3b1ad6339cc777
5
5
  SHA512:
6
- metadata.gz: 4a1a0bb5870a8fcbd739b00f394b6be7abb39b62142531383e200e5f8c2b7c2930b9bbadcbe73dc16482400de3e05898c520bdc2449c358d842aff35a4d23ee1
7
- data.tar.gz: fc388ea4d13736d703a92f7f4098918c41c8d8648b03ac96f3549f78c5a9c3377f7db7e228f9e4cece9d0636a49bc9813fbf68d20bd2574e3060f16fe45aa01c
6
+ metadata.gz: 44a5afe8aecd424d1ece6110bb9c86fd19eb0c768da90a2644197a194d2c3ce1c3eb48c6e31f6fae912e1639cb1585adcf9ba8273c3a892ab5cdb54183a09cf1
7
+ data.tar.gz: d5f9cc18e76b649b9a5581b4a67964dd181b37e72d7aa175c6514a6f019fc90f355d8e1f794807037d9848888fb1b7f96741e20392f7eec0e4fc1de3bafc1fbe
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gcp_scheduler (0.1.0)
4
+ gcp_scheduler (0.2.0)
5
5
  activesupport
6
6
  google-cloud-scheduler-v1 (>= 0.7.0)
7
7
  thor
data/README.md CHANGED
@@ -59,16 +59,16 @@ gcp_scheduler delete --gcp_project your_project_name --prefix development- --reg
59
59
  ```yaml
60
60
  jobs:
61
61
  - name: WeeklyJob
62
- description: "Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>"
62
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
63
63
  schedule: '0 9 * * *'
64
64
  time_zone: Asia/Tokyo
65
- uri: "https://yourdomain.example.com/api/v1/jobs"
65
+ uri: https://yourdomain.example.com/api/v1/jobs
66
66
  http_method: POST
67
67
  params:
68
68
  job_name: weekly_job
69
69
  http_headers:
70
70
  Content-Type: application/json
71
- Authorization: "Bearer <%= ENV['SECRET'] %>"
71
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
72
72
  ```
73
73
 
74
74
  ## Contributing
@@ -2,7 +2,7 @@ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "gcp_scheduler"
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Akira Kusumoto"]
8
8
  s.email = ["akirakusumo10@gmail.com"]
@@ -27,10 +27,9 @@ module GcpScheduler
27
27
  end
28
28
 
29
29
  def create(scheduler_file_path:, prefix: "")
30
- Scheduler.scheduler_config(scheduler_file_path)[:jobs].each do |job|
31
- scheduler_name = "#{prefix}#{job[:name]}"
32
- puts "Create #{scheduler_name}"
33
- scheduler.create_job(name: scheduler_name,
30
+ Scheduler.scheduler_config_jobs(file_path: scheduler_file_path, job_name_prefix: prefix).each do |job|
31
+ puts "Create #{job[:name]}"
32
+ scheduler.create_job(name: job[:name],
34
33
  description: job[:description],
35
34
  uri: job[:uri],
36
35
  schedule: job[:schedule],
@@ -38,7 +37,7 @@ module GcpScheduler
38
37
  params: job[:params],
39
38
  http_method: job[:http_method],
40
39
  headers: job[:http_headers])
41
- puts "Created #{scheduler_name}"
40
+ puts "Created #{job[:name]}"
42
41
  end
43
42
  end
44
43
  end
@@ -6,6 +6,13 @@ module GcpScheduler
6
6
  def scheduler_config(file_path)
7
7
  YAML.safe_load(ERB.new(File.read(file_path)).result, aliases: true).with_indifferent_access
8
8
  end
9
+
10
+ def scheduler_config_jobs(file_path:, job_name_prefix: "")
11
+ config = scheduler_config(file_path)
12
+ config[:jobs].map do |job|
13
+ (config[:defaults] || {}).merge(job.merge(name: "#{job_name_prefix}#{job[:name]}")).with_indifferent_access
14
+ end
15
+ end
9
16
  end
10
17
 
11
18
  def initialize(project:, location:)
@@ -37,11 +44,15 @@ module GcpScheduler
37
44
  http_target: {
38
45
  uri: uri,
39
46
  http_method: http_method,
40
- body: params.to_json,
47
+ body: params,
41
48
  headers: headers
42
49
  },
43
50
  }
44
51
 
52
+ if params.present? && headers["Content-Type"] == "application/json"
53
+ job[:http_target][:body] = params.to_json
54
+ end
55
+
45
56
  client.create_job parent: parent, job: job
46
57
  end
47
58
 
@@ -1,12 +1,12 @@
1
1
  jobs:
2
2
  - name: WeeklyJob
3
- description: "Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>"
3
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
4
4
  schedule: '0 9 * * *'
5
5
  time_zone: Asia/Tokyo
6
- uri: "https://yourdomain.example.com/api/v1/jobs"
6
+ uri: https://yourdomain.example.com/api/v1/jobs
7
7
  http_method: POST
8
8
  params:
9
9
  job_name: weekly_job
10
10
  http_headers:
11
11
  Content-Type: application/json
12
- Authorization: "Bearer <%= ENV['SECRET'] %>"
12
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
@@ -0,0 +1,19 @@
1
+ defaults:
2
+ uri: https://yourdomain.example.com/api/v1/jobs
3
+ http_method: POST
4
+ time_zone: Asia/Tokyo
5
+ http_headers:
6
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
7
+ Content-Type: application/json
8
+
9
+ jobs:
10
+ - name: WeeklyJob
11
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
12
+ schedule: '0 9 * * *'
13
+ params:
14
+ job_name: weekly_job
15
+ - name: DailyJob
16
+ description: 'Runs every day at 10:30 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
17
+ schedule: '30 10 * * *'
18
+ params:
19
+ job_name: daily_job
@@ -27,4 +27,60 @@ describe GcpScheduler::Scheduler do
27
27
  expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
28
28
  end
29
29
  end
30
+
31
+ describe "#scheduler_config_jobs" do
32
+ before do
33
+ travel_to Time.parse("2023/4/1 11:00")
34
+ ENV["SECRET"] = "secret_key"
35
+ end
36
+
37
+ after do
38
+ travel_back
39
+ end
40
+
41
+ it "Configuration file with default settings" do
42
+ path = fixture_path("scheduler_with_defaults.yml")
43
+ jobs = GcpScheduler::Scheduler.scheduler_config_jobs(file_path: path,
44
+ job_name_prefix: "rspec-")
45
+
46
+ expect(jobs.size).to eq(2)
47
+ weekly_job = jobs.first
48
+ expect(weekly_job[:name]).to eq("rspec-WeeklyJob")
49
+ expect(weekly_job[:params]).to eq({ "job_name" => "weekly_job" })
50
+ expect(weekly_job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 11:00")
51
+ expect(weekly_job[:schedule]).to eq("0 9 * * *")
52
+
53
+ daily_job = jobs.last
54
+ expect(daily_job[:name]).to eq("rspec-DailyJob")
55
+ expect(daily_job[:params]).to eq({ "job_name" => "daily_job" })
56
+ expect(daily_job[:description]).to eq("Runs every day at 10:30 a.m. Created:2023/04/01 11:00")
57
+ expect(daily_job[:schedule]).to eq("30 10 * * *")
58
+
59
+ # defaults
60
+ jobs.each do |job|
61
+ expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
62
+ expect(job[:http_method]).to eq("POST")
63
+ expect(job[:time_zone]).to eq("Asia/Tokyo")
64
+ http_headers = { "Authorization" => "Bearer secret_key", "Content-Type" => "application/json" }
65
+ expect(job[:http_headers]).to eq(http_headers)
66
+ end
67
+ end
68
+
69
+ it "Configuration file without default settings" do
70
+ path = fixture_path("scheduler.yml")
71
+ jobs = GcpScheduler::Scheduler.scheduler_config_jobs(file_path: path,
72
+ job_name_prefix: "rspec-")
73
+
74
+ expect(jobs.size).to eq(1)
75
+ job = jobs.first
76
+ expect(job[:name]).to eq("rspec-WeeklyJob")
77
+ expect(job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 11:00")
78
+ expect(job[:schedule]).to eq("0 9 * * *")
79
+ expect(job[:time_zone]).to eq("Asia/Tokyo")
80
+ expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
81
+ expect(job[:http_method]).to eq("POST")
82
+ expect(job[:params]).to eq({ "job_name" => "weekly_job" })
83
+ expect(job[:http_headers]).to eq("Authorization" => "Bearer secret_key", "Content-Type" => "application/json")
84
+ end
85
+ end
30
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcp_scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Kusumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-21 00:00:00.000000000 Z
11
+ date: 2023-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -106,6 +106,7 @@ files:
106
106
  - lib/gcp_scheduler/command.rb
107
107
  - lib/gcp_scheduler/scheduler.rb
108
108
  - spec/fixtures/scheduler.yml
109
+ - spec/fixtures/scheduler_with_defaults.yml
109
110
  - spec/gcp_scheduler/scheduler_spec.rb
110
111
  - spec/gcp_scheduler_spec.rb
111
112
  - spec/spec_helper.rb