What we have so far works, however ideally, printing the pid should
not rely on the completion of the job. The only thing that should rely
on the completion of the job would be outputting the exitstatus.
Here's my vision..
class JobServer
def execute(cmd)
# kicks off the job and returns the pid
return pid
end
def status(pid)
# returns the status of the job. IE is the pid
alive? or dead?
return pid.alive?
end
def exitstatus(pid)
# returns the exitstatus based on the PID
return pid.exitstatus
end
end
# Scenerio: I need to kick off two jobs and a third upon completion of
the first (when exit statis is 0).
# Ideally, one would be able to kick off a job with execute() and know
about it's pid. Then later on, be able to reference the status of that
pid, such as alive? or dead?. If dead, one could call the
exitstatus() method to find out what the return code of that job was.
case mode
when %r/server/i
DRb.start_service "druby://#{ hostname }:#{ port }"=
,
JobServer.new
DRb.thread.join
when %r/client/i
DRb.start_service
job =3D DRbObject.new(nil, "druby://#{ hostname
}:#{ port }")
pid =3D job.execute("ruby -e' sleep 3 and exit 43 '=
")
# without having to wait on the execution
above to finish:
if job.status(pid) =3D=3D true
puts "exit status =3D " + job.existstat=
us(pid)
else
puts "job still running"
end
end
#Again, this code isnt functional, just a concept of how I would see.
What's preventing me from turning waht we have into this is my lack of
knowledge in the following w/ your code:
# 1) not exactly sure attr_accessor does
# 2) not sure what Queue.new does
# 3) not sure what def initialize does
# Again, I really=B2 appreciate your help in understanding and assistance.
On 12/3/05,
<> wrote:
> On Sun, 4 Dec 2005, x1 wrote:
>
> > I've hacked at it and managed to learn quite a few things in the
> > process but I cant seem to assign the Exec class to a DRb server to
> > handle requests.
> >
> > Ideally, the <client> would be able to do something like:
>
> <snip>
>
> harp:~ > cat servant.rb
> #! /usr/bin/env ruby
> %w( thread drb socket time yaml ).each{|lib| require lib}
>
> class Execute
> %w( cmd queue pipe pid thread exitstatus alive ).each{|a| attr_acce=
ssor a}
> def initialize(cmd)
> @cmd =3D cmd
> @queue =3D Queue.new
> @thread =3D Thread.new(queue) do |q|
> pipe =3D IO.popen(cmd)
> q.push pipe
> q.push pipe.pid
> begin
> loop{ stdout =3D pipe.gets or break }
> pipe.close
> q.push $?.exitstatus
> rescue =3D> e
> q.push e
> end
> end
> @pipe =3D queue.pop
> @pid =3D queue.pop
> @exitstatus =3D nil
> end
> def wait
> @exitstatus =3D @queue.pop
> end
> end
>
> class Executioner
> def execute cmd
> Execute::new cmd
> end
> end
>
> mode =3D ARGV.shift
> hostname =3D Socket.gethostname
> port =3D 4501
>
> case mode
> when %r/server/i
> DRb.start_service "druby://#{ hostname }:#{ port }", Executioner.=
new
> DRb.thread.join
>
> when %r/client/i
> DRb.start_service
> executioner =3D DRbObject.new nil, "druby://#{ hostname }:#{ port=
}"
> sleep =3D executioner.execute "ruby -e' sleep 3 and exit 43 '"
> y "start" =3D> Time::now.iso8601
> y "pid" =3D> sleep.pid
> y "exitstatus" =3D> sleep.wait
> y "finish" =3D> Time::now.iso8601
> end
>
>
>
> harp:~ > ./servant.rb server &
> [1] 15727
>
>
>
> harp:~ > ./servant.rb client
> ---
> start: "2005-12-03T16:58:19-07:00"
> ---
> pid: 15729
> ---
> exitstatus: 43
> ---
> finish: "2005-12-03T16:58:22-07:00"
>
>
> hth.
>
>
> -a
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
> | ara [dot] t [dot] howard [at] noaa [dot] gov
> | all happiness comes from the desire for others to be happy. all misery
> | comes from the desire for oneself to be happy.
> | -- bodhicaryavatara
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
>
>
>