Class: Resque::Plugins::JobHistory::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/plugins/job_history/cleaner.rb

Overview

JobHistory cleanup functions to allow the user to cleanup Redis for histories.

Class Method Summary collapse

Class Method Details

.clean_all_old_running_jobsObject



9
10
11
12
13
# File 'lib/resque/plugins/job_history/cleaner.rb', line 9

def clean_all_old_running_jobs
  job_classes.each do |class_name|
    Resque::Plugins::JobHistory::HistoryDetails.new(class_name).clean_old_running_jobs
  end
end

.fixup_all_keysObject



15
16
17
18
19
20
# File 'lib/resque/plugins/job_history/cleaner.rb', line 15

def fixup_all_keys
  job_classes.each do |class_name|
    fixup_job_keys class_name
  end
  fixup_linear_keys
end

.fixup_job_keys(class_name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/resque/plugins/job_history/cleaner.rb', line 22

def fixup_job_keys(class_name)
  keys = unknown_job_keys(class_name)

  keys.each do |stranded_key|
    Resque::Plugins::JobHistory::Job.new(class_name, stranded_key).purge
    del_key(stranded_key, "Stranded job key deleted")
  end
end

.fixup_linear_keysObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/resque/plugins/job_history/cleaner.rb', line 31

def fixup_linear_keys
  details  = Resque::Plugins::JobHistory::HistoryDetails.new("")
  hash_key = details.linear_jobs.job_classes_key

  classes = details.redis.hgetall hash_key
  job_ids = details.linear_jobs.job_ids

  classes.keys.each do |job_id|
    next if job_ids.include?(job_id)

    Resque.logger.warn("deleting missing job class - #{job_id}")
    details.redis.hdel hash_key, job_id
  end
end

.purge_all_jobsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resque/plugins/job_history/cleaner.rb', line 46

def purge_all_jobs
  job_classes.each do |class_name|
    purge_class class_name
  end

  purge_linear_history

  del_key(Resque::Plugins::JobHistory::HistoryDetails.job_history_key, "Purging job_history_key")

  redis.keys("*").each do |key|
    del_key(key, "Purging unknown key")
  end
end

.purge_class(class_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/resque/plugins/job_history/cleaner.rb', line 83

def purge_class(class_name)
  return if similar_name?(class_name)

  details = Resque::Plugins::JobHistory::HistoryDetails.new("class_name")
  details.running_jobs.jobs.each(&:purge)
  details.finished_jobs.jobs.each(&:purge)

  class_keys(class_name).each do |job_key|
    del_key(job_key, "Purging job key")
  end
end

.purge_invalid_jobsObject



75
76
77
78
79
80
81
# File 'lib/resque/plugins/job_history/cleaner.rb', line 75

def purge_invalid_jobs
  job_classes.each do |class_name|
    next if Resque::Plugins::JobHistory::HistoryDetails.new(class_name).class_name_valid?

    purge_class(class_name)
  end
end

.purge_linear_historyObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/resque/plugins/job_history/cleaner.rb', line 60

def purge_linear_history
  linear_jobs = Resque::Plugins::JobHistory::JobList.new.linear_jobs

  fixup_linear_keys

  linear_jobs.job_ids.each do |job_id|
    linear_jobs.remove_job(job_id)
  end

  list_keys = redis.keys("job_history..*")
  list_keys.each do |key|
    del_key(key, "Purging unknown linear history key")
  end
end

.similar_name?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/resque/plugins/job_history/cleaner.rb', line 95

def similar_name?(class_name)
  job_classes.any? do |job_name|
    job_name != class_name && job_name[0..class_name.length - 1] == class_name
  end
end