UNPKG

5 kBJavaScriptView Raw
1"use strict";
2var lifeEvent_1 = require("../environment/lifeEvent");
3var shortid = require("shortid");
4var Job = (function () {
5 /**
6 * Job constructor
7 * @param e
8 * @param name
9 */
10 function Job(e, name) {
11 this.e = e;
12 this.id = shortid.generate();
13 this.name = name;
14 this.lifeCycle = [];
15 this.createLifeEvent("created", null, name);
16 this.e.log(1, "New Job \"" + name + "\" created.", this);
17 }
18 /**
19 * Class name for logging.
20 * @returns {string}
21 */
22 Job.prototype.toString = function () {
23 return "Job";
24 };
25 /**
26 * Check if job is locally available.
27 * @returns {boolean}
28 */
29 Job.prototype.isLocallyAvailable = function () {
30 return this.locallyAvailable;
31 };
32 /**
33 * Set if the job is locally available.
34 * @param available
35 */
36 Job.prototype.setLocallyAvailable = function (available) {
37 this.locallyAvailable = available;
38 };
39 /**
40 * Get the life cycle object.
41 * @returns {LifeEvent[]}
42 */
43 Job.prototype.getLifeCycle = function () {
44 return this.lifeCycle;
45 };
46 /**
47 * Create a new life event.
48 * @param verb
49 * @param start
50 * @param finish
51 */
52 Job.prototype.createLifeEvent = function (verb, start, finish) {
53 this.lifeCycle.push(new lifeEvent_1.LifeEvent(verb, start, finish));
54 };
55 /**
56 * Set a new name.
57 * @param name
58 */
59 Job.prototype.setName = function (name) {
60 this.name = name;
61 };
62 /**
63 * Get the name.
64 * @returns {string}
65 */
66 Job.prototype.getName = function () {
67 return this.name;
68 };
69 /**
70 * Get the ID.
71 * @returns {string}
72 */
73 Job.prototype.getId = function () {
74 return this.id;
75 };
76 /**
77 * Get the name proper.
78 * @returns {string}
79 */
80 Job.prototype.getNameProper = function () {
81 return this.getName();
82 };
83 /**
84 * Set the nest.
85 * @param nest
86 */
87 Job.prototype.setNest = function (nest) {
88 this.nest = nest;
89 };
90 /**
91 * Get the nest.
92 * @returns {Nest}
93 */
94 Job.prototype.getNest = function () {
95 return this.nest;
96 };
97 /**
98 * Set the tunnel.
99 * @param tunnel
100 */
101 Job.prototype.setTunnel = function (tunnel) {
102 this.tunnel = tunnel;
103 };
104 /**
105 * Get the tunnel.
106 * @returns {Tunnel}
107 */
108 Job.prototype.getTunnel = function () {
109 return this.tunnel;
110 };
111 /**
112 * Function to call to fail a job while in a tunnel.
113 * @param reason
114 */
115 Job.prototype.fail = function (reason) {
116 var j = this;
117 if (!j.getTunnel()) {
118 j.e.log(3, "Job \"" + j.getName() + "\" failed before tunnel was set.", j);
119 }
120 if (!j.getNest()) {
121 j.e.log(3, "Job \"" + j.getName() + "\" does not have a nest.", j);
122 }
123 j.tunnel.executeFail(j, j.getNest(), reason);
124 };
125 /**
126 * Transfer a job to another tunnel directly.
127 * @param tunnel
128 */
129 Job.prototype.transfer = function (tunnel) {
130 var job = this;
131 var oldTunnel = this.getTunnel();
132 job.setTunnel(tunnel);
133 tunnel.arrive(job, null);
134 job.e.log(1, "Transferred to Tunnel \"" + tunnel.getName() + "\".", job, [oldTunnel]);
135 job.createLifeEvent("transfer", oldTunnel.getName(), tunnel.getName());
136 };
137 /**
138 * Move function error.
139 */
140 Job.prototype.move = function (destinationNest, callback) {
141 throw "This type of job cannot be moved.";
142 };
143 /**
144 * Sends an email.
145 * @param emailOptions Email options
146 * #### Sending pug template email example
147 * ```js
148 * // my_tunnel.js
149 * tunnel.run(function (job, nest) {
150 * job.email({
151 * subject: "Test email from pug template",
152 * to: "john.smith@example.com",
153 * template: "./template_files/my_email.pug"
154 * });
155 * });
156 * ```
157 *
158 * ```js
159 * // template_files/my_email.pug
160 * h1="Example email!"
161 * p="Got job ID " + job.getId()
162 * ```
163 * #### Sending plain-text email
164 * ```js
165 * tunnel.run(function (job, nest) {
166 * job.email({
167 * subject: "Test email with hard-coded plain-text",
168 * to: "john.smith@example.com",
169 * text: "My email body!"
170 * });
171 * });
172 * ```
173 * #### Sending html email
174 * ```js
175 * tunnel.run(function (job, nest) {
176 * job.email({
177 * subject: "Test email with hard-coded html",
178 * to: "john.smith@example.com",
179 * html: "<h1>My email body!</h1>"
180 * });
181 * });
182 * ```
183 */
184 Job.prototype.email = function (emailOptions) {
185 var job = this;
186 var emailer = job.e.getEmailer();
187 emailer.sendMail(emailOptions, job);
188 };
189 return Job;
190}());
191exports.Job = Job;