Last active
February 21, 2017 06:31
-
-
Save thomasjslone/465e4c181c4a1e9c5579832acbab5352 to your computer and use it in GitHub Desktop.
better ruby bot model
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
# @qued = [] @timestamped = [] @threads = [] @done = [] @log = [] | |
# | |
# - run | |
# - stop | |
# - running? | |
# - state? | |
# - schedual_task | |
# - unschedual_task | |
# - que_task | |
# - que_file | |
# - exec_task | |
# - exec_file | |
# - abort | |
# - info | |
class Bot | |
def initialize(tasks,auto) | |
if tasks.is_a? Array and tasks.length > 0 | |
@qued = tasks | |
else | |
@qued = [] | |
end | |
@timestamped = [] | |
@threads = [] | |
@done = [] | |
@delays = [1.0,1.0,1.0] | |
@running = false | |
@log = ["Log created @ " + Time.now.to_s] | |
if auto and tasks != nil and tasks != [] ; run ; end | |
true | |
end | |
def run | |
if @running == false | |
@running = true | |
@thread_cleaning = Thread.new { loop do | |
if @threads.length > 0 | |
@threads.each do |t| | |
if t[2].alive? == false | |
t[2].kill | |
@done << [t[1].to_s, Time.now.to_s.split(" ")[0..1].join(".").tr(":-","..").to_s] | |
@threads.delete(t) | |
end | |
end | |
end | |
sleep @delays[0].to_f ; end } | |
@timestamp_monitoring = Thread.new { loop do | |
if @timestamped.length > 0 | |
@timestamped.each do |ts| | |
if Time.now >= ts[1] | |
@qued << ts[0] ; @timestamped.delete(ts) | |
end | |
end | |
end | |
sleep @delays[1].to_f ; end} | |
@que_execution = Thread.new { loop do | |
if @qued.length > 0 | |
qued = @qued ; @qued = [] | |
qued.each do |t| | |
begin | |
pcid = rand(1..9999) | |
process = "Thread.new { " + t.to_s + "}" | |
@threads << [pcid,t.to_s,eval(process.to_s),Time.now.to_s.split(" ")[0..1].join(".").to_s.tr(":-","..").to_s] | |
rescue | |
@log << "Task Failed: " + t.to_s + " @ " + Time.now.to_s | |
end | |
end | |
end | |
sleep @delays[2].to_f ; end } | |
end | |
end | |
def stop ; if @running ; @thread_cleaning.kill ; @timestamp_monitoring.kill ; @que_execution.kill ; @running = false ; true ; else ; false ; end ; end | |
def abort ; @threads.each { |t| t.kill } ; @threads = [] ; end | |
def running? ; @running ; end | |
def state? ; if @running and @threads.length > 0 ; "ACTIVE" ; elsif @running and @threads.length == 0 ; "IDLE" ; elsif @running == false and @threads.length > 0 ; "BACKGROUND" ; elsif @running == false and @threads.length == 0 ; "IDLE" ; end ; end | |
def schedual_task(task,time) ; t = time.to_s.split(".") ; t = Time.new(t[0],t[1],t[2],t[3],t[4],t[5]) ; if Time.now < t ; @timestamped << [task.to_s,t] ; true ; else ; false ; end ; end | |
def unschedual_task(task,time) ; t = time.to_s.split(".") ; t = Time.new(t[0],t[1],t[2],t[3],t[4],t[5]) ; task = [task.to_s,time] ; if @timestamped.inlude?(task) ; @timestamped.delete(task) ; true ; else ; false ; end ; end | |
def que_task(task) | |
if task.is_a? String and task.length > 0 | |
@qued << task ; true | |
else | |
false | |
end | |
end | |
def exec_task(task) | |
begin | |
pcid = rand(1..9999) | |
activity = "Thread.new { " + task.to_s + " }" | |
@threads << [pcid,task.to_s,eval(activity.to_s),Time.now.to_s.split(" ")[0..1].join(".").tr(":-","..").to_s] | |
true | |
rescue | |
false | |
end | |
end | |
def que_file(path) | |
if File.exist?(path) | |
begin | |
fi = File.open(path.to_s,"r") ; task_load = fi.read.to_s ; fi.close | |
task_load = task_load.to_s.split("\n") | |
if task_load.length > 0 | |
task_load.each do |t| | |
if que_task(t) == false ; @log << "Que Failed: " + t.to_s + " @ " + Time.now.to_s ; end | |
end | |
true | |
else | |
false | |
end | |
rescue | |
false | |
end | |
else | |
false | |
end | |
end | |
def exec_file(path) | |
if File.exist?(path) | |
begin | |
fi = File.open(path,"r") ; task_load = fi.read.to_s ; fi.close | |
task_load = task_load.to_s.split("\n") | |
if task_load.length > 0 | |
task_load.each do |t| | |
if exec_task(t) == false ; @log << "Failed: " + t.to_s + " @ " + Time.now.to_s ; end | |
end | |
true | |
else | |
fasle | |
end | |
rescue | |
false | |
end | |
else | |
false | |
end | |
end | |
def info | |
str = "" | |
str << "State: " + state?.to_s + "\n" | |
str << "Active: " + @threads.length.to_s + "\n" | |
str << "Schedualed: " + @timestamped.length.to_s + "\n" | |
str << "Finished: " + @done.length.to_s + "\n" | |
str << "Cycle Running: " + @running.to_s + "\n" | |
str << "Cycle Delays: " + @delays[0].to_s + " , " + @delays[1].to_s + " , " + @delays[2].to_s | |
str.to_s | |
end | |
end | |
$bot = Bot.new(nil,false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment