/*
  mysql -h localhost -u root -p

  
 */

/* Create user queue */  
use mysql; 
insert into user (Host, User, Password)
  values('localhost', 'queue', '');
insert into user (Host, User, Password)
  values('%', 'queue', password('qu3u3'));
/* on duplicate key update; */

/* Allow queue user to access queue db */
use mysql; 
insert into db (Host, Db, User, Select_priv)
  values('localhost','queue','queue','Y');
insert into db (Host, Db, User, Select_priv)
  values('%','queue','queue','Y');

/* Create queue db */
create database queue;

/* Create list table */
use queue;
create table list (
  /* job id number */
  jid integer not null auto_increment,

  /* user id */
  uid integer not null,

  /* user login name */
  user varchar(63) default '' not null,

  /* directory name */
  directory varchar(255) default '' not null,

  /* script name */
  script varchar(1023) default '' not null,

  /* job status: waiting, selected, removed, running, ended, dying, killed */
  status varchar(255) default 'waiting',

  /* node where the job is running */
  node varchar(255),

  /* job process id */
  pid integer,

  /* log date */
  log datetime,

  /* start date */
  start datetime,

  /* end date */
  end datetime,

--
  primary key (jid)
);

/* Create node table */
use queue;
create table node (
  /* queue id number */
  qid integer not null auto_increment,

   /* host name */
  host varchar(255),

  /* job status: sleeping, selected, running, dead */
  status varchar(255) default 'sleeping',

  /* job process id */
  pid integer,

  /* job id number */
  jid integer default '0',

  /* log date */
  log datetime,

--
  primary key (qid)
);


/* Grant queue user to queue tables */
grant select, insert, update, delete on queue.* to queue@localhost;
grant select, insert, update, delete on queue.* to queue;
grant create temporary tables on queue.* to queue@localhost;
grant create temporary tables on queue.* to queue;

/* Flush all the stuff */
flush query cache;
