Parent

BdrbJobQueue

Model for storing jobs/tasks persisted to the database

Public Class Methods

find_next(worker_name,worker_key = nil) click to toggle source

find next task from the table

    # File lib/backgroundrb/bdrb_job_queue.rb, line 6
 6:   def self.find_next(worker_name,worker_key = nil)
 7:     returned_job = nil
 8:     transaction do
 9:       unless worker_key
10:         #use ruby time stamps for time calculations as db might have different times than what is calculated by ruby/rails
11:         t_job = find(:first,:conditions => [" worker_name = ? AND taken = ? AND scheduled_at <= ? ", worker_name, 0, Time.now.utc ],:lock => true)
12:       else
13:         t_job = find(:first,:conditions => [" worker_name = ? AND taken = ? AND worker_key = ? AND scheduled_at <= ? ", worker_name, 0, worker_key, Time.now.utc ],:lock => true)
14:       end
15:       if t_job
16:         t_job.taken = 1
17:         t_job.started_at = Time.now.utc
18:         t_job.save
19:         returned_job = t_job
20:       end
21:     end
22:     returned_job
23:   end
insert_job(options = { }) click to toggle source

insert a new job for processing. jobs added will be automatically picked by the appropriate worker

    # File lib/backgroundrb/bdrb_job_queue.rb, line 36
36:   def self.insert_job(options = { })
37:     transaction do
38:       options.merge!(:submitted_at => Time.now.utc,:finished => 0,:taken => 0)
39:       t_job = new(options)
40:       t_job.save
41:     end
42:   end
remove_job(options = { }) click to toggle source

remove a job from table

    # File lib/backgroundrb/bdrb_job_queue.rb, line 45
45:   def self.remove_job(options = { })
46:     transaction do
47:       t_job_id = find(:first, :conditions => options.merge(:finished => 0,:taken => 0),:lock => true)
48:       delete(t_job_id)
49:     end
50:   end

Public Instance Methods

finish!() click to toggle source

Mark a job as finished

    # File lib/backgroundrb/bdrb_job_queue.rb, line 53
53:   def finish!
54:     self.class.transaction do
55:       self.finished = 1
56:       self.finished_at = Time.now.utc
57:       self.job_key = "finished_#{Time.now.utc.to_i}_#{job_key}"
58:       self.save
59:     end
60:     Thread.current[:persistent_job_id] = nil
61:     Thread.current[:job_key] = nil
62:     nil
63:   end
release_job() click to toggle source

release a job and mark it to be unfinished and free. useful, if inside a worker, processing of this job failed and you want it to process later

    # File lib/backgroundrb/bdrb_job_queue.rb, line 27
27:   def release_job
28:     self.class.transaction do
29:       self.taken = 0
30:       self.started_at = nil
31:       self.save
32:     end
33:   end

secsequence

--- SEC00016

seccomment

--- ""

method_list

