Class: Crocoduck::Job

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/crocoduck/job.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Job

Returns a new instance of Job.



36
37
38
# File 'lib/crocoduck/job.rb', line 36

def initialize(entry)
  @entry = entry
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/crocoduck/job.rb', line 15

def description
  @description
end

Instance Attribute Details

#entryObject

Returns the value of attribute entry.



34
35
36
# File 'lib/crocoduck/job.rb', line 34

def entry
  @entry
end

Class Method Details

.init_with_id(entry_id) ⇒ Object

A convienance initializer that returns a Crocoduck::Job instance with its entry object ready to go.



28
29
30
# File 'lib/crocoduck/job.rb', line 28

def self.init_with_id(entry_id)
  new(Entry.new entry_id)
end

.perform(entry_id) ⇒ Object

“perform“ is the method called by Resque. A Crocoduck job only expects an “entry_id“ corresponding to a record in your Mongo store. An “Entry“ is instantiated with said “entry_id“ and passed to a new instance of this job and run is called on it.



22
23
24
# File 'lib/crocoduck/job.rb', line 22

def self.perform(entry_id)
  init_with_id(entry_id).run
end

Instance Method Details

#cleanupObject

This method will always be called, regardless of the failure or success of your job.



74
75
76
77
# File 'lib/crocoduck/job.rb', line 74

def cleanup
  entry.close
  logger.info "Job cleaned up"
end

#do_workObject

The “do_work“ method should be overridden to do some kind of work on the stored entry object.



42
43
44
45
46
47
48
49
50
51
# File 'lib/crocoduck/job.rb', line 42

def do_work
  logger.info "Starting work"
  # Do Something with entry
  # entry.update "derp", "herp"
  logger.info entry["url"]
  # shorturl = shorturl.generate @entry.url
  # store.update entry_id, 'shorturl', shorturl
  # store.update entry_id, 'shorturl_status, job_status
  logger.info "Ending work"
end

#finishedObject

This method will be called once “do_work“ has finished successfully. Do anything you’d need to do once the processing was finished properly (save out your entry, update stats, et cetera).



68
69
70
# File 'lib/crocoduck/job.rb', line 68

def finished
  logger.info "Job finished successfully"
end

#handle_exception(e) ⇒ Object

If you job failed, you can do something interesting here. Generally you will want to ultimately raise the exception so Resque can track it.



55
56
57
# File 'lib/crocoduck/job.rb', line 55

def handle_exception(e)
  raise e
end

#runObject

The “run“ method is a thin wrapper around “do_work“ which lets us do some setup, benchmark the work we’ll do, cleanly handle exceptions if thrown by the “do_work“ call, and clean up our store and entry on success.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/crocoduck/job.rb', line 83

def run
  setup
  # The job will not process anything unless our datastore has enough
  # information to connect and if a valid entry object could be fetched
  # from the store.
  return unless entry.setup?
  benchmark :info, "Running job" do
    do_work
  end
# Exception handling is parceled out to ``Job`` methods you can override
# to handle cleanup specific to your task.
rescue Exception => e
  handle_exception e
else
  finished
ensure
  cleanup
end

#setupObject

This method will be called immediately before sanity checks and before “do_work“ is called.



61
62
63
# File 'lib/crocoduck/job.rb', line 61

def setup
  logger.info "Job is setup"
end