Class: Xxeo::Job

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(queue, name, path, status) ⇒ Object



11
12
13
14
15
# File 'lib/job.rb', line 11

def Job.create(queue, name, path, status)
  raise unless queue.class == Xxeo::FileQ

  Job.new(queue, name, path, status)
end

Instance Method Details

#callback_read_logsObject

The @fq locks and then does a callback here



137
138
139
140
141
142
143
144
# File 'lib/job.rb', line 137

def callback_read_logs
  data = ''
  File.open(@path + '/log', "r") do
    |f|
    data = f.read
  end
  return data
end

#callback_status_mesgObject

The @fq locks and then does a callback here



102
103
104
105
106
107
108
109
# File 'lib/job.rb', line 102

def callback_status_mesg
  data = ''
  File.open(@path + '/status', "r") do
    |f|
    data = f.read
  end
  return data
end

#dataObject

Raises:



21
22
23
24
25
26
# File 'lib/job.rb', line 21

def data
  raise WrongStatus.new('cannot disown a non-owned job') unless @own
  data = nil
  File.open(@path + '/data') { |f| data = f.read }
  return data
end

#disownObject

Raises:



53
54
55
56
57
58
# File 'lib/job.rb', line 53

def disown
  raise WrongStatus.new('cannot disown a non-owned job') unless @own
  @own = false
  File.unlink(@path + '/pid')
  @owning_pid = nil
end

#is_file?Boolean



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

def is_file?
  meta = read_meta
  raise unless meta
  return meta[:XX_type] == 'file'
end

#log(msg) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
129
# File 'lib/job.rb', line 121

def log(msg)
  raise WrongStatus.new('cannot log on non-owned job') unless @own
  File.open(@path + '/log', "a") do
    |f|
    log_msg = Time.now.to_s + " == " + msg + "\n"
    f.write(log_msg)
    @logs << log_msg
  end
end

#mark_doneObject

Raises:



146
147
148
149
150
# File 'lib/job.rb', line 146

def mark_done
  raise WrongStatus.new('cannot mark job as done on non-owned job') unless @own
  status_mesg = "finishing"
  @fq.mark_job_done(self)
end

#mark_errorObject

Raises:



152
153
154
155
156
# File 'lib/job.rb', line 152

def mark_error
  raise WrongStatus.new('cannot mark job as error on non-owned job') unless @own
  status_mesg = "finishing"
  @fq.mark_job_error(self)
end

#nameObject



17
18
19
# File 'lib/job.rb', line 17

def name
  return @name
end

#original_pathnameObject



38
39
40
41
42
43
# File 'lib/job.rb', line 38

def original_pathname
  meta = read_meta
  raise unless meta
  return meta[:XX_original_file_name] if meta[:XX_type] == 'file'
  return nil
end

#own?Boolean



49
50
51
# File 'lib/job.rb', line 49

def own?
  @own
end

#owning_pidObject



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

def owning_pid
  @owning_pid 
end

#pullObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/job.rb', line 65

def pull
  return self if @own 

  # This is kinda wonky, obviously

  # since the @fq already called set_as_active on an object

  if @fq.pull_job(@name)
    @own = true
    @path = @fq.run_que_path + @name
    @status = ST_RUN
    @owning_pid = $$
    return self
  end
  return nil
end

#read_logObject



131
132
133
134
# File 'lib/job.rb', line 131

def read_log
  return @logs.join('') if @own
  return @fq.status_logs_for_job(@name)
end

#read_metaObject



45
46
47
# File 'lib/job.rb', line 45

def read_meta
  return @fq.meta_for_job(@name)
end

#reinsertObject

Raises:



158
159
160
161
162
163
# File 'lib/job.rb', line 158

def reinsert
  raise WrongStatus.new('cannot reinsert on non-owned job') unless @own
  status_mesg = "reinserting"
  log("reinserting job")
  @fq.reinsert_job(self)
end

#set_as_activeObject



80
81
82
83
84
85
86
87
# File 'lib/job.rb', line 80

def set_as_active
  @own = true
  # Record

  @path = @fq.run_que_path + @name
  @status = ST_RUN
  File.open(@path + '/pid', "w") { |f| f.write("#{$$}\n") }
  @owning_pid = $$
end

#set_status(path, status) ⇒ Object



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

def set_status(path, status)
  @path = path
  @status = status
end

#statusObject

TODO: this could be stale



90
91
92
93
# File 'lib/job.rb', line 90

def status
  return @status if @own
  return @fq.status(@name)
end

#status_allObject



117
118
119
# File 'lib/job.rb', line 117

def status_all
  return [@status, @owning_pid, status_mesg]
end

#status_mesgObject

TODO: this could be stale



96
97
98
99
# File 'lib/job.rb', line 96

def status_mesg
  return @status_mesg if @own
  return @fq.status_mesg_for_job(@name)
end

#status_mesg=(msg) ⇒ Object

Raises:



111
112
113
114
115
# File 'lib/job.rb', line 111

def status_mesg=(msg)
  raise WrongStatus.new('cannot set status mesg on non-owned job') unless @own
  @status_mesg = msg
  File.open(@path + '/status', "w") { |f| f.write(msg) }
end