Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implemented bin/perfectsched

  • Loading branch information...
commit 6dee6d6eab79dbb10888833713eb5cdb90f5be79 1 parent 14a9a44
@frsyuki frsyuki authored
View
19 lib/perfectsched/backend/rdb_compat.rb
@@ -52,6 +52,23 @@ def initialize(client, config)
attr_reader :db
+ def init_database(options)
+ sql = %[
+ CREATE TABLE IF NOT EXISTS `test_scheds` (
+ id VARCHAR(256) NOT NULL,
+ timeout INT NOT NULL,
+ next_time INT NOT NULL,
+ cron VARCHAR(128) NOT NULL,
+ delay INT NOT NULL,
+ data BLOB NOT NULL,
+ timezone VARCHAR(256) NULL,
+ PRIMARY KEY (id)
+ );]
+ connect {
+ @db.run sql
+ }
+ end
+
def get_schedule_metadata(key, options={})
connect {
row = @db.fetch("SELECT id, timeout, next_time, cron, delay, data, timezone FROM `#{@table}` LIMIT 1").first
@@ -73,7 +90,7 @@ def list(options, &block)
}
end
- def submit(key, type, cron, delay, timezone, data, next_time, next_run_time, options)
+ def add(key, type, cron, delay, timezone, data, next_time, next_run_time, options)
data = data.dup
data[:type] = type
connect {
View
8 lib/perfectsched/client.rb
@@ -33,6 +33,10 @@ def initialize(config)
attr_reader :backend
attr_reader :config
+ def init_database(options={})
+ @backend.init_database(options)
+ end
+
def get_schedule_metadata(key, options={})
@backend.get_schedule_metadata(key, options)
end
@@ -43,7 +47,7 @@ def get_schedule_metadata(key, options={})
# :data
# :delay => 0
# :timezone => UTC
- def submit(key, type, options={})
+ def add(key, type, options={})
cron = options[:cron]
raise ArgumentError, ":cron option is required" unless cron
@@ -62,7 +66,7 @@ def submit(key, type, options={})
next_run_time = next_time + delay
end
- @backend.submit(key, type, cron, delay, timezone, data, next_time, next_run_time, options)
+ @backend.add(key, type, cron, delay, timezone, data, next_time, next_run_time, options)
# TODO return value
return next_time, next_run_time
View
41 lib/perfectsched/command/perfectsched.rb
@@ -10,6 +10,7 @@
add <key> <type> <cron> <data> Register a new schedule
delete <key> Delete a registered schedule
run <class> Run a worker process
+ init Initialize a backend database
]
op.version = PerfectSched::VERSION
@@ -18,11 +19,11 @@
config_path = 'config/perfectsched.yml'
require_files = []
-add_config = {
+add_options = {
:delay => 0,
:timezone => 'UTC',
- :start => nil,
- :run => nil,
+ :next_time => nil,
+ :next_run_time => nil,
}
op.separator("options:")
@@ -38,19 +39,19 @@
op.separator("\noptions for add:")
op.on('-d', '--delay SEC', 'Delay time before running a schedule (default: 0)', Integer) {|i|
- add_config[:delay] = i
+ add_options[:delay] = i
}
op.on('-t', '--timezone NAME', 'Set timezone (default: UTC)') {|s|
- add_config[:timezone] = s
+ add_options[:timezone] = s
}
op.on('-s', '--start UNIXTIME', 'Set the first schedule time (default: now)', Integer) {|i|
- add_config[:start] = i
+ add_options[:next_time] = i
}
op.on('-a', '--at UNIXTIME', 'Set the first run time (default: start+delay)', Integer) {|i|
- add_config[:run] = i
+ add_options[:next_run_time] = i
}
op.separator("\noptions for run:")
@@ -80,12 +81,12 @@
when 'delete'
cmd = :delete
+ usage nil unless ARGV.length == 1
key = ARGV[0]
- usage nil unless ARGV.length == 0
when 'add'
cmd = :add
- usage nil unless ARGV.length == 3
+ usage nil unless ARGV.length == 4
key, type, cron, data = *ARGV
require 'json'
data = JSON.load(data)
@@ -95,6 +96,10 @@
usage nil unless ARGV.length == 1
klass = ARGV[0]
+ when 'init'
+ cmd = :init
+ usage nil unless ARGV.length == 0
+
else
raise "unknown command: '#{cmd}'"
end
@@ -117,12 +122,17 @@
case cmd
when :list
+ n = 0
PerfectSched.open(config_load_proc.call) {|scheds|
+ format = "%30s %10s %18s %7s %11s %28s %28s %s"
+ puts format % ['key', 'type', 'cron', 'delay', 'timezone', 'next_time', 'next_run_time', 'data']
scheds.list {|sched|
- # TODO
- p sched
+ next_time = sched.next_time ? Time.at(sched.next_time) : sched.next_time
+ next_run_time = sched.next_run_time ? Time.at(sched.next_run_time) : sched.next_run_time
+ puts format % [sched.key, sched.type, sched.cron, sched.delay, sched.timezone, next_time, next_run_time, sched.data]
}
}
+ puts "#{n} entries."
when :delete
PerfectSched.open(config_load_proc.call) {|scheds|
@@ -131,7 +141,9 @@
when :add
PerfectSched.open(config_load_proc.call) {|scheds|
- scheds.submit(key, type, {:cron=>cron, :timezone=>timezone, :data=>data})
+ add_options[:cron] = cron
+ add_options[:data] = data
+ scheds.add(key, type, add_options)
}
when :run
@@ -140,5 +152,10 @@
}
klass = Object.const_get(klass)
PerfectSched::Worker.run(klass, &config_load_proc)
+
+when :init
+ PerfectSched.open(config_load_proc.call) {|scheds|
+ scheds.client.init_database
+ }
end
View
4 lib/perfectsched/schedule_collection.rb
@@ -49,8 +49,8 @@ def poll_multi(options={})
# :data => Hash
# :cron
# :timezone
- def submit(key, type, options={})
- @client.submit(key, type, options)
+ def add(key, type, options={})
+ @client.add(key, type, options)
end
def close
Please sign in to comment.
Something went wrong with that request. Please try again.