<commit_before>require 'sqlite3'
require 'securerandom'
require 'yaml'

module TaskHgmd
  extend MyLog

  @@db_file = $SETTINGS.db_file
  raise "db_dile entry not exists in config file" if @@db_file.nil?

  def self.open_db()
    puts @@db_file
    `mkdir -p tmp`
    db = SQLite3::Database.open(@@db_file)
    db.busy_timeout 3000
    return db
  end

  def self.run_sql(sql)
    db = open_db
    ret = db.execute(sql)
    db.close
    return ret
  end

  def self.init_db()
    sql = <<-EOS
create table if not exists tasks(
pid int, 
status text,
uuid string,
args string
    )
    EOS
    db = open_db()
    db.execute sql      
    db.close()
  end 

  def self.add_task(uuid, args = nil)
    db = open_db
    db.execute <<-EOS
insert into tasks(pid, status, uuid, args)
values( -1, \"NotDone\", '#{uuid}', '#{args.to_s}' )
    EOS
    db.close
  end

  def self.set_pid(uuid, pid)
    db = open_db
    db.execute "update tasks set pid = \"#{pid}\" where uuid = \"#{uuid}\" "
    db.close
  end

  def self.done_task(uuid)
    db = open_db
    db.execute "UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" "
    db.close    
  end
  def self.done_task_bash(uuid)
    cmd = <<-EOS
    sqlite3 #{@@db_file} 'UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" '
    EOS
    `cmd`
  end

  def self.spawn(bashfile,args = [], dir = Dir.pwd,
   uuid = Time.now.strftime("%Y%m%d-%H_%M_%S%Z") + "--" + SecureRandom.uuid )
    init_db()
    args_str = args.join(" ")
    add_task(uuid, args_str)
    # http://dba.stackexchange.com/questions/47919/how-do-i-specify-a-timeout-in-sqlite3-from-the-command-line
    bash_cmd = <<-EOS
set -xv
bash #{bashfile} #{ args_str } || exit 1
sqlite3 -init ./etc/set_timeout.sql #{@@db_file} 'UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" '
exit 0
    EOS
    `mkdir -p ./log/tasks`
    pid = Process.spawn( bash_cmd , :chdir => dir, :pgroup=>nil,
      [:out,:err]=>[ File.join("./log/tasks/", uuid ), "w+"] )
    set_pid(uuid, pid)
    Process.detach pid
    return pid
  end

end
<commit_msg>mod spawn arg<commit_after>require 'sqlite3'
require 'securerandom'
require 'yaml'

module TaskHgmd
  extend MyLog

  @@db_file = $SETTINGS.db_file
  raise "db_dile entry not exists in config file" if @@db_file.nil?

  def self.open_db()
    puts @@db_file
    `mkdir -p tmp`
    db = SQLite3::Database.open(@@db_file)
    db.busy_timeout 3000
    return db
  end

  def self.run_sql(sql)
    db = open_db
    ret = db.execute(sql)
    db.close
    return ret
  end

  def self.init_db()
    sql = <<-EOS
create table if not exists tasks(
pid int, 
status text,
uuid string,
args string
    )
    EOS
    db = open_db()
    db.execute sql      
    db.close()
  end 

  def self.add_task(uuid, args = nil)
    db = open_db
    db.execute <<-EOS
insert into tasks(pid, status, uuid, args)
values( -1, \"NotDone\", '#{uuid}', '#{args.to_s}' )
    EOS
    db.close
  end

  def self.set_pid(uuid, pid)
    db = open_db
    db.execute "update tasks set pid = \"#{pid}\" where uuid = \"#{uuid}\" "
    db.close
  end

  def self.done_task(uuid)
    db = open_db
    db.execute "UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" "
    db.close    
  end
  def self.done_task_bash(uuid)
    cmd = <<-EOS
    sqlite3 #{@@db_file} 'UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" '
    EOS
    `cmd`
  end

  def self.spawn(bashfile,args = [], dir = Dir.pwd,
   uuid = Time.now.strftime("%Y%m%d-%H_%M_%S%Z") + "--" + SecureRandom.uuid )
    init_db()
    args_str = args.join(" ")
    add_task(uuid, args_str)
    # http://dba.stackexchange.com/questions/47919/how-do-i-specify-a-timeout-in-sqlite3-from-the-command-line
    bash_cmd = <<-EOS
set -xv
bash #{bashfile} #{ args_str } || exit 1
sqlite3 -init ./etc/set_timeout.sql #{@@db_file} 'UPDATE tasks SET status = \"Done\" WHERE uuid = \"#{uuid}\" '
exit 0
    EOS
    `mkdir -p ./log/tasks`
    pid = Process.spawn( bash_cmd , :chdir => dir, :pgroup=>nil,
      [:out,:err]=>[ File.join($SETTINGS.root,"log/tasks/", uuid ), "w+"] )
    set_pid(uuid, pid)
    Process.detach pid
    return pid
  end

end

{'fraction_non_alphanumeric': 0.15758896151053015, 'fraction_numerical': 0.00823045267489712, 'mean_word_length': 2.6631205673758864, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1245973', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1605, 'n_words': 400}