1 /**
2 Copyright: Copyright (c) 2019, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module distssh.types;
7 
8 import core.thread : Thread;
9 import std.datetime : SysTime, dur;
10 
11 public import my.path : Path;
12 import my.named_type;
13 
14 immutable globalEnvHostKey = "DISTSSH_HOSTS";
15 immutable globalEnvFileKey = "DISTSSH_IMPORT_ENV";
16 immutable globalEnvFilterKey = "DISTSSH_ENV_EXPORT_FILTER";
17 immutable globalEnvPurge = "DISTSSH_AUTO_PURGE";
18 immutable globalEnvPurgeWhiteList = "DISTSSH_PURGE_WLIST";
19 immutable distShell = "distshell";
20 immutable distCmd = "distcmd";
21 immutable distsshEnvExport = "distssh_env.export";
22 immutable ulong defaultTimeout_s = 2;
23 /// Number of the top X candidates to choose a server from to put the work on.
24 immutable topCandidades = 3;
25 
26 @safe:
27 
28 struct HostLoad {
29     Host host;
30     Load load;
31     /// When the host was last updated
32     SysTime updated;
33 }
34 
35 alias Host = NamedType!(string, Tag!"Host", null, Comparable, ImplicitConvertable);
36 
37 /// The load of a host.
38 struct Load {
39     import std.datetime : Duration;
40 
41     double loadAvg = int.max;
42     Duration accessTime = 1.dur!"hours";
43     bool unknown = true;
44 
45     bool opEquals(const typeof(this) o) nothrow @safe pure @nogc {
46         if (unknown && o.unknown)
47             return true;
48         return loadAvg == o.loadAvg && accessTime == o.accessTime;
49     }
50 
51     int opCmp(const typeof(this) o) pure @safe @nogc nothrow {
52         if (unknown && o.unknown)
53             return 0;
54         else if (unknown)
55             return 1;
56         else if (o.unknown)
57             return -1;
58 
59         if (loadAvg < o.loadAvg)
60             return -1;
61         else if (loadAvg > o.loadAvg)
62             return 1;
63 
64         if (accessTime < o.accessTime)
65             return -1;
66         else if (accessTime > o.accessTime)
67             return 1;
68 
69         return this == o ? 0 : 1;
70     }
71 }
72 
73 @("shall sort the loads")
74 unittest {
75     import std.algorithm : sort;
76     import std.array : array;
77     import core.time : dur;
78     import std.conv : to;
79 
80     {
81         auto raw = [
82             Load(0.6, 500.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
83         ].sort.array;
84         assert(raw[0].loadAvg == 0.5, raw[0].to!string);
85     }
86 
87     {
88         auto raw = [
89             Load(0.5, 600.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
90         ].sort.array;
91         assert(raw[0].accessTime == 500.dur!"msecs");
92     }
93 }
94 
95 /// Mirror of an environment.
96 alias Env = NamedType!(string[string], Tag!"Environment", null);