Class: CronEdit::Crontab
- Inherits:
-
Object
- Object
- CronEdit::Crontab
- Defined in:
- lib/cronedit.rb
Overview
Main class that manipulates actual system cron. Additionally, it is the base class for other types of Crontab utils (FileCrontab, DummyCrontab)
Direct Known Subclasses
Class Method Summary collapse
-
.Add(anId, aDef) ⇒ Object
Add a new crontab entry definition.
-
.List ⇒ Object
List current crontab.
-
.Merge(anotherCrontab) ⇒ Object
Merges this crontab with another one (see instance method merge).
-
.Remove(*anIds) ⇒ Object
Remove a crontab entry definitions identified by anIds from current crontab (see instance method remove).
-
.Subtract(anotherCrontab) ⇒ Object
Removes crontab definitions of another crontab (see instance method subtract).
Instance Method Summary collapse
-
#add(anId, aDef) ⇒ Object
Add a new crontab entry definition.
-
#clear! ⇒ Object
Clear crontab completely and immediately.
-
#commit ⇒ Object
Merges the existing crontab with all modifications and installs the new crontab.
-
#initialize(aUser = nil) ⇒ Crontab
constructor
Use crontab for user aUser.
-
#list ⇒ Object
Read the current crontab and parse it returns a Hash (entry id or index)=>CronEntry.
-
#listFull ⇒ Object
Read the current crontab and parse it, keeping the format returns a [entires, lines] where entries = Hash (entry id or index)=>CronEntry; lines = Array of Strings of EntryPlaceholders.
-
#listRaw ⇒ Object
Lists raw content from crontab returns array of text lines.
-
#merge(aCrontab) ⇒ Object
Bulk addition/merging of crontab definitions from another Crontab (FileCrontab, DummyCrontab, or a Crontab of another user).
-
#remove(*anIds) ⇒ Object
Remove a crontab entry definitions identified by anIds.
-
#review ⇒ Object
A helper method that prints out the items to be added and removed.
-
#rollback ⇒ Object
Discards all modifications (since last commit, or creation).
-
#setIO(anInput, anOutput) ⇒ Object
Set alternative I/O for reading/writing cron entries.
-
#subtract(aCrontab) ⇒ Object
Bulk subtraction/removal of crontab definitions from another Crontab (FileCrontab, DummyCrontab, or a Crontab of another user).
Constructor Details
#initialize(aUser = nil) ⇒ Crontab
Use crontab for user aUser
63 64 65 66 67 |
# File 'lib/cronedit.rb', line 63 def initialize aUser = nil @user = aUser @opts = {:close_input=>true, :close_output=>true} rollback() end |
Class Method Details
.Add(anId, aDef) ⇒ Object
Add a new crontab entry definition. (see instance method add).
71 72 73 74 75 76 |
# File 'lib/cronedit.rb', line 71 def Add anId, aDef cm = self.new entry = cm.add anId, aDef cm.commit entry end |
.List ⇒ Object
List current crontab.
86 87 88 |
# File 'lib/cronedit.rb', line 86 def List self.new.list end |
.Merge(anotherCrontab) ⇒ Object
Merges this crontab with another one (see instance method merge).
91 92 93 94 95 |
# File 'lib/cronedit.rb', line 91 def Merge anotherCrontab cm=self.new cm.merge anotherCrontab cm.commit end |
.Remove(*anIds) ⇒ Object
Remove a crontab entry definitions identified by anIds from current crontab (see instance method remove).
79 80 81 82 83 |
# File 'lib/cronedit.rb', line 79 def Remove *anIds cm = self.new cm.remove *anIds cm.commit end |
.Subtract(anotherCrontab) ⇒ Object
Removes crontab definitions of another crontab (see instance method subtract).
98 99 100 101 102 |
# File 'lib/cronedit.rb', line 98 def Subtract anotherCrontab cm=self.new cm.subtract anotherCrontab cm.commit end |
Instance Method Details
#add(anId, aDef) ⇒ Object
Add a new crontab entry definition. Becomes effective only after commit().
-
aDef is can be a standart text definition or a Hash definition (see CronEntry::DEFAULTS)
-
anId is an identification of the entry (for later modification or deletion)
returns newly added CronEntry
110 111 112 113 |
# File 'lib/cronedit.rb', line 110 def add anId, aDef @adds[anId.to_s] = CronEntry.new( aDef ) @removals.delete anId.to_s end |
#clear! ⇒ Object
Clear crontab completely and immediately. Warning: no commit needed (no rollback anymore)
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/cronedit.rb', line 169 def clear! rollback # we dont do it using crontab command (-r , -d) because it differs on various platfoms (vixiecron vs. dilloncron) io = getOutput begin io << '' ensure io.close if @opts[:close_output] end end |
#commit ⇒ Object
Merges the existing crontab with all modifications and installs the new crontab. returns the merged parsed crontab hash
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cronedit.rb', line 144 def commit # merge crontab currentEntries, lines = listFull() mergedEntries = nil # install it io = getOutput begin mergedEntries = dumpCron currentEntries, lines, io ensure io.close if @opts[:close_output] end # No idea why but without this any wait crontab reads and writes appears not synchronizes sleep 0.01 #clean changes :) rollback() mergedEntries end |
#list ⇒ Object
Read the current crontab and parse it returns a Hash (entry id or index)=>CronEntry
188 189 190 191 |
# File 'lib/cronedit.rb', line 188 def list res = listFull res ? res[0] : nil end |
#listFull ⇒ Object
Read the current crontab and parse it, keeping the format returns a [entires, lines] where entries = Hash (entry id or index)=>CronEntry; lines = Array of Strings of EntryPlaceholders
195 196 197 198 199 200 201 202 |
# File 'lib/cronedit.rb', line 195 def listFull io = getInput begin return parseCrontabFull(io) ensure io.close if @opts[:close_input] end end |
#listRaw ⇒ Object
Lists raw content from crontab returns array of text lines
206 207 208 209 210 211 212 213 214 |
# File 'lib/cronedit.rb', line 206 def listRaw io = getInput begin entries = io.readlines return (entries.first =~ /^no/).nil? ? entries : [] #returns empty list if no entry ensure io.close if @opts[:close_input] end end |
#merge(aCrontab) ⇒ Object
Bulk addition/merging of crontab definitions from another Crontab (FileCrontab, DummyCrontab, or a Crontab of another user)
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cronedit.rb', line 116 def merge aCrontab entries, lines = aCrontab.listFull #todo: we throw the incoming lines away #merge original data @adds.merge! entries #merge noncommited data as well aCrontab.adds.each {|k,v| @adds[k]=v} aCrontab.removals.each {|k,v| remove k} self end |
#remove(*anIds) ⇒ Object
Remove a crontab entry definitions identified by anIds. Becomes effective only after commit().
135 136 137 138 139 140 |
# File 'lib/cronedit.rb', line 135 def remove *anIds anIds.each { |id| @adds.delete id.to_s @removals[id.to_s]=id.to_s } end |
#review ⇒ Object
A helper method that prints out the items to be added and removed
181 182 183 184 |
# File 'lib/cronedit.rb', line 181 def review puts "To be added: #{@adds.inspect}" puts "To be removed: #{@removals.keys.inspect}" end |
#rollback ⇒ Object
Discards all modifications (since last commit, or creation)
163 164 165 166 |
# File 'lib/cronedit.rb', line 163 def rollback @adds = {} @removals = {} end |
#setIO(anInput, anOutput) ⇒ Object
Set alternative I/O for reading/writing cron entries. If set, the respective stream will be used instead of calling system ‘crontab’
217 218 219 220 221 |
# File 'lib/cronedit.rb', line 217 def setIO anInput, anOutput @input = anInput @output = anOutput self end |
#subtract(aCrontab) ⇒ Object
Bulk subtraction/removal of crontab definitions from another Crontab (FileCrontab, DummyCrontab, or a Crontab of another user)
128 129 130 131 132 |
# File 'lib/cronedit.rb', line 128 def subtract aCrontab entries, lines = aCrontab.listFull entries.each {|id,entry| remove id} aCrontab.adds.each {|id,entry| remove id} end |