fluentd-server 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.env +5 -2
  3. data/.gitignore +2 -0
  4. data/.travis.yml +3 -0
  5. data/CHANGELOG.md +12 -0
  6. data/Procfile +2 -0
  7. data/README.md +19 -7
  8. data/Rakefile +7 -9
  9. data/db/migrate/{20140512203133_create_posts.rb → 0001_create_posts.rb} +0 -0
  10. data/db/migrate/0002_create_delayed_jobs.rb +22 -0
  11. data/db/migrate/0003_create_tasks.rb +14 -0
  12. data/db/schema.rb +25 -1
  13. data/fluentd-server.gemspec +4 -0
  14. data/jobs/.gitkeep +0 -0
  15. data/lib/fluentd_server/cli.rb +55 -5
  16. data/lib/fluentd_server/config.rb +12 -0
  17. data/lib/fluentd_server/decorator.rb +23 -1
  18. data/lib/fluentd_server/{environments.rb → environment.rb} +17 -1
  19. data/lib/fluentd_server/model.rb +59 -1
  20. data/lib/fluentd_server/task_runner.rb +88 -0
  21. data/lib/fluentd_server/version.rb +1 -1
  22. data/lib/fluentd_server/web.rb +68 -21
  23. data/lib/fluentd_server/web_helper.rb +14 -10
  24. data/public/css/bootstrap.min.css +4 -4
  25. data/public/js/bootstrap.min.js +4 -5
  26. data/{bench → spec/bench}/bench.rb +0 -0
  27. data/{bench → spec/bench}/result.md +1 -1
  28. data/spec/model_spec.rb +33 -0
  29. data/spec/spec_helper.rb +8 -3
  30. data/spec/task_runner_spec.rb +27 -0
  31. data/spec/tmp/.gitkeep +0 -0
  32. data/spec/web_helper_spec.rb +15 -0
  33. data/spec/web_spec.rb +103 -1
  34. data/views/_js.slim +30 -0
  35. data/views/_navbar.slim +6 -4
  36. data/views/_style.slim +4 -0
  37. data/views/layout.slim +8 -11
  38. data/views/posts/create.slim +3 -2
  39. data/views/posts/edit.slim +4 -3
  40. data/views/posts/layout.slim +8 -0
  41. data/views/posts/menu.slim +7 -0
  42. data/views/tasks/ajax.erb +21 -0
  43. data/views/tasks/show.slim +24 -0
  44. metadata +124 -53
  45. data/spec/api_spec.rb +0 -42
  46. data/views/posts/index.slim +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23543f023f2b14f82abde08cceba1337e730c494
4
- data.tar.gz: bb43d8d9a3686cc3e1cba3da52bf532438ba1cc9
3
+ metadata.gz: 27858b7cd48db1e7cad5f4b2d1f10724b863f103
4
+ data.tar.gz: 0d0ed73cd1cc881632cafdcefb546b9a9071f3b0
5
5
  SHA512:
6
- metadata.gz: b430c589ae0aaa6f102d36c98a61dc6230510ea590f528922f26d30a3e4b1511b07b323d7107b82ed417c00fab6734cb657ff7e4ceca60ef771d9a0766a0be64
7
- data.tar.gz: 512e455f5e280674c272a992c8ea676323e207c7693eb597d460a9a6d3c88135b394d21b95872724a28c13f340d53538e5832f982b34132cf34644bc1dde0376
6
+ metadata.gz: bfb1a7297aa0c1f929966fa330060ba52c0ddc207418abf9cf6de97c2b6ac00ad2fb7ef449c833531706f4008755c1fbca69466460d19131defbc9e1f99e9140
7
+ data.tar.gz: dc2dc999877f2f215d3ab65ee29823697e6429690a150b89ad8da5e12b273ce72519dff4299130f0aad8d93e94fe556a3328e00867e18aa98a5c0ec7b4fdbd36
data/.env CHANGED
@@ -1,5 +1,8 @@
1
1
  PORT=5126
2
2
  HOST=0.0.0.0
3
3
  # DATABASE_URL=sqlite3:data/fluentd_server.db
