launch_job 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/launch_job.rb +122 -0
- metadata +80 -0
data/lib/launch_job.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# launch job
|
2
|
+
# Author: Jackie.ju@gmail.com
|
3
|
+
|
4
|
+
# launch a job with a function, interval
|
5
|
+
# sometime job need run very freqently and log cost too much compared with running time, in this case should set needlog=false
|
6
|
+
require 'rubygems'
|
7
|
+
require 'ps_grep'
|
8
|
+
|
9
|
+
def launch_job_with_hash(hash)
|
10
|
+
# symbolize
|
11
|
+
hash = hash.inject({}){|m,(k,v)| m[k.to_sym] = v; m}
|
12
|
+
launch_job(hash[:fn], hash[:ms], hash[:needlog], hash[:before], hash[:error],
|
13
|
+
hash[:unique], hash[:max_error], hash[:name])
|
14
|
+
end
|
15
|
+
def launch_job(fn, ms=nil, needlog=true, fn_before_launch=nil, error_handler=nil, unique=false, max_error=nil, name=nil)
|
16
|
+
if fn.class == Hash
|
17
|
+
return launch_job_with_hash(fn)
|
18
|
+
end
|
19
|
+
ms = 1 if ms == nil
|
20
|
+
|
21
|
+
if unique == true && name == nil
|
22
|
+
p "unique job must have a name!"
|
23
|
+
return
|
24
|
+
else
|
25
|
+
ar = find_process("_"+name+"_")
|
26
|
+
p ar
|
27
|
+
if ar && ar.size > 0
|
28
|
+
p "Unique job named #{name} already running"
|
29
|
+
return
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
fn= fn.to_s
|
34
|
+
Process.detach fork{
|
35
|
+
p("Job process #{$$}: #{fn.to_s} ")
|
36
|
+
# $PROGRAM_NAME is equal to $0
|
37
|
+
|
38
|
+
# sometime the OS limite the length of $0 to its orginal size, so suggest not give name with length > 19
|
39
|
+
$0 ="_#{name}_" if name
|
40
|
+
# $progname = $0
|
41
|
+
# alias $PROGRAM_NAME $0
|
42
|
+
# alias $0 $progname
|
43
|
+
|
44
|
+
# trace_var(:$0) {|val| $PROGRAM_NAME = val} # update for ps
|
45
|
+
|
46
|
+
|
47
|
+
Object.send(fn_before_launch.to_s) if fn_before_launch
|
48
|
+
|
49
|
+
count = 0
|
50
|
+
errcount = 0
|
51
|
+
tm = 0
|
52
|
+
|
53
|
+
while(1)
|
54
|
+
tf_start = Time.now.to_f
|
55
|
+
if needlog
|
56
|
+
_tm = Time.now
|
57
|
+
if _tm.to_i - tm.to_i > 600
|
58
|
+
p "[#{_tm}]I'm running.(count=#{count}, err #{errcount})"
|
59
|
+
tm = _tm
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
begin
|
64
|
+
|
65
|
+
Object.send(fn.to_s)
|
66
|
+
|
67
|
+
rescue Exception=>e
|
68
|
+
|
69
|
+
|
70
|
+
p "!!Exception when executing job [#{fn.to_s}]: #{e.inspect}"
|
71
|
+
|
72
|
+
if error_handler
|
73
|
+
r = Object.send(error_handler.to_s, e)
|
74
|
+
if r
|
75
|
+
p "Job stop"
|
76
|
+
exit
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
errcount+=1
|
81
|
+
if max_error !=nil && errcount> max_error
|
82
|
+
p "Error exceed max error number , job quit"
|
83
|
+
exit
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
count+=1
|
89
|
+
|
90
|
+
int = ms.to_f - (Time.now.to_f - tf_start)
|
91
|
+
int = 0.001 if int <= 0
|
92
|
+
p "sleep #{int}" if needlog
|
93
|
+
sleep(int)
|
94
|
+
|
95
|
+
p "after sleep"
|
96
|
+
end
|
97
|
+
}
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
=begin
|
102
|
+
# test
|
103
|
+
|
104
|
+
def start
|
105
|
+
p "start job"
|
106
|
+
# throw "fafas"
|
107
|
+
end
|
108
|
+
def error(e)
|
109
|
+
p "error:#{e}"
|
110
|
+
return true
|
111
|
+
end
|
112
|
+
def before
|
113
|
+
p "before job"
|
114
|
+
end
|
115
|
+
launch_job({
|
116
|
+
:fn=>"start",
|
117
|
+
:error=>"error",
|
118
|
+
:before=>"before",
|
119
|
+
:unique=>true,
|
120
|
+
:name=>"xx"
|
121
|
+
})
|
122
|
+
=end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: launch_job
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ju Weihua (Jackie Ju)
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2016-01-05 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ps_grep
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Help launch a ruby backend job!
|
36
|
+
email: jackie.ju@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/launch_job.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://rubygems.org/gems/launch_job
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.7
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Help launch a ruby backend job!
|
79
|
+
test_files: []
|
80
|
+
|