--- 
- methods: 
  - visibility: public
    aref: M000005
    name: find_next
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/backgroundrb/bdrb_job_queue.rb, line 6</span>\n 6:   <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">find_next</span>(<span class=\"ruby-identifier\">worker_name</span>,<span class=\"ruby-identifier\">worker_key</span> = <span class=\"ruby-keyword kw\">nil</span>)\n 7:     <span class=\"ruby-identifier\">returned_job</span> = <span class=\"ruby-keyword kw\">nil</span>\n 8:     <span class=\"ruby-identifier\">transaction</span> <span class=\"ruby-keyword kw\">do</span>\n 9:       <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-identifier\">worker_key</span>\n\
      10:         <span class=\"ruby-comment cmt\">#use ruby time stamps for time calculations as db might have different times than what is calculated by ruby/rails</span>\n\
      11:         <span class=\"ruby-identifier\">t_job</span> = <span class=\"ruby-identifier\">find</span>(<span class=\"ruby-identifier\">:first</span>,<span class=\"ruby-identifier\">:conditions</span> =<span class=\"ruby-operator\">&gt;</span> [<span class=\"ruby-value str\">&quot; worker_name = ? AND taken = ? AND scheduled_at &lt;= ? &quot;</span>, <span class=\"ruby-identifier\">worker_name</span>, <span class=\"ruby-value\">0</span>, <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">utc</span> ],<span class=\"ruby-identifier\">:lock</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-keyword kw\">true</span>)\n\
      12:       <span class=\"ruby-keyword kw\">else</span>\n\
      13:         <span class=\"ruby-identifier\">t_job</span> = <span class=\"ruby-identifier\">find</span>(<span class=\"ruby-identifier\">:first</span>,<span class=\"ruby-identifier\">:conditions</span> =<span class=\"ruby-operator\">&gt;</span> [<span class=\"ruby-value str\">&quot; worker_name = ? AND taken = ? AND worker_key = ? AND scheduled_at &lt;= ? &quot;</span>, <span class=\"ruby-identifier\">worker_name</span>, <span class=\"ruby-value\">0</span>, <span class=\"ruby-identifier\">worker_key</span>, <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">utc</span> ],<span class=\"ruby-identifier\">:lock</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-keyword kw\">true</span>)\n\
      14:       <span class=\"ruby-keyword kw\">end</span>\n\
      15:       <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">t_job</span>\n\
      16:         <span class=\"ruby-identifier\">t_job</span>.<span class=\"ruby-identifier\">taken</span> = <span class=\"ruby-value\">1</span>\n\
      17:         <span class=\"ruby-identifier\">t_job</span>.<span class=\"ruby-identifier\">started_at</span> = <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">utc</span>\n\
      18:         <span class=\"ruby-identifier\">t_job</span>.<span class=\"ruby-identifier\">save</span>\n\
      19:         <span class=\"ruby-identifier\">returned_job</span> = <span class=\"ruby-identifier\">t_job</span>\n\
      20:       <span class=\"ruby-keyword kw\">end</span>\n\
      21:     <span class=\"ruby-keyword kw\">end</span>\n\
      22:     <span class=\"ruby-identifier\">returned_job</span>\n\
      23:   <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      find next task from the table
      </p>
    params: (worker_name,worker_key = nil)
  - visibility: public
    aref: M000007
    name: insert_job
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/backgroundrb/bdrb_job_queue.rb, line 36</span>\n\
      36:   <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">insert_job</span>(<span class=\"ruby-identifier\">options</span> = { })\n\
      37:     <span class=\"ruby-identifier\">transaction</span> <span class=\"ruby-keyword kw\">do</span>\n\
      38:       <span class=\"ruby-identifier\">options</span>.<span class=\"ruby-identifier\">merge!</span>(<span class=\"ruby-identifier\">:submitted_at</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">utc</span>,<span class=\"ruby-identifier\">:finished</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-value\">0</span>,<span class=\"ruby-identifier\">:taken</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-value\">0</span>)\n\
      39:       <span class=\"ruby-identifier\">t_job</span> = <span class=\"ruby-identifier\">new</span>(<span class=\"ruby-identifier\">options</span>)\n\
      40:       <span class=\"ruby-identifier\">t_job</span>.<span class=\"ruby-identifier\">save</span>\n\
      41:     <span class=\"ruby-keyword kw\">end</span>\n\
      42:   <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      insert a new job for processing. jobs added will be automatically picked by
      the appropriate worker
      </p>
    params: (options = { })
  - visibility: public
    aref: M000008
    name: remove_job
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/backgroundrb/bdrb_job_queue.rb, line 45</span>\n\
      45:   <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">remove_job</span>(<span class=\"ruby-identifier\">options</span> = { })\n\
      46:     <span class=\"ruby-identifier\">transaction</span> <span class=\"ruby-keyword kw\">do</span>\n\
      47:       <span class=\"ruby-identifier\">t_job_id</span> = <span class=\"ruby-identifier\">find</span>(<span class=\"ruby-identifier\">:first</span>, <span class=\"ruby-identifier\">:conditions</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-identifier\">options</span>.<span class=\"ruby-identifier\">merge</span>(<span class=\"ruby-identifier\">:finished</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-value\">0</span>,<span class=\"ruby-identifier\">:taken</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-value\">0</span>),<span class=\"ruby-identifier\">:lock</span> =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-keyword kw\">true</span>)\n\
      48:       <span class=\"ruby-identifier\">delete</span>(<span class=\"ruby-identifier\">t_job_id</span>)\n\
      49:     <span class=\"ruby-keyword kw\">end</span>\n\
      50:   <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      remove a job from table
      </p>
    params: (options = { })
  category: Class
  type: Public
- methods: 
  - visibility: public
    aref: M000009
    name: finish!
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/backgroundrb/bdrb_job_queue.rb, line 53</span>\n\
      53:   <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">finish!</span>\n\
      54:     <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>.<span class=\"ruby-identifier\">transaction</span> <span class=\"ruby-keyword kw\">do</span>\n\
      55:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">finished</span> = <span class=\"ruby-value\">1</span>\n\
      56:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">finished_at</span> = <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">utc</span>\n\
      57:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">job_key</span> = <span class=\"ruby-node\">&quot;finished_#{Time.now.utc.to_i}_#{job_key}&quot;</span>\n\
      58:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">save</span>\n\
      59:     <span class=\"ruby-keyword kw\">end</span>\n\
      60:     <span class=\"ruby-constant\">Thread</span>.<span class=\"ruby-identifier\">current</span>[<span class=\"ruby-identifier\">:persistent_job_id</span>] = <span class=\"ruby-keyword kw\">nil</span>\n\
      61:     <span class=\"ruby-constant\">Thread</span>.<span class=\"ruby-identifier\">current</span>[<span class=\"ruby-identifier\">:job_key</span>] = <span class=\"ruby-keyword kw\">nil</span>\n\
      62:     <span class=\"ruby-keyword kw\">nil</span>\n\
      63:   <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Mark a job as finished
      </p>
    params: ()
  - visibility: public
    aref: M000006
    name: release_job
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/backgroundrb/bdrb_job_queue.rb, line 27</span>\n\
      27:   <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">release_job</span>\n\
      28:     <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>.<span class=\"ruby-identifier\">transaction</span> <span class=\"ruby-keyword kw\">do</span>\n\
      29:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">taken</span> = <span class=\"ruby-value\">0</span>\n\
      30:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">started_at</span> = <span class=\"ruby-keyword kw\">nil</span>\n\
      31:       <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">save</span>\n\
      32:     <span class=\"ruby-keyword kw\">end</span>\n\
      33:   <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      release a job and mark it to be unfinished and free. useful, if inside a
      worker, processing of this job failed and you want it to process later
      </p>
    params: ()
  category: Instance
  type: Public

sectitle

--- 

[Validate]

Generated with the Darkfish Rdoc Generator.