4
- # LOG_PATH=log/application.log
5
- # LOG_LEVEL=warn
4
+ # JOB_DIR=jobs
5
+ # LOG_PATH=STDOUT
6
+ # LOG_LEVEL=debug
7
+ # LOG_SHIFT_AGE=0
8
+ # LOG_SHIFT_SIZE=1048576
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ pkg/*
15
15
  data/*
16
16
  log/*
17
17
  fluentd-server/*
18
+ spec/tmp
19
+ jobs/*
data/.travis.yml CHANGED
@@ -3,3 +3,6 @@ rvm:
3
3
  - 2.1
4
4
  gemfile:
5
5
  - Gemfile
6
+ env:
7
+ - DATA_DIR=
8
+ - DATA_DIR=spec/tmp
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 0.2.0
2
+
3
+ Enhancements:
4
+
5
+ * Support restarting td-agent via serf
6
+ * Created serf-td-agent gem
7
+ * Introduce Resque
8
+ * Ajax reload of command result
9
+ * Save/load the content of configuration into/from a file (experimental)
10
+ * Created acts_as_file gem
11
+
1
12
  # 0.1.0
2
13
 
3
14
  First version
15
+
data/Procfile CHANGED
@@ -1 +1,3 @@
1
1
  web: bundle exec unicorn -E production -p $PORT -o $HOST -c config/unicorn.conf
2
+ job: bundle exec bin/fluentd-server job
3
+ serf: $(bundle exec gem path serf-td-agent)/bin/serf agent
data/README.md CHANGED
@@ -47,7 +47,7 @@ The `include` directive of fluentd config supports `http`, so write just one lin
47
47
  include http://fqdn.to.fluentd-server/api/:name?port=24224
48
48
  ```
49
49
 
50
- where :name is the name of your config post, so that it will download the real configuration from the Fluentd Server.
50
+ so that it will download the real configuration from the Fluentd Server where :name is the name of your post.
51
51
 
52
52
  ## Installation
53
53
 
@@ -95,20 +95,32 @@ The default configuration is as follows:
95
95
  PORT=5126
96
96
  HOST=0.0.0.0
97
97
  # DATABASE_URL=sqlite3:data/fluentd_server.db
98
- # LOG_PATH=log/application.log
99
- # LOG_LEVEL=warn
98
+ # JOB_DIR=jobs
99
+ # LOG_PATH=STDOUT
100
+ # LOG_LEVEL=debug
101
+ # LOG_SHIFT_AGE=0
102
+ # LOG_SHIFT_SIZE=1048576
100
103
  ```
101
104
 
105
+ ### DATA_DIR (experimental)
106
+
107
+ Configure `DATA_DIR` in `.env` file as:
108
+
109
+ ```
110
+ DATA_DIR=data
111
+ ```
112
+
113
+ to store and load Fluentd config contents not from DB, but from local files located at the directory `DATA_DIR`.
114
+ This would be useful when you want to manage your config files with git.
115
+
102
116
  ## HTTP API
103
117
 
104
118
  See [API.md](API.md).
105
119
 
106
120
  ## ToDo
107
121
 
108
- * Local file storage
109
-
110
- * Saving and loading conf from localfiles rather than DB would be nice because it makes possible to manage conf with git
111
- * Fluentd Server should cache them on memory, and refresh caches by detecting files are updated
122
+ * Create a sync worker to synchronize local file existences with db (delete and create entries on db).
123
+ * Automatic deployment (restart) support like the one of chef-server
112
124
 
113
125
  ## ChangeLog
114
126
 
data/Rakefile CHANGED
@@ -1,17 +1,16 @@
1
1
  require "bundler/gem_tasks"
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dotenv/tasks'
5
+ require 'fluentd_server/environment'
2
6
 
3
7
  require 'rspec/core/rake_task'
4
8
  RSpec::Core::RakeTask.new(:spec) do |t|
5
9
  t.rspec_opts = ["-c", "-f progress"] # '--format specdoc'
6
10
  t.pattern = 'spec/**/*_spec.rb'
7
11
  end
8
-
9
- lib = File.expand_path('../lib', __FILE__)
10
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
11
- require 'dotenv/tasks'
12
- require 'fluentd_server/config'
13
-
14
- require 'sinatra/activerecord/rake'
12
+ task :test => :spec
13
+ task :default => :spec
15
14
 
16
15
  task :console => :dotenv do
17
16
  require "fluentd_server"
@@ -22,5 +21,4 @@ task :console => :dotenv do
22
21
  end
23
22
  task :c => :console
24
23
 
