appjob 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,114 @@
1
- require 'appjob/version'
2
-
3
1
  class Appjob
4
2
  include Singleton
5
- EVERY_PATH = File.join(Rails.root,'config','every.rb')
6
3
 
7
- def self.job(at, to_job)
4
+ EVERY_FILE = File.join(Rails.root,'config','every.rb')
5
+ FLAG_FILE = File.join(Rails.root,'tmp','appjob_flag')
6
+ CMD_FILE = File.join(Rails.root,'script','appjob')
7
+
8
+ @threads = {}
9
+ @monitor = false
10
+
11
+ def self.configure
12
+ raise "Appjob: No rails app detected" unless
13
+ defined? Rails and RAILS_ENV and RAILS_ROOT
14
+ Thread.abort_on_exception = true
15
+ FileUtils.cp(File.join(File.dirname(__FILE__),'appjob','every.rb'), EVERY_FILE)
16
+ FileUtils.cp(File.join(File.dirname(__FILE__),'appjob','appjob'), CMD_FILE)
17
+ log "succefully configured" if configured?
18
+ end
19
+
20
+ def self.configured?
21
+ (File.exist?(EVERY_FILE) and
22
+ File.exist?(CMD_FILE))
23
+ end
24
+
25
+ def self.every period, name=nil
8
26
  t = Thread.new do
9
- loop { sleep(at.to_i); eval(to_job) }
27
+ loop { sleep(period.to_i); yield }
10
28
  end
29
+ t[:name] = name if name
30
+ @threads[t[:name]] = t
11
31
  end
12
32
 
13
- def self.run
14
- if defined? Rails and RAILS_ENV and RAILS_ROOT
15
- log 'Init ok, env='+Rails.env.to_s
16
- Thread.abort_on_exception = true
17
- FileUtils.cp(File.join(File.dirname(__FILE__),'appjob','every.rb'), EVERY_PATH) unless File.exist? EVERY_PATH
18
- eval(File.read(EVERY_PATH))
33
+ def self.start
34
+ if @threads.size < 1
35
+ configure unless configured?
36
+ log "Init ok, RAILS_ENV=#{Rails.env.to_s}"
37
+ eval File.read(EVERY_FILE)
38
+ monitor unless @monitor
19
39
  else
20
- puts 'Appjob: No rails app detected'
40
+ log "Already started"
21
41
  end
22
42
  end
23
43
 
24
- def self.every attrs, &block
25
- job(attrs, (instance_eval &block if block_given?))
44
+ def self.stop(name=nil)
45
+ if @threads.size > 0
46
+ if name.nil?
47
+ list = @threads
48
+ else
49
+ if @threads.has_key?(name)
50
+ list = {}
51
+ list[name]=@threads.fetch(name)
52
+ else
53
+ log "No victims with name=#{name}"
54
+ return
55
+ end
56
+ end
57
+
58
+ list.each do |name, t|
59
+ if t.is_a?(Thread)
60
+ t.kill
61
+ list.delete(name)
62
+ @threads.delete(name) if list.empty?
63
+ log "task #{name} killed"
64
+ else
65
+ log "task #{name} is not a Thread "
66
+ end
67
+ end
68
+ else
69
+ log "nothing to stop"
70
+ end
71
+ end
72
+
73
+ def self.list
74
+ @threads
26
75
  end
27
76
 
28
77
  def self.log(message)
29
- RAILS_DEFAULT_LOGGER.warn("** [Appjob] #{message}")
78
+ RAILS_DEFAULT_LOGGER.info("[appjob] #{message}")
79
+ end
80
+
81
+ def self.monitor
82
+ @monitor = Thread.new do
83
+ loop {
84
+ sleep 2;
85
+ if get_command
86
+ todo = get_command.split
87
+ case todo[0]
88
+ when 'start'
89
+ start
90
+ clean_command
91
+ when 'stop'
92
+ if todo[1]
93
+ stop(todo[1])
94
+ else
95
+ stop
96
+ end
97
+ clean_command
98
+ end
99
+ end
100
+ }
101
+ end
102
+ end
103
+
104
+ def self.get_command
105
+ cmd = File.exist?(FLAG_FILE) ? File.read(FLAG_FILE) : nil
106
+ cmd.strip unless cmd.nil?
107
+ end
108
+
109
+ def self.clean_command
110
+ File.delete(FLAG_FILE) if File.exist?(FLAG_FILE)
30
111
  end
31
- end
112
+ end
32
113
 
33
- Appjob.run
114
+ Appjob.start
@@ -0,0 +1,13 @@
1
+ #!/home/programmer/.rvm/bin/ruby
2
+
3
+ case ARGV[0]
4
+ when 'start'
5
+ File.open(File.join(File.dirname(__FILE__),'..','tmp','appjob_flag'),'w') do |f|
6
+ f << 'start'
7
+ end
8
+ when 'stop'
9
+ File.open(File.join(File.dirname(__FILE__),'..','tmp','appjob_flag'),'w') do |f|
10
+ postfix = ARGV[1].nil? ? '' : ARGV[1]
11
+ f << 'stop '+postfix
12
+ end
13
+ end
@@ -1,4 +1,20 @@
1
- every 10.seconds do 'log "10 sec task"' end
2
- every 15.seconds do 'log "15 sec task"' end
1
+ #fixed appjob task, started once at app run
3
2
  log "find me at app/config/every.rb"
4
3
 
4
+ #repeated appjob-task, first argument - repeat interval, second - task name(must be unique)
5
+ every 3.seconds, "live" do
6
+ log "Live! 3 sec"
7
+ end
8
+
9
+ every 20.seconds, "killer" do
10
+ log "killer 20 sec"
11
+ end
12
+
13
+ every 6.seconds, "victim" do
14
+ log "victim 6 sec"
15
+ end
16
+
17
+ every 7.seconds, "list" do
18
+ log list
19
+ log "list task every 7 sec"
20
+ end
@@ -1,3 +1,3 @@
1
1
  class Appjob
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appjob
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 9
10
- version: 0.0.9
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeks
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-26 00:00:00 +04:00
18
+ date: 2011-11-02 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -35,6 +35,7 @@ files:
35
35
  - Rakefile
36
36
  - appjob.gemspec
37
37
  - lib/appjob.rb
38
+ - lib/appjob/appjob
38
39
  - lib/appjob/every.rb
39
40
  - lib/appjob/version.rb
40
41
  has_rdoc: true