appjob 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/appjob.rb +38 -58
  2. data/lib/appjob/version.rb +1 -1
  3. metadata +4 -4
@@ -6,14 +6,18 @@ class Appjob
6
6
  CMD_FILE = File.join(Rails.root,'script','appjob')
7
7
 
8
8
  @threads = {}
9
- @monitor = false
9
+ @monitor = nil
10
10
 
11
11
  def self.configure
12
12
  raise "Appjob: No rails app detected" unless
13
13
  defined? Rails and RAILS_ENV and RAILS_ROOT
14
+
15
+ %w(EVERY_FILE CMD_FILE).each do |f|
16
+ FileUtils.cp(File.join(File.dirname(__FILE__),'appjob', File.basename(eval(f))), eval(f))
17
+ end
18
+
14
19
  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)
20
+
17
21
  log "succefully configured" if configured?
18
22
  end
19
23
 
@@ -22,11 +26,11 @@ class Appjob
22
26
  File.exist?(CMD_FILE))
23
27
  end
24
28
 
25
- def self.every period, name=nil
29
+ def self.every period, tname=nil
26
30
  t = Thread.new do
27
31
  loop { sleep(period.to_i); yield }
28
32
  end
29
- t[:name] = name if name
33
+ t[:name] = tname if tname
30
34
  @threads[t[:name]] = t
31
35
  end
32
36
 
@@ -35,82 +39,58 @@ class Appjob
35
39
  configure unless configured?
36
40
  log "Init ok, RAILS_ENV=#{Rails.env.to_s}"
37
41
  eval File.read(EVERY_FILE)
38
- monitor unless @monitor
42
+ monitor if @monitor.nil?
39
43
  else
40
- log "Already started"
44
+ log "already started"
41
45
  end
42
46
  end
43
47
 
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
48
+ def self.stop_all
49
+ if @threads.empty?
50
+ log "nothing to stop, task list is empty"
68
51
  else
69
- log "nothing to stop"
52
+ @threads.each_key { |tname| stop(tname) }
53
+ @threads = {}
70
54
  end
71
55
  end
72
56
 
73
- def self.list
74
- @threads
57
+ def self.stop(tname)
58
+ if @threads.has_key?(tname)
59
+ @threads.fetch(tname).kill
60
+ @threads.delete(tname)
61
+ log "task #{tname} killed"
62
+ else
63
+ log "no valid victim with name=#{tname}"
64
+ end
75
65
  end
76
66
 
77
67
  def self.log(message)
78
- RAILS_DEFAULT_LOGGER.info("[appjob] #{message}")
68
+ Rails.logger.info("[appjob] #{message}")
79
69
  end
80
70
 
81
71
  def self.monitor
82
72
  @monitor = Thread.new do
83
73
  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
74
+ sleep 1
75
+ case flag[:cmd]
76
+ when 'start' then start
77
+ when 'stop'
78
+ flag[:task] ? stop(flag[:task]) : stop_all
79
+ end unless flag.nil?
80
+ clear_flag if flag
100
81
  }
101
82
  end
102
83
  end
103
84
 
104
- def self.get_command
105
- cmd = File.exist?(FLAG_FILE) ? File.read(FLAG_FILE) : nil
106
- cmd.strip unless cmd.nil?
85
+ def self.flag
86
+ cmd = File.exist?(FLAG_FILE) ? File.read(FLAG_FILE).to_s.strip.split : nil
87
+ { :cmd=>cmd[0], :task=>cmd[1] } unless cmd.nil?
107
88
  end
108
89
 
109
- def self.clean_command
90
+ def self.clear_flag
110
91
  File.delete(FLAG_FILE) if File.exist?(FLAG_FILE)
111
92
  end
112
-
113
- #попахивает ?!
93
+ # уже лучше
114
94
  end
115
95
 
116
- Appjob.start
96
+ Appjob.start
@@ -1,3 +1,3 @@
1
1
  class Appjob
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
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: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 14
10
+ version: 0.0.14
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-11-02 00:00:00 +04:00
18
+ date: 2011-11-07 00:00:00 +04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21