25
- task :test => :spec
26
- task :default => :spec
24
+ require 'sinatra/activerecord/rake'
@@ -0,0 +1,22 @@
1
+ class CreateDelayedJobs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :delayed_jobs do |table|
4
+ table.integer :priority, :default => 0, :null => false # Allows some jobs to jump to the front of the queue
5
+ table.integer :attempts, :default => 0, :null => false # Provides for retries, but still fail eventually.
6
+ table.text :handler, :null => false # YAML-encoded string of the object that will do work
7
+ table.text :last_error # reason for last failure (See Note below)
8
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
9
+ table.datetime :locked_at # Set when a client is working on this object
10
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
11
+ table.string :locked_by # Who is working on this object (if locked)
12
+ table.string :queue # The name of the queue this job is in
13
+ table.timestamps
14
+ end
15
+
16
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
17
+ end
18
+
19
+ def self.down
20
+ drop_table :delayed_jobs
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class CreateTasks < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tasks do |t|
4
+ t.string :name
5
+ t.text :body
6
+ t.integer :exit_code
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :tasks
13
+ end
14
+ end
data/db/schema.rb CHANGED
@@ -11,7 +11,23 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20140512203133) do
14
+ ActiveRecord::Schema.define(version: 3) do
15
+
16
+ create_table "delayed_jobs", force: true do |t|
17
+ t.integer "priority", default: 0, null: false
18
+ t.integer "attempts", default: 0, null: false
19
+ t.text "handler", null: false
20
+ t.text "last_error"
21
+ t.datetime "run_at"
22
+ t.datetime "locked_at"
23
+ t.datetime "failed_at"
24
+ t.string "locked_by"
25
+ t.string "queue"
26
+ t.datetime "created_at"
27
+ t.datetime "updated_at"
28
+ end
29
+
30
+ add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority"
15
31
 
16
32
  create_table "posts", force: true do |t|
17
33
  t.string "name"
@@ -22,4 +38,12 @@ ActiveRecord::Schema.define(version: 20140512203133) do
22
38
 
23
39
  add_index "posts", ["name"], name: "index_posts_on_name", unique: true
24
40
 
41
+ create_table "tasks", force: true do |t|
42
+ t.string "name"
43
+ t.text "body"
44
+ t.integer "exit_code"
45
+ t.datetime "created_at"
46
+ t.datetime "updated_at"
47
+ end
48
+
25
49
  end
@@ -39,5 +39,9 @@ Gem::Specification.new do |spec|
39
39
  spec.add_runtime_dependency 'slim'
40
40
  spec.add_runtime_dependency "unicorn"
41
41
  spec.add_runtime_dependency "unicorn-worker-killer"
42
+ spec.add_runtime_dependency "delayed_job_active_record"
43
+ spec.add_runtime_dependency "daemons"
44
+ spec.add_runtime_dependency "serf-td-agent"
45
+ spec.add_runtime_dependency "acts_as_file"
42
46
  # spec.add_runtime_dependency 'sqlite3'
43
47
  end
data/jobs/.gitkeep ADDED
File without changes
@@ -7,54 +7,104 @@ class FluentdServer::CLI < Thor
7
7
  BASE_DIR = File.join(Dir.pwd, "fluentd-server")
8
8
  DATA_DIR = File.join(BASE_DIR, "data")
9
9
  LOG_DIR = File.join(BASE_DIR, "log")
10
+ JOB_DIR = File.join(BASE_DIR, "jobs")
10
11
  LOG_FILE = File.join(LOG_DIR, "application.log")
11
12
  ENV_FILE = File.join(BASE_DIR, ".env")
12
13
  PROCFILE = File.join(BASE_DIR, "Procfile")
13
14
  CONFIGRU_FILE = File.join(BASE_DIR, "config.ru")
14
- DB_DIR = File.join(BASE_DIR, "db")
15
15
  CONFIG_DIR= File.join(BASE_DIR, "config")
16
16
 
17
17
  DEFAULT_DOTENV =<<-EOS
18
18
  PORT=5126
19
19
  HOST=0.0.0.0
20
20
  DATABASE_URL=sqlite3:#{DATA_DIR}/fluentd_server.db
21
+ JOB_DIR=#{JOB_DIR}
21
22
  LOG_PATH=#{LOG_FILE}
22
23
  LOG_LEVEL=warn
24
+ LOG_SHIFT_AGE=0
25
+ LOG_SHIFT_SIZE=1048576
23
26
  EOS
24
27
 
