Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/19 23:20:52 (5 years ago)
Author:
abeham
Message:

Added further projects useful for interfacing with irace

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/tools/HeuristicLab.HiveDrain/HiveStatus/Program.cs

    r15921 r16667  
    3131    public const int EXIT_OK = 0;
    3232    public const int EXIT_ERR = 1;
    33     public const int EXIT_WAITING = 2;
    34     public const int EXIT_FAILED = 3;
    35     public const int EXIT_NOTFOUND = 4;
     33    public const int EXIT_FAILED = 2;
     34    public const int EXIT_NOTFOUND = 3;
    3635
    3736    static void Main(string[] args) {
     37      //AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "HiveStatus.exe.config");
    3838      var result = Parser.Default.ParseArguments<Options>(args)
    3939        .WithParsed(x => {
     
    4545    }
    4646
    47     private static int Run(Options opt) {
    48       if (!string.IsNullOrEmpty(opt.Username)) {
    49         HiveServiceLocator.Instance.Username = opt.Username;
    50         var password = opt.Password;
     47    private static int Run(Options opts) {
     48#if __DRY_RUN__
     49      Console.WriteLine("DONE");
     50      return EXIT_OK;
     51#endif
     52      if (string.IsNullOrEmpty(opts.Username) && HeuristicLab.Clients.Common.Properties.Settings.Default.UserName.ToLower() == "anonymous") {
     53        Console.Write("Username: ");
     54        opts.Username = Console.ReadLine();
     55      }
     56      if (!string.IsNullOrEmpty(opts.Username)) {
     57        HiveServiceLocator.Instance.Username = opts.Username;
     58        var password = opts.Password;
    5159        if (string.IsNullOrEmpty(password)) {
    52           Console.Error.Write("Password for " + opt.Username + ": ");
     60          Console.Error.Write("Password for " + opts.Username + ": ");
    5361          password = ReadPassword();
    5462        }
    5563        HiveServiceLocator.Instance.Password = password;
    5664      }
    57       return CheckStatus(opt);
     65      return CheckStatus(opts);
     66    }
     67
     68    private static int CheckStatus(Options opt) {
     69      if (opt.JobId == default(Guid)) return CheckStatusAll(opt);
     70
     71      var jobId = opt.JobId;
     72
     73      if (opt.Verbose) {
     74
     75        List<LightweightTask> tasks = null;
     76        try {
     77          tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(jobId));
     78        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
     79          Console.WriteLine("?");
     80          return EXIT_NOTFOUND;
     81        }
     82
     83        int completed, failed, running, paused;
     84        GetStats(tasks, out completed, out failed, out running, out paused);
     85        Console.WriteLine(FormatStateInfo(tasks.Count, completed, failed, running, paused));
     86        return failed > 0 ? EXIT_FAILED : EXIT_OK;
     87
     88      } else {
     89
     90        Job job = null;
     91        try {
     92          job = HiveServiceLocator.Instance.CallHiveService(x => x.GetJob(jobId));
     93        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
     94          Console.WriteLine("?");
     95          return EXIT_NOTFOUND;
     96        }
     97        Console.WriteLine(FormatStateInfo(job.JobCount, job.FinishedCount, job.CalculatingCount));
     98        return EXIT_OK;
     99      }
     100    }
     101
     102    private static int CheckStatusAll(Options opt) {
     103      List<Job> jobs = null;
     104      try {
     105        jobs = HiveServiceLocator.Instance.CallHiveService(x => x.GetJobs());
     106      } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
     107        Console.WriteLine("?");
     108        return EXIT_NOTFOUND;
     109      }
     110
     111      if (opt.Verbose) {
     112        List<LightweightTask> tasks = null;
     113        int totFinished, totFailed, totCalculating, totPaused, total;
     114        totFinished = totFailed = totCalculating = totPaused = total = 0;
     115        try {
     116
     117          foreach (var j in jobs) {
     118            tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(j.Id));
     119
     120            int finished, failed, calculating, paused;
     121            GetStats(tasks, out finished, out failed, out calculating, out paused);
     122            totFinished += finished;
     123            totFailed += failed;
     124            totCalculating += calculating;
     125            totPaused += paused;
     126            total += tasks.Count;
     127
     128            Console.WriteLine(FormatStateInfo(tasks.Count, finished, failed, calculating, paused) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
     129          }
     130        } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
     131          Console.WriteLine("?");
     132          return EXIT_NOTFOUND;
     133        }
     134        Console.WriteLine();
     135        Console.WriteLine(FormatStateInfo(total, totFinished, totFailed, totCalculating, totPaused) + " | ALL");
     136        return totFailed > 0 ? EXIT_FAILED : EXIT_OK;
     137      } else {
     138        int totFinished, totCalculating, total;
     139        totFinished = totCalculating = total = 0;
     140        foreach (var j in jobs) {
     141          Console.WriteLine(FormatStateInfo(j.JobCount, j.FinishedCount, j.CalculatingCount) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
     142          totFinished += j.FinishedCount;
     143          totCalculating += j.CalculatingCount;
     144          total += j.JobCount;
     145        }
     146        Console.WriteLine();
     147        Console.WriteLine(FormatStateInfo(total, totFinished, totCalculating) + " | ALL");
     148        return EXIT_OK;
     149      }
     150    }
     151
     152    private static void GetStats(List<LightweightTask> tasks, out int completed, out int failed, out int running, out int paused) {
     153      completed = tasks.Count(x => x.State == TaskState.Finished);
     154      failed = tasks.Count(x => x.State == TaskState.Failed || x.State == TaskState.Aborted);
     155      running = tasks.Count(x => x.State == TaskState.Calculating || x.State == TaskState.Transferring
     156|| x.State == TaskState.Waiting);
     157      paused = tasks.Count(x => x.State == TaskState.Paused);
     158    }
     159
     160    private static string FormatStateInfo(int total, int finished, int calculating) {
     161      var state = finished == total ? "DONE" : "CALC";
     162      return string.Join(" ", new object[] { state
     163        , "CALC=" + calculating.ToString().PadRight(4)
     164        , "DONE=" + finished.ToString().PadRight(4) });
     165    }
     166
     167    private static string FormatStateInfo(int total, int finished, int failed, int calculating, int paused) {
     168      var state = finished == total ? "DONE" : failed > 0 ? "FAIL" : calculating > 0 ? "CALC" : paused > 0 ? "PAUS" : "NDEF";
     169      return string.Join(" ", new object[] { state
     170        , "CALC=" + calculating.ToString().PadRight(4)
     171        , "DONE=" + finished.ToString().PadRight(4)
     172        , "FAIL=" + failed.ToString().PadRight(4)
     173        , "PAUS=" + paused.ToString().PadRight(4) });
     174    }
     175
     176    #region Helpers
     177    private static string Truncate(string text, int length) {
     178      if (text.Length <= length) return text;
     179      return text.Substring(0, length - 3) + "...";
    58180    }
    59181
     
    103225      return sb.ToString();
    104226    }
    105 
    106     private static int CheckStatus(Options opt) {
    107       if (opt.All) return CheckStatusAll(opt);
    108 
    109       var jobId = opt.JobId;
    110 
    111       if (opt.Verbose) {
    112 
    113         List<LightweightTask> tasks = null;
    114         try {
    115           tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(jobId));
    116         } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
    117           Console.WriteLine("?");
    118           return EXIT_NOTFOUND;
    119         }
    120 
    121         int completed, failed, running, paused;
    122         GetStats(tasks, out completed, out failed, out running, out paused);
    123         Console.WriteLine(FormatStateInfo(tasks.Count, completed, failed, running, paused));
    124         return completed == tasks.Count ? EXIT_OK : failed > 0 ? EXIT_FAILED : EXIT_WAITING;
    125 
    126       } else {
    127 
    128         Job job = null;
    129         try {
    130           job = HiveServiceLocator.Instance.CallHiveService(x => x.GetJob(jobId));
    131         } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
    132           Console.WriteLine("?");
    133           return EXIT_NOTFOUND;
    134         }
    135         Console.WriteLine(FormatStateInfo(job.JobCount, job.FinishedCount, job.CalculatingCount));
    136         return job.FinishedCount == job.JobCount ? EXIT_OK : EXIT_WAITING;
    137       }
    138     }
    139 
    140     private static int CheckStatusAll(Options opt) {
    141       List<Job> jobs = null;
    142       try {
    143         jobs = HiveServiceLocator.Instance.CallHiveService(x => x.GetJobs());
    144       } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
    145         Console.WriteLine("?");
    146         return EXIT_NOTFOUND;
    147       }
    148 
    149       if (opt.Verbose) {
    150         List<LightweightTask> tasks = null;
    151         int totFinished, totFailed, totCalculating, totPaused, total;
    152         totFinished = totFailed = totCalculating = totPaused = total = 0;
    153         try {
    154 
    155           foreach (var j in jobs) {
    156             tasks = HiveServiceLocator.Instance.CallHiveService(x => x.GetLightweightJobTasksWithoutStateLog(j.Id));
    157 
    158             int finished, failed, calculating, paused;
    159             GetStats(tasks, out finished, out failed, out calculating, out paused);
    160             totFinished += finished;
    161             totFailed += failed;
    162             totCalculating += calculating;
    163             totPaused += paused;
    164             total += tasks.Count;
    165 
    166             Console.WriteLine(FormatStateInfo(tasks.Count, finished, failed, calculating, paused) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
    167           }
    168         } catch (System.ServiceModel.Security.SecurityAccessDeniedException) {
    169           Console.WriteLine("?");
    170           return EXIT_NOTFOUND;
    171         }
    172         Console.WriteLine();
    173         Console.WriteLine(FormatStateInfo(total, totFinished, totFailed, totCalculating, totPaused) + " | ALL");
    174         return totFinished == total ? EXIT_OK : totFailed > 0 ? EXIT_FAILED : EXIT_WAITING;
    175       } else {
    176         int totFinished, totCalculating, total;
    177         totFinished = totCalculating = total = 0;
    178         foreach (var j in jobs) {
    179           Console.WriteLine(FormatStateInfo(j.JobCount, j.FinishedCount, j.CalculatingCount) + " | " + Truncate(j.Name, 40).PadRight(40) + " | " + j.DateCreated.ToShortDateString());
    180           totFinished += j.FinishedCount;
    181           totCalculating += j.CalculatingCount;
    182           total += j.JobCount;
    183         }
    184         Console.WriteLine();
    185         Console.WriteLine(FormatStateInfo(total, totFinished, totCalculating) + " | ALL");
    186         return totFinished == total ? EXIT_OK : EXIT_WAITING;
    187       }
    188     }
    189 
    190     private static void GetStats(List<LightweightTask> tasks, out int completed, out int failed, out int running, out int paused) {
    191       completed = tasks.Count(x => x.State == TaskState.Finished);
    192       failed = tasks.Count(x => x.State == TaskState.Failed || x.State == TaskState.Aborted);
    193       running = tasks.Count(x => x.State == TaskState.Calculating || x.State == TaskState.Transferring
    194 || x.State == TaskState.Waiting);
    195       paused = tasks.Count(x => x.State == TaskState.Paused);
    196     }
    197 
    198     private static string FormatStateInfo(int total, int finished, int calculating) {
    199       var state = finished == total ? "DONE" : "CALC";
    200       return string.Join(" ", new object[] { state
    201         , "CALC=" + calculating.ToString().PadRight(4)
    202         , "DONE=" + finished.ToString().PadRight(4) });
    203     }
    204 
    205     private static string FormatStateInfo(int total, int finished, int failed, int calculating, int paused) {
    206       var state = finished == total ? "DONE" : failed > 0 ? "FAIL" : calculating > 0 ? "CALC" : paused > 0 ? "PAUS" : "NDEF";
    207       return string.Join(" ", new object[] { state
    208         , "CALC=" + calculating.ToString().PadRight(4)
    209         , "DONE=" + finished.ToString().PadRight(4)
    210         , "FAIL=" + failed.ToString().PadRight(4)
    211         , "PAUS=" + paused.ToString().PadRight(4) });
    212     }
    213 
    214     private static string Truncate(string text, int length) {
    215       if (text.Length <= length) return text;
    216       return text.Substring(0, length - 3) + "...";
    217     }
     227    #endregion
    218228  }
    219229}
Note: See TracChangeset for help on using the changeset viewer.