using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using System.Configuration;

using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Services;

namespace stress {
    /*
     * this is intended to make the jobservice fail.
     */
    class Program {
        static long[] wsid_back = null;
        static long[] WSID {
            get {
                if (wsid_back == null) {
                    string[] tas = ConfigurationSettings.AppSettings["test_accounts"].Split(',');
                    wsid_back = new long[tas.Length];
                    for (int i = 0; i < tas.Length; i++) {
                        wsid_back[i] = long.Parse(tas[i].Trim());
                    }
                }
                return wsid_back;
            }
        }
        static string CONTEXT = ConfigurationSettings.AppSettings["test_context"];
        static int EXECS_PER_ACCOUNT = int.Parse(ConfigurationSettings.AppSettings["execs_per_account"]);

        const string Q_SEL1 = "SELECT objID",
            Q_REST1 = @"
FROM Galaxy 
WHERE 
  r < 17			-- r IS NOT deredenned
  and extinction_r > 0.275	-- extinction more than 0.175
";
        const string Q_SEL2 = "select *",
      Q_REST2 = @"
FROM photoobj
";
        static int MAX_THREADS = 1000;
        
        const string LBL_PRE = "STRESS_JOB ";
        static void debug() {
            Servers ss = new Servers();
            CJServer cjs = new CJServer();
            cjs.Name = "DRfB";
            cjs.Context = "DD2";
            cjs.TimeOut = 500;
            cjs.DBUser = "webuser";
            cjs.DBPw = "read4w3now!";
            cjs.DBMachine = "thumper";
            cjs.DBCatalog = "BestDR5_efg";
            cjs.GPriv = "debug";
            cjs.ExecCap = 1;
            ss.UpdateServer(1759332931, "x", "DRA",cjs, 2);
        }
        
        static void Main(string[] args) {
            debug();
            return;
            edu.jhu.Cas.Services.Jobs jobs = new Jobs();
            edu.jhu.Cas.Services.Users u = new Users();
            Users.Authenticate(1759332931, "x");
            Users.Authenticate(1759332931, "x");
            Users.Authenticate(1759332931, "x");
            return;
            ThreadPool.SetMaxThreads(MAX_THREADS,1000);
            for (int i = 0; i < EXECS_PER_ACCOUNT; i++) {
                for (int j = 0; j < WSID.Length; j++) {
                    string tab = "tab_" + i;
                    string query = Q_SEL1 + " into " + tab + Q_REST1;
                    string lbl = LBL_PRE + i+"["+j+"]";
                    DJob dj = new DJob(WSID[j], query, CONTEXT, lbl, "drop table " + tab);
                    WaitCallback wcb = new WaitCallback(dj.Run);
                    ThreadPool.QueueUserWorkItem(wcb);
                }
            }
            Console.ReadLine();
        }
        class DJob {
            string q, context, lbl,cleanup;
            long id, wsid;
            const int POLL_DELAY = 3000;
            int end_status;
            CasJobs cj;
            public DJob(long wsid, string query, string context, string label, string cleanup) {
                q = query;
                lbl = label;
                this.wsid = wsid;
                this.context = context;
                this.cleanup = cleanup;
                cj = new CasJobs();
            }
            public void Run(object state) {               
                maybeCleanup();
                id = cj.SubmitJob(wsid, q, context, lbl, 500, 0);
                Console.WriteLine("Started " + lbl + " [" + id + "]");
                Job j;
                do { 
                    j = cj.FindJob(wsid, "jobid = " + id)[0];
                    Thread.Sleep(POLL_DELAY);
                } while (j.Status < 4);
                end_status = j.Status;
                maybeCleanup();
                Console.WriteLine("Completed " + lbl + " [" + id + "]");
            }
            void maybeCleanup() {
                try {
                    cj.SubmitShortJob(wsid, cleanup, "MyDB", "CLEANUP " + lbl, 0);
                }
                catch {}
            }
        }
    }
}