25
28
  DEFAULT_PROCFILE =<<-EOS
26
29
  web: unicorn -E production -p $PORT -o $HOST -c config/unicorn.conf
30
+ job: fluentd-server job
31
+ serf: $(gem path serf-td-agent)/bin/serf agent
27
32
  EOS
28
33
 
29
34
  default_command :start
30
35
 
36
+ def initialize(args = [], opts = [], config = {})
37
+ super(args, opts, config)
38
+ end
39
+
31
40
  desc "new", "Creates fluentd-server resource directory"
32
41
  def new
33
42
  FileUtils.mkdir_p(LOG_DIR)
43
+ FileUtils.mkdir_p(JOB_DIR)
34
44
  File.write ENV_FILE, DEFAULT_DOTENV
35
45
  File.write PROCFILE, DEFAULT_PROCFILE
36
46
  FileUtils.cp(File.expand_path("../../../config.ru", __FILE__), CONFIGRU_FILE)
37
- FileUtils.cp_r(File.expand_path("../../../db", __FILE__), DB_DIR)
38
47
  FileUtils.cp_r(File.expand_path("../../../config", __FILE__), CONFIG_DIR)
39
48
  puts 'fluentd-server new finished.'
40
49
  end
41
50
 
42
51
  desc "init", "Creates database schema"
43
52
  def init
44
- require 'fluentd_server/environments'
53
+ Dotenv.load
54
+ require 'fluentd_server/environment'
45
55
  require 'rake'
46
56
  require 'sinatra/activerecord/rake'
47
- Rake::Task['db:migrate'].invoke
57
+ ActiveRecord::Tasks::DatabaseTasks.db_dir = File.expand_path("../../../db", __FILE__)
58
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [File.expand_path("../../../db/migrate", __FILE__)]
59
+ # ToDo: Fix that db:migrate raises an error in the case of sqlite3 like
60
+ # SQLite3::SQLException: database schema has changed: INSERT INTO "schema_migrations" ("version") VALUES (?)
61
+ # Rake::Task['db:migrate'].invoke
62
+ # Use db:schema:load after generating db/schema.rb by executing db:migrate several times for now
63
+ Rake::Task['db:schema:load'].invoke
48
64
  puts 'fluentd-server init finished.'
49
65
  end
50
66
 
67
+ desc "migrate", "Migrate database schema"
68
+ def migrate
69
+ Dotenv.load
70
+ require 'fluentd_server/environment'
71
+ require 'rake'
72
+ require 'sinatra/activerecord/rake'
73
+ ActiveRecord::Tasks::DatabaseTasks.db_dir = File.expand_path("../../../db", __FILE__)
74
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [File.expand_path("../../../db/migrate", __FILE__)]
75
+ Rake::Task['db:migrate'].invoke
76
+ end
77
+
51
78
  desc "start", "Sartup fluentd_server"
52
79
  def start
53
- Dotenv.load
80
+ self.migrate # do migration before start not to forget on updation
54
81
  require "foreman/cli"
55
82
  Foreman::CLI.new.invoke(:start, [], {})
56
83
  end
57
84
 
85
+ # reference: https://gist.github.com/robhurring/732327
86
+ desc "job", "Sartup fluentd_server job worker"
87
+ def job
88
+ Dotenv.load
89
+ require 'delayed_job'
90
+ require 'fluentd_server/model'
91
+ worker_options = {
92
+ :min_priority => ENV['MIN_PRIORITY'],
93
+ :max_priority => ENV['MAX_PRIORITY'],
94
+ :queues => (ENV['QUEUES'] || ENV['QUEUE'] || '').split(','),
95
+ :quiet => false
96
+ }
97
+ Delayed::Worker.new(worker_options).start
98
+ end
99
+
100
+ desc "job_clear", "Clear fluentd_server delayed_job queue"
101
+ def job_clear
102
+ Dotenv.load
103
+ require 'delayed_job'
104
+ require 'fluentd_server/model'
105
+ Delayed::Job.delete_all
106
+ end
107
+
58
108
  no_tasks do
59
109
  def abort(msg)
60
110
  $stderr.puts msg
@@ -5,10 +5,18 @@ require 'dotenv'
5
5
  Dotenv.load
6
6
 
7
7
  module FluentdServer::Config
8
+ def self.data_dir
9
+ ENV['DATA_DIR'] == "" ? nil : ENV['DATA_DIR']
10
+ end
11
+
8
12
  def self.database_url
