This repository has been archived by the owner on Jan 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Job.js
147 lines (142 loc) · 4.49 KB
/
Job.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* global App */
define([
'ext/jquery.ext',
'ember-model',
'models/TravisModel',
'models/Repo',
'models/Log',
'ext/Helpers',
'ext/DurationCalculations',
'ext/TravisAjax'
], function ($, Ember, TravisModel, Repo, Log, Helpers, DurationCalculations, TravisAjax) {
var Job = TravisModel.extend(DurationCalculations, {
repoId : Ember.attr('string', {key : 'repository_id'}),
buildId : Ember.attr('string'),
commitId : Ember.attr('string'),
logId : Ember.attr('string'),
queue : Ember.attr('string'),
state : Ember.attr('string'),
number : Ember.attr(Number),
startedAt : Ember.attr('string'),
finishedAt : Ember.attr('string'),
allowFailure : Ember.attr('boolean'),
repositorySlug : Ember.attr('string'),
repo : Ember.belongsTo('App.Repo', {key : 'repository_id'}),
build : Ember.belongsTo('App.Build'),
commit : Ember.belongsTo('App.Commit'),
_config : Ember.attr('object', {key : 'config'}),
log : function () {
this.set('isLogAccessed', true);
return Log.create({
job : this
});
}.property(),
repoSlug : function () {
return this.get('repositorySlug');
}.property('repositorySlug'),
config : function () {
return Helpers.compact(this.get('_config'));
}.property('_config'),
isFinished : function () {
var _ref;
return (_ref = this.get('state')) === 'passed' || _ref === 'failed' || _ref === 'errored' || _ref === 'canceled';
}.property('state'),
clearLog : function () {
if (this.get('isLogAccessed')) {
return this.get('log').clear();
}
},
configValues : function () {
var buildConfig, config, keys;
config = this.get('config');
buildConfig = this.get('build.config');
if (config && buildConfig) {
keys = $.intersect($.keys(buildConfig), Helpers.CONFIG_KEYS);
return keys.map(function (key) {
return config[key];
});
} else {
return [];
}
}.property('config'),
canCancel : function () {
return this.get('state') === 'created' || this.get('state') === 'queued';
}.property('state'),
cancel : function () {
return TravisAjax.post("/jobs/" + (this.get('id')), {
_method : 'delete'
});
},
requeue : function () {
return TravisAjax.post('/requests', {
job_id : this.get('id')
});
},
appendLog : function (part) {
return this.get('log').append(part);
},
subscribe : function () {
if (this.get('subscribed')) {
return;
}
this.set('subscribed', true);
return App.pusher.subscribe("job-" + (this.get('id')));
},
unsubscribe : function () {
if (!this.get('subscribed')) {
return;
}
this.set('subscribed', false);
return App.pusher.unsubscribe("job-" + (this.get('id')));
},
onStateChange : function () {
if (this.get('state') === 'finished' && App.pusher) {
return App.pusher.unsubscribe("job-" + (this.get('id')));
}
}.observes('state'),
isPropertyLoaded : function (key) {
if (['finishedAt'].contains(key) && !this.get('isFinished')) {
return true;
} else if (key === 'startedAt' && this.get('state') === 'created') {
return true;
} else {
return this._super(key);
}
}
});
Job.reopenClass({
queued : function () {
var filtered;
filtered = Ember.FilteredRecordArray.create({
modelClass : App.Job,
filterFunction : function (job) {
return ['created', 'queued'].indexOf(job.get('state')) !== -1;
},
filterProperties : ['state', 'queue']
});
this.fetch().then(function (/* array */) {
filtered.updateFilter();
return filtered.set('isLoaded', true);
});
return filtered;
},
running : function () {
var filtered;
filtered = Ember.FilteredRecordArray.create({
modelClass : App.Job,
filterFunction : function (job) {
return job.get('state') === 'started';
},
filterProperties : ['state']
});
this.fetch({
state : 'started'
}).then(function (/* array */) {
filtered.updateFilter();
return filtered.set('isLoaded', true);
});
return filtered;
}
});
return Job;
});