Skip to content

Commit

Permalink
Merge pull request #1 from bluerabbit/scheduler_configuration_file
Browse files Browse the repository at this point in the history
YAML configuration file changed
  • Loading branch information
bluerabbit committed Apr 21, 2023
2 parents d3d0c7f + 9ea39f2 commit c809c97
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 77 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ export GOOGLE_APPLICATION_CREDENTIALS=your_credential.json
### ジョブの作成

```
gcp_scheduler create --gcp_project your_project_name --scheduler_file path/to/scheduler.yml --uri https://your_domain.example.com --prefix development- --region asia-northeast1 --secret your_secret_token
gcp_scheduler create --gcp_project your_project_name --scheduler_file path/to/scheduler.yml --prefix development- --region asia-northeast1
```

- scheduler_fileを元にジョブを作成します。
- prefixは、ジョブ名に付与されるプレフィックスです。
- regionは、Cloud Schedulerのリージョンです。
- uriは、スケジューラーがHTTP POSTを実行するURLです。
- すべてのジョブは一つのURLにapplication/jsonでリクエストパラメータのjob_nameにscheduler_fileのjob名が入ります。
- secretは、Http Authorization Header Bearer tokenです。

### ジョブの一覧表示

Expand All @@ -52,19 +49,26 @@ gcp_scheduler delete --gcp_project your_project_name --prefix development- --reg
|---|-------------------------------------------------------------------------------------------|
| `--gcp_project` | Google Cloud project name |
| `--scheduler_file` | Path to the scheduler configuration file (used with `create` command) |
| `--uri` | URL of the endpoint to be executed by the job (used with `create` command) |
| `--prefix` | Prefix for the job name (used with `create`, `list`, and `delete` commands) |
| `--region` | Cloud Scheduler region |
| `--secret` | Http Authorization Header Bearer token to be sent by the job (used with `create` command) |

## scheduler.yml file

scheduler.ymlファイルは、ジョブのスケジュール情報を定義するために使用されます。以下は、scheduler.ymlファイルのサンプルです。
`--scheduler_file`オプションでyamlファイルを指定してください。スケジュールを定義するために使用されます。以下は、scheduler.ymlファイルのサンプルです。