9
13
  ENV.fetch('DATABASE_URL', 'sqlite3:data/fluentd_server.db')
10
14
  end
11
15
 
16
+ def self.job_dir
17
+ ENV.fetch('JOB_DIR', 'jobs')
18
+ end
19
+
12
20
  def self.log_path
13
21
  ENV.fetch('LOG_PATH', 'STDOUT')
14
22
  end
@@ -24,4 +32,8 @@ module FluentdServer::Config
24
32
  def self.log_shift_size
25
33
  ENV.fetch('LOG_SHIFT_SIZE', '1048576')
26
34
  end
35
+
36
+ def self.task_max_num
37
+ ENV.fetch('TASK_MAX_NUM', '20').to_i
38
+ end
27
39
  end
@@ -1,7 +1,8 @@
1
1
  require 'sinatra/decorator'
2
+ require 'fluentd_server/web_helper'
2
3
 
3
4
  class PostDecorator < Sinatra::Decorator::Base
4
- include Rack::Utils
5
+ include FluentdServer::WebHelper
5
6
 
6
7
  def success_message
7
8
  'Success!'
@@ -17,5 +18,26 @@ class PostDecorator < Sinatra::Decorator::Base
17
18
  namespace = OpenStruct.new(locals)
18
19
  ERB.new(self.body, nil, "-").result(namespace.instance_eval { binding })
19
20
  end
21
+
22
+ def link_to
23
+ %Q[<a href="#{h("/posts/#{self.id}/edit")}">
24
+ <span class="label label-success">&nbsp;</span> ##{h(self.id)} #{h(self.name)}
25
+ </a>]
26
+ end
27
+
28
+ def create_button
29
+ %Q[<div style="padding: 0 0 10px 10px;font-variant:small-caps;">
30
+ <a class="btn btn-default #{active_if(self.new?)}" role="button"
31
+ href="/posts/create">Create Config</a></div>]
32
+ end
20
33
  end
21
34
 
35
+ class TaskDecorator < Sinatra::Decorator::Base
36
+ include FluentdServer::WebHelper
37
+
38
+ def link_to
39
+ %Q[<a href="#{h("/tasks/#{self.id}")}">
40
+ <span class="label label-success">&nbsp;</span> ##{h(self.id)} #{h(self.name)}
41
+ </a>]
42
+ end
43
+ end
@@ -3,6 +3,8 @@ require 'sinatra/activerecord'
3
3
  require 'fluentd_server/config'
4
4
  require 'fluentd_server/logger'
5
5
 
6
+ ROOT = File.expand_path('../../..', __FILE__)
7
+
6
8
  configure do
7
9
  set :show_exceptions, true
8
10
  ActiveRecord::Base.logger = FluentdServer.logger
@@ -29,6 +31,20 @@ end
29
31
  configure :test do
30
32
  ActiveRecord::Base.establish_connection(
31
33
  :adapter => 'sqlite3',
32
- :database => ':memory'
34
+ :database => ':memory:'
33
35
  )
34
36
  end
37
+
38
+ # Configure DelayedJob
39
+ require 'delayed_job'
40
+ configure do
41
+ Delayed::Worker.backend = :active_record # This defines Delayed::Job model
42
+ Delayed::Worker.logger = FluentdServer.logger
43
+ end
44
+
45
+ configure :development, :test do
46
+ Delayed::Worker.destroy_failed_jobs = true
47
+ Delayed::Worker.sleep_delay = 5
48
+ Delayed::Worker.max_attempts = 5
49
+ Delayed::Worker.max_run_time = 5.minutes
50
+ end
@@ -1,10 +1,68 @@
1
1
  require 'sinatra/activerecord'
2
2
  require 'sinatra/decorator'
3
- require 'fluentd_server/environments'
3
+ require 'fluentd_server/environment'
4
+ require 'fluentd_server/task_runner'
5
+ require 'acts_as_file'
6
+
7
+ class Delayed::Job < ActiveRecord::Base; end
4
8
 
5
9
  class Post < ActiveRecord::Base
6
10
  include Sinatra::Decorator::Decoratable
11
+ include FluentdServer::Logger
7
12
 
8
13
  validates :name, presence: true
9
14
  validates :body, presence: true
