beanworker 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.0.2 2011-10-24
2
+
3
+ * 1 major enhancement:
4
+ * added 'beanworker' in bin
5
+ * 1 minor enhancement:
6
+ added DSL sample to README
7
+
1
8
  === 0.0.1 2011-10-20
2
9
 
3
10
  * 1 major enhancement:
data/Manifest.txt CHANGED
@@ -3,4 +3,5 @@ Manifest.txt
3
3
  README.rdoc
4
4
  Rakefile
5
5
  lib/beanworker.rb
6
- lib/beanworker/worker.rb
6
+ lib/beanworker/worker.rb
7
+ bin/beanworker
data/README.rdoc CHANGED
@@ -21,6 +21,40 @@ beanstalk-client
21
21
  -P|--pid - path to the directory, where pid-file(s) will be saved, default is current directory
22
22
 
23
23
  == DSL
24
+ jobs.rb:
25
+
26
+ class SomeWorker
27
+ extend Beanworker::Worker
28
+
29
+ class << self
30
+ def some_job(args={})
31
+ do_somethin_slow_with(args)
32
+ end
33
+
34
+ def another_job(args={})
35
+ do_something
36
+ end
37
+ end
38
+ end
39
+
40
+ class AnotherWorker
41
+ extend Beanworker::Worker
42
+
43
+ class << self
44
+ def scheduled_job(args={})
45
+ do_somethin_slow_with(args)
46
+ end
47
+ end
48
+
49
+ schedule :scheduled_job, 2*24*3600, at: '04:30', timeout: 3600, { param1: 'some_value' }
50
+ end
51
+
52
+ SlowWorker.perform 2, :some_job.
53
+ :another_job
54
+
55
+ AnotherWorker.perform 1, :scheduled_jov
56
+
57
+ == API:
24
58
 
25
59
  TODO
26
60
 
data/bin/beanworker ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ if ARGV[0] == '-h' || ARGV[0] == '--help'
3
+ abort 'beanworker <jobs.rb> <host:port|path/to/file.yml> [-d|--daemonize] [-m|--monitor] [-P|--pid <path/to/pid/dir>]'
4
+ end
5
+
6
+ if ARGV[0] == '-v' || ARGV[0] == '--version'
7
+ abort Beanworker::VERSION
8
+ end
9
+
10
+ opts = {
11
+ daemonize: ARGV.include?('-d') || ARGV.include?('--daemonize'),
12
+ monitor: ARGV.include?('-m') || ARGV.include?('--monitor'),
13
+ pid_dir: (i = ARGV.index('-P') || ARGV.index('--pid')) ? File.expand_path(ARGV[i+1], Dir.pwd) : './',
14
+ jobs_file: File.expand_path(ARGV[0], Dir.pwd)
15
+ }
16
+
17
+ conn_file = File.expand_path(ARGV[1], Dir.pwd)
18
+ File.file?(conn_file) ? opts[:connection_file] = conn_file : opts[:connection_host] = ARGV[1]
19
+
20
+ trap('INT') do
21
+ puts 'Exiting'
22
+ exit
23
+ end
24
+
25
+
26
+ require File.expand_path('../../lib/beanworker', __FILE__)
27
+ Beanworker.run opts
@@ -1,5 +1,6 @@
1
1
  #coding: utf-8
2
2
  require 'timeout'
3
+ require 'time'
3
4
  require 'pry'
4
5
 
5
6
  module Beanworker
@@ -16,6 +17,22 @@ module Beanworker
16
17
  end
17
18
  end
18
19
 
20
+ def schedule(name, frequency, opts={}, *args)
21
+ at = make_first_schedule(frequency, opts[:at])
22
+ timeout_secs = [opts[:timeout] || frequency, frequency-5].min
23
+ Thread.new do
24
+ loop do
25
+ sleep(at - Time.now)
26
+ Timeout.timeout timeout_secs do
27
+ self.send(name, *args)
28
+ end
29
+ at += frequency
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
19
36
  def get_one_job(connection)
20
37
  job = connection.reserve
21
38
  name, args = job.stats['tube'].gsub('.', '_'), job.ybody
@@ -47,5 +64,14 @@ module Beanworker
47
64
  def fork_default(val)
48
65
  @need_fork = val
49
66
  end
67
+
68
+ def make_first_schedule(frequency, at)
69
+ if at && (frequency % (24*3600) == 0)
70
+ t = Time.parse at
71
+ t > Time.now ? t : (t + 24*3600)
72
+ else
73
+ Time.now + frequency
74
+ end
75
+ end
50
76
  end
51
77
  end
data/lib/beanworker.rb CHANGED
@@ -4,7 +4,7 @@ require 'beanqueue'
4
4
  require File.dirname(__FILE__) + '/beanworker/worker'
5
5
 
6
6
  module Beanworker
7
- VERSION = '0.0.1'
7
+ VERSION = '0.0.2'
8
8
 
9
9
  class << self
10
10
  attr_accessor :connection_config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beanworker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-20 00:00:00.000000000Z
12
+ date: 2011-10-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: beanstalk-client
16
- requirement: &27436880 !ruby/object:Gem::Requirement
16
+ requirement: &15411580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *27436880
24
+ version_requirements: *15411580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: beanqueue
27
- requirement: &27436380 !ruby/object:Gem::Requirement
27
+ requirement: &15410040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *27436380
35
+ version_requirements: *15410040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hoe
38
- requirement: &27435920 !ruby/object:Gem::Requirement
38
+ requirement: &15407500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,13 +43,14 @@ dependencies:
43
43
  version: '2.12'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *27435920
46
+ version_requirements: *15407500
47
47
  description: ! 'It is a worker-side Beanstalk-based job-queueing dispatcher
48
48
 
49
49
  This is a pre-release, I will complete tests and docs in couple days'
50
50
  email:
51
51
  - gearhead@it-primorye.ru
52
- executables: []
52
+ executables:
53
+ - beanworker
53
54
  extensions: []
54
55
  extra_rdoc_files:
55
56
  - History.txt
@@ -61,6 +62,7 @@ files:
61
62
  - Rakefile
62
63
  - lib/beanworker.rb
63
64
  - lib/beanworker/worker.rb
65
+ - bin/beanworker
64
66
  - test/test_beanworker.rb
65
67
  - .gemtest
66
68
  homepage: http://github.com/GearHead90/beanworker