```yaml
weekly_job:
class: WeeklyJob
cron: '0 9 * * 1'
jobs:
- name: WeeklyJob
description: "Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>"
schedule: '0 9 * * *'
time_zone: Asia/Tokyo
uri: "https://yourdomain.example.com/api/v1/jobs"
http_method: POST
params:
job_name: weekly_job
http_headers:
Content-Type: application/json
Authorization: "Bearer <%= ENV['SECRET'] %>"
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion gcp_scheduler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.email = ["akirakusumo10@gmail.com"]
s.homepage = "https://github.com/bluerabbit/gcp_scheduler"
s.summary = "A command-line interface for managing Google Cloud Scheduler jobs with ease"
s.description = "GCP Scheduler CLI is a Ruby gem that provides a simple command-line interface for managing Google Cloud Scheduler jobs. With this tool, you can create, list, and delete Cloud Scheduler jobs using intuitive commands. It streamlines job management tasks by allowing you to define job schedules in a YAML file and supports custom job prefixes, regions, and authentication tokens."
s.description = "GCP Scheduler is a Ruby gem that provides a simple command-line interface for managing Google Cloud Scheduler jobs. With this tool, you can create, list, and delete Cloud Scheduler jobs using intuitive commands. It streamlines job management tasks by allowing you to define job schedules in a YAML file."

s.files = `git ls-files`.split("\n")
s.require_paths = ["lib"]
Expand Down
17 changes: 7 additions & 10 deletions lib/gcp_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ module GcpScheduler

class << self
def list(gcp_project:, region:, prefix: "")
GcpScheduler::Command.list(gcp_project: gcp_project, prefix: prefix, region: region)
command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
command.list(prefix: prefix)
end

def delete(gcp_project:, region:, prefix: "")
GcpScheduler::Command.delete(gcp_project: gcp_project, prefix: prefix, region: region)
command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
command.delete(prefix: prefix)
end

def create(gcp_project:, region:, scheduler_file_path:, uri:, secret:, time_zone:, prefix: "")
GcpScheduler::Command.create(gcp_project: gcp_project,
region: region,
prefix: prefix,
scheduler_file_path: scheduler_file_path,
uri: uri,
secret: secret,
time_zone: time_zone)
def create(gcp_project:, region:, scheduler_file_path:, prefix: "")
command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
command.create(prefix: prefix, scheduler_file_path: scheduler_file_path)
end
end
end
12 changes: 3 additions & 9 deletions lib/gcp_scheduler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,14 @@ def delete

desc "create", "Create schedules from scheduler.yml"
method_option :gcp_project, type: :string, required: true
method_option :region, type: :string, default: "asia-northeast1"
method_option :prefix, type: :string, default: ""
method_option :region, type: :string, required: true
method_option :scheduler_file, type: :string, required: true
method_option :uri, type: :string, required: true
method_option :secret, type: :string, default: ""
method_option :time_zone, type: :string, default: "Etc/UTC"
method_option :prefix, type: :string, default: ""
def create
GcpScheduler.create(gcp_project: options[:gcp_project],
region: options[:region],
prefix: options[:prefix],
scheduler_file_path: options[:scheduler_file],
uri: options[:uri],
secret: options[:secret],
time_zone: options[:time_zone])
scheduler_file_path: options[:scheduler_file])
end

desc "version", "Show Version"
Expand Down
64 changes: 33 additions & 31 deletions lib/gcp_scheduler/command.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
module GcpScheduler
class Command
class << self
def list(gcp_project:, region:, prefix: "")
scheduler = Scheduler.new(project: gcp_project, location: region)
scheduler.jobs.each do |job|
scheduler_name = job.name.split("/").last
next unless scheduler_name.start_with?(prefix)
attr_reader :scheduler

puts "- name:#{scheduler_name} description:#{job.description} schedule:#{job.schedule}"
end
def initialize(gcp_project:, region:)
@scheduler = Scheduler.new(project: gcp_project, location: region)
end

def list(prefix: "")
scheduler.jobs.each do |job|
scheduler_name = job.name.split("/").last
next unless scheduler_name.start_with?(prefix)

puts "- name:#{scheduler_name} description:#{job.description} schedule:#{job.schedule}"
end
end

def delete(gcp_project:, region:, prefix: "")
scheduler = Scheduler.new(project: gcp_project, location: region)
scheduler.jobs.each do |job|
scheduler_name = job.name.split("/").last
next unless scheduler_name.start_with?(prefix)
def delete(prefix: "")
scheduler.jobs.each do |job|
scheduler_name = job.name.split("/").last
next unless scheduler_name.start_with?(prefix)

puts "Delete #{scheduler_name}"
scheduler.delete_job(scheduler_name)
puts "Deleted #{scheduler_name}"
end
puts "Delete #{scheduler_name}"
scheduler.delete_job(scheduler_name)
puts "Deleted #{scheduler_name}"
end
end

def create(gcp_project:, region:, scheduler_file_path:, uri:, secret:, time_zone:, prefix: "")
scheduler = Scheduler.new(project: gcp_project, location: region)
Scheduler.scheduler_config(scheduler_file_path).each do |name, h|
scheduler_name = "#{prefix}#{name}"
puts "Create #{scheduler_name}"
scheduler.create_job(name: scheduler_name,
description: "#{h["class"]} CreatedAt:#{Time.current.strftime("%Y/%m/%d %-H:%M")}",
uri: uri,
schedule: h["cron"],
params: { job_name: name },
secret: secret,
time_zone: time_zone)
puts "Created #{scheduler_name}"
end
def create(scheduler_file_path:, prefix: "")
Scheduler.scheduler_config(scheduler_file_path)[:jobs].each do |job|
scheduler_name = "#{prefix}#{job[:name]}"
puts "Create #{scheduler_name}"
scheduler.create_job(name: scheduler_name,
description: job[:description],
uri: job[:uri],
schedule: job[:schedule],
time_zone: job[:time_zone],
params: job[:params],
http_method: job[:http_method],
headers: job[:http_headers])
puts "Created #{scheduler_name}"
end
end
end
Expand Down
15 changes: 5 additions & 10 deletions lib/gcp_scheduler/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Scheduler

class << self
def scheduler_config(file_path)
YAML.load(ERB.new(File.read(file_path)).result).with_indifferent_access
YAML.safe_load(ERB.new(File.read(file_path)).result, aliases: true).with_indifferent_access
end
end

Expand All @@ -24,8 +24,9 @@ def create_job(name:,
description:,
uri:,
schedule:,
secret:, time_zone:, params: {},
http_method: :POST,
time_zone:,
params: {},
http_method:,
headers: {})

job = {
Expand All @@ -37,13 +38,7 @@ def create_job(name:,
uri: uri,
http_method: http_method,
body: params.to_json,
headers: headers.merge(
{
"Authorization" => "Bearer #{secret}",
"Content-Type" => "application/json",
"User-Agent" => "Google-Cloud-Scheduler",
},
),
headers: headers
},
}

Expand Down
15 changes: 12 additions & 3 deletions spec/fixtures/scheduler.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
weekly_job:
class: WeeklyJob
cron: '0 9 * * 1'
jobs:
- name: WeeklyJob
description: "Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>"
schedule: '0 9 * * *'
time_zone: Asia/Tokyo
uri: "https://yourdomain.example.com/api/v1/jobs"
http_method: POST
params:
job_name: weekly_job
http_headers:
Content-Type: application/json
Authorization: "Bearer <%= ENV['SECRET'] %>"
23 changes: 21 additions & 2 deletions spec/gcp_scheduler/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

describe GcpScheduler::Scheduler do
describe "#scheduler_config" do
before do
travel_to Time.parse("2023/4/1 10:00")
ENV["SECRET"] = "secret_key"
end

after do
travel_back
end

it do
path = fixture_path("scheduler.yml")
path = fixture_path("scheduler.yml")
config = GcpScheduler::Scheduler.scheduler_config(path)
expect(config).to eq({ "weekly_job" => { "class" => "WeeklyJob", "cron" => "0 9 * * 1" } })
jobs = config[:jobs]
expect(jobs.size).to eq(1)
job = jobs.first
expect(job[:params]).to eq({ "job_name" => "weekly_job" })
expect(job[:time_zone]).to eq("Asia/Tokyo")
expect(job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 10:00")
expect(job[:http_headers]).to eq("Authorization" => "Bearer secret_key", "Content-Type" => "application/json")
expect(job[:http_method]).to eq("POST")
expect(job[:name]).to eq("WeeklyJob")
expect(job[:schedule]).to eq("0 9 * * *")
expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "pry"
require "rspec"

require "active_support/testing/time_helpers"
require "gcp_scheduler"
require_relative "support/path"

RSpec.configure do |config|
config.include Spec::Path
config.include ActiveSupport::Testing::TimeHelpers
end

0 comments on commit c809c97

Please sign in to comment.