15
+
16
+ if FluentdServer::Config.data_dir
17
+ include ActsAsFile
18
+
19
+ def filename
20
+ File.join(FluentdServer::Config.data_dir, "#{self.name}.erb") if self.name
21
+ end
22
+
23
+ acts_as_file :body => self.instance_method(:filename)
24
+ end
25
+
26
+ def new?
27
+ self.id.nil?
28
+ end
29
+ end
30
+
31
+ class Task < ActiveRecord::Base
32
+ include Sinatra::Decorator::Decoratable
33
+ include FluentdServer::Logger
34
+ include ActsAsFile
35
+ include TaskRunner # task runnable codes are here
36
+
37
+ def filename
38
+ prefix = "#{self.id.to_s.rjust(4, '0')}" if self.id
39
+ File.join(FluentdServer::Config.job_dir, "#{prefix}_result.txt") if prefix
40
+ end
41
+
42
+ acts_as_file :body => self.instance_method(:filename)
43
+
44
+ def finished?
45
+ !self.exit_code.nil?
46
+ end
47
+
48
+ def successful?
49
+ self.finished? and self.exit_code == 0
50
+ end
51
+
52
+ def error?
53
+ self.finished? and self.exit_code != 0
54
+ end
55
+
56
+ def new?
57
+ self.id.nil?
58
+ end
59
+
60
+ def self.create_and_delete(*args)
61
+ created = self.create(*args)
62
+ if self.count > FluentdServer::Config.task_max_num
63
+ oldest = self.first
64
+ oldest.destroy_with_file
65
+ end
66
+ created
67
+ end
10
68
  end
@@ -0,0 +1,88 @@
1
+ # included by class Task
2
+ module TaskRunner
3
+ def self.included(klass)
4
+ require 'fileutils'
5
+ klass.extend(ClassMethods)
6
+ end
7
+
8
+ def restart
9
+ system(write_event_header('restart'))
10
+ cmd = serf_event('restart')
11
+ logger.debug "run #{cmd}"
12
+ self.exit_code = system(cmd)
13
+ self.save!
14
+ end
15
+
16
+ def status
17
+ system(write_query_header('status'))
18
+ self.delay.delayed_status
19
+ end
20
+
21
+ def delayed_status
22
+ cmd = serf_query('status')
23
+ logger.debug "run #{cmd}"
24
+ self.exit_code = system(cmd)
25
+ self.save!
26
+ end
27
+
28
+ def configtest
29
+ system(write_query_header('configtest'))
30
+ self.delay.delayed_configtest
31
+ end
32
+
33
+ def delayed_configtest
34
+ cmd = serf_query('configtest')
35
+ logger.debug "run #{cmd}"
36
+ self.exit_code = system(cmd)
37
+ self.save!
38
+ end
39
+
40
+ ## delayed_job hooks
41
+
42
+ def before(job)
43
+ @job = job
44
+ end
45
+
46
+ def failure
47
+ logger.warn "job #{@job.attributes} failed"
48
+ end
49
+
50
+ ## helpers
51
+
52
+ def write_event_header(cmd)
53
+ "echo '$ serf event td-agent-#{cmd}' > #{self.filename}"
54
+ end
55
+
56
+ def write_query_header(cmd)
57
+ "echo '$ serf query td-agent-#{cmd}' > #{self.filename}"
58
+ end
59
+
60
+ # serf event works asynchronously, so it does not take time
61
+ def serf_event(cmd)
62
+ "#{self.class.serf_path} event td-agent-#{cmd} >> #{self.filename} 2>&1"
63
+ end
64
+
65
+ # serf query works synchronously, so it takes time
66
+ def serf_query(cmd)
67
+ "#{self.class.serf_path} query td-agent-#{cmd} >> #{self.filename} 2>&1"
68
+ end
69
+
70
+ module ClassMethods
71
+ def serf_path
72
+ @serf_path ||= "#{find_path_gem('serf-td-agent')}/bin/serf"
73
+ end
74
+
75
+ # from gem-path gem
76
+ def find_path_gem name
77
+ path_gem = Gem.path.find do |base|
78
+ path_gem = $LOAD_PATH.find do |path|
79
+ path_gem = path[%r{#{base}/gems/#{name}\-[^/-]+/}]
80
+ break path_gem if path_gem
81
+ end
82
+ break path_gem if path_gem
83
+ end
84
+ path_gem[0...-1] if path_gem
85
+ end
86
+ end
87
+ end
88
+
@@ -1,3 +1,3 @@
1
1
  module FluentdServer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end