-
Notifications
You must be signed in to change notification settings - Fork 6
/
github-event-task.js
120 lines (93 loc) · 4.58 KB
/
github-event-task.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
/***************************************************************************/
/* Augeo.io is a web application that uses Natural Language Processing to */
/* classify a user's internet activity into different 'skills'. */
/* Copyright (C) 2016 Brian Redd */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/
/***************************************************************************/
/* Description: Object to manage Github event queue tasks */
/***************************************************************************/
// Required local modules
var AbstractObject = require('../../public/javascript/common/abstract-object');
var AbstractQueueTask = require('../abstract-queue-task');
var GithubInterfaceService = require('../../interface-service/github-interface-service');
var Logger = require('../../module/logger');
// Global variables
var log = new Logger();
// Constants
var TASK = 'github-queue-task';
// Access with $this.variable inside of class and class.variable outside
function publicStaticVariables($this)
{
var maxRequests = 10;
var window = 60;
var requestsPerWindow = 5000;
$this.MAX_EXECUTION_TIME = ((window / requestsPerWindow) * maxRequests * 60) + 300/2; // 2 commit requests per second for 300 requests
}
// Constructor
var $this = function(user, githubData, lastEventId, logData) {
log.functionCall(TASK, 'constructor', logData.parentProcess, logData.username, {'userId': (user)?user._id:'invalid',
'screenName': (githubData)?githubData.screenName:'invalid', 'lastEventId': lastEventId});
// Call parent constructor
$this.base.constructor.call(this, user);
// public variables
this.accessToken = githubData.accessToken;
this.commits = new Array();
this.eTag = null;
this.isPoll = false; // Required field for revolving queues
this.lastEventId = lastEventId;
this.path = '/users/' + githubData.screenName + '/events';
this.poll = 60000;
this.screenName = githubData.screenName;
};
publicStaticVariables($this);
AbstractObject.extend(AbstractQueueTask, $this, {
execute: function(logData, callback) {
log.functionCall(TASK, 'execute', logData.parentProcess, logData.username);
var task = this;
GithubInterfaceService.getCommits(this.user, this.accessToken, this.path, this.eTag, this.lastEventId, logData, function(result) {
task.commits = task.commits.concat(result.commits);
// If it's the first request..
if(task.path.indexOf('?page=') < 0) {
// Only set eTag if it is the initial task request
task.eTag = result.eTag;
// Set lastEventId to null, it will be updated when all new commits are retrieved
if(task.commits.length > 0) {
task.isPoll = false;
task.lastEventId = null;
}
}
task.path = result.path;
// Last request
if(!task.path) {
// Capture lastEventId
if(task.commits.length > 0) {
task.lastEventId = task.commits[0].eventId;
}
task.isPoll = true;
}
task.poll = result.poll;
task.wait = result.wait;
callback(task);
});
},
reset: function(logData) {
log.functionCall(TASK, 'reset', logData.parentProcess, logData.username, {'screenName':this.screenName});
this.commits.length = 0;
this.path = '/users/' + this.screenName + '/events';
this.wait = 0;
}
});
module.exports = $this;