Created
April 14, 2017 11:04
-
-
Save thomasjslone/752c0ff62c9fba00700c734ecc03511d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Name: Ruby Bot | |
## Version: 2.0.2 | |
## Release: 2017-3-29 | |
## Manufacturer: Thomas Tech | |
class Bot | |
def initialize(auto) | |
@todo, @doing, @done, @failed, @log = [], [], [], [], [] | |
@run, @main = false, nil | |
@cycles, @delay = 0, 0.0 | |
self.log "Initialized" | |
if auto ; self.start ; end | |
end | |
def start | |
if @run == false | |
self.log "Started Main Cycle" | |
@run = true | |
@main = Thread.new{ while @run | |
if @doing.length > 0 | |
@doing.each do |t| | |
if t[1].alive? == false | |
@done << [t[0].to_s, t[2].to_s, t[1].value] ; @doing.delete t | |
end | |
end | |
end | |
if @todo.length > 0 | |
todo = @todo ; @todo = [] | |
todo.each do |t| | |
begin | |
th = eval "Thread.new {" + t.to_s + "}" ; p = "" ; 4.times { p << rand(11).to_s } | |
@doing << [t.to_s,th,p.to_s,[tstamp.to_s]] | |
rescue | |
self.log "Task generated an exception and was skipped: " + t.to_s | |
end | |
end | |
end | |
@cycles += 1 ; sleep @delay.to_f | |
end } | |
true | |
else | |
false | |
end | |
end | |
def stop | |
if @run | |
@run = false | |
if @doing.length > 0 | |
@doing.each do |t| | |
n = @doing.length.to_s | |
t.kill @done << t[0].to_s, t[2].to_s, t[1].value | |
self.log "Killed " + n.to_s + " running tasks." | |
end | |
@doing = [] | |
@main.kill ; @main = nil | |
end | |
self.log "Stopped main cycle." | |
true | |
else | |
false | |
end | |
end | |
def state | |
if @run == true and @doing.length > 0 ; "Acive" | |
elsif @run == true and @doing.length == 0 ; "Idle" | |
elsif @run == false and @doing.length > 0 ; "Background" | |
elsif @run == false and @doing.length == 0 ; "Dormant" | |
end | |
end | |
def info | |
str = "" | |
str << "Active: " + state.to_s + "\n" | |
str << "Todo: " + @todo.length.to_s + "\n" | |
str << "Doing: " + @doing.length.to_s + "\n" | |
str << "Done: " + @done.length.to_s + "\n" | |
str << "Cycles: " + @cycles.to_s + "\n" | |
str << "Delay: " + @delay.to_s + "\n" | |
str.to_s | |
end | |
def add(t) | |
if t.is_a? String | |
@todo << t.to_s | |
self.log "Taskque recieved new taskset: " + t.to_s | |
elsif t.is_a? Array | |
t.each do |ta| | |
@todo << ta.to_s | |
end | |
self.log "Taskque recieved new taskset: " + t.to_s | |
end | |
end | |
def remove(t) | |
rt = [] ; nrt = 0 | |
if t.is_a? String | |
rt << t.to_s | |
elsif t.is_a? Array | |
rt = t | |
end | |
rt.each do |ttr| | |
if @todo.inlcude?(ttr); self.log "Task removed from que: " + ttr.to_s ; @todo.delete ttr ; nrt += 1 ; end | |
end | |
if nrt > 0 | |
nrt.to_i | |
else | |
false | |
end | |
end | |
def exeq(t) | |
et = [] ; etc = 0 | |
if t.is_a? String | |
et << t.to_s | |
elsif t.is_a? Array | |
et = t | |
end | |
et.each do |tte| | |
if @todo.include?(tte) | |
begin | |
th = eval("Thread.new {" + tte.to_s + "}") ; p = "" ; 4.times { p << rand(11).to_s } | |
@doing << [t.to_s,th,p.to_s,[tstamp.to_s]] | |
@todo.delete(tte) | |
self.log "Task pushed to cycle manually: " + tte.to_s | |
ect += 1 | |
rescue | |
self.log "Manual task push generated an exception and was skipped: " + t.to_s | |
end | |
end | |
end | |
if ect > 0 | |
ect.to_i | |
else | |
false | |
end | |
end | |
def kill(t) | |
kt = [] ; kc = 0 | |
if t.is_a? STRING | |
kt << t | |
else | |
kt = t | |
end | |
if @doing.length > 0 | |
kt.each do |pcid| | |
@doing.each do |th| | |
if th[2].to_s == pcid | |
th[1].kill | |
th[3] << tstamp.to_s | |
self.log "Active task killed, pcid: " + th[2].to_s + ", Task: " + th[0].to_s | |
@done << th | |
@doing.delete th | |
kc += 1 | |
end | |
end | |
end | |
if kc == 1 | |
true | |
else | |
kc | |
end | |
else | |
false | |
end | |
end | |
def todo ; @todo ; end | |
def doing ; @doing ; end | |
def done ; @done ; end | |
def cycles ; @cycles ; end | |
def delay ; @delay.to_f ; end | |
def set_delay(d) ; if d.is_a? Float ; od = @delay.to_s ; @delay = d.to_f ; self.log "Main cycle delay was changed from " + od.to_s + " to " + @delay.to_s ; true ; else ; false ; end ; end | |
def log str ; if str.to_s != "" ; @log << tstamp.to_s + " :: " + str.to_s ; end ; end | |
def get_log ; @log ; end | |
def view_log ; if @log.length < 30 ; puts @log.join("\n").to_s ; else ; puts @log[-25..-1].join("\n").to_s ; end ; end | |
def exec(r) ; begin ; eval(r.to_s) ; rescue => exception ; exception.to_s + "\n" + exception.backtrace.join("\n").to_s ; end ; end | |
def tstamp ; Time.now.to_s.split(" ")[0..1].join(".").split(":").join(".").split("-").join(".").to_s ; end | |
def cstamp(ts) ; if ts.is_a? Time ; t = ts.split(".") ; Time.new(t[0].to_i,t[1].to_i,t[2].to_i,t[3].to_i,t[4].to_i,t[5].to_i) ; else ; false ; end ; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment