Indefero

Indefero Commit Details


Date:2010-05-06 03:27:08 (14 years 7 months ago)
Author:Loic d'Anterroches
Branch:develop, feature-issue_links, feature.better-home, feature.content-md5, feature.diff-whitespace, feature.download-md5, feature.issue-links, feature.issue-of-others, feature.issue-summary, feature.search-filter, feature.webrepos, feature.wiki-default-page, master, release-1.1, release-1.2, release-1.3
Commit:2f22d48dd0a046956b20763722c3bb0ea3dcf85d
Parents: d7843a55bd0552d575eeb5b1e9d6d4e8d024cc24
Message:Added the queue system to handle the webhooks and asynchronous events.

Changes:

File differences

src/IDF/Migrations/14Queue.php
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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero 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 2 of the License, or
# (at your option) any later version.
#
# InDefero 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Add the new IDF_Queue model.
*
*/
function IDF_Migrations_14Queue_up($params=null)
{
$models = array(
'IDF_Queue',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
foreach ($models as $model) {
$schema->model = new $model();
$schema->createTables();
}
}
function IDF_Migrations_14Queue_down($params=null)
{
$models = array(
'IDF_Queue',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
foreach ($models as $model) {
$schema->model = new $model();
$schema->dropTables();
}
}
src/IDF/Migrations/Backup.php
5151
5252
5353
54
5455
5556
5657
......
9495
9596
9697
98
9799
98100
99101
'IDF_Review_FileComment',
'IDF_Key',
'IDF_Scm_Cache_Git',
'IDF_Queue',
);
$db = Pluf::db();
// Now, for each table, we dump the content in json, this is a
'IDF_Review_FileComment',
'IDF_Key',
'IDF_Scm_Cache_Git',
'IDF_Queue',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
src/IDF/Migrations/Install.php
4848
4949
5050
51
5152
5253
5354
......
8586
8687
8788
89
8890
8991
9092
'IDF_Review_FileComment',
'IDF_Key',
'IDF_Scm_Cache_Git',
'IDF_Queue',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
$perm = Pluf_Permission::getFromString('IDF.project-authorized-user');
if ($perm) $perm->delete();
$models = array(
'IDF_Queue',
'IDF_Scm_Cache_Git',
'IDF_Key',
'IDF_Review_FileComment',
src/IDF/Queue.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero 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 2 of the License, or
# (at your option) any later version.
#
# InDefero 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
n# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Queue system for the management of asynchronous operations.
*
* Anybody can add an item to the queue and any application can
* register itself to process an item from the queue.
*
* An item in the queue is considered as fully processed when all the
* handlers have processed it successfully.
*
* To push a new item in the queue:
*
* <code>
* $item = new IDF_Queue();
* $item->type = 'new_commit';
* $item->payload = array('what', 'ever', array('data'));
* $item->create();
* </code>
*
* To process one item from the queue, you first need to register an
* handler, by adding the following in your relations.php file before
* the return statement or in your config file.
*
* <code>
* Pluf_Signal::connect('IDF_Queue::processItem',
* array('YourApp_Class', 'processItem'));
* </code>
*
* The processItem method will be called with two arguments, the first
* is the name of the signal ('IDF_Queue::processItem') and the second
* is an array with:
*
* <code>
* array('item' => $item,
* 'res' => array('OtherApp_Class::handler' => false,
* 'FooApp_Class::processItem' => true));
* </code>
*
* When you process an item, you need first to check if the type is
* corresponding to what you want to work with, then you need to check
* in 'res' if you have not already processed successfully the item,
* that is the key 'YourApp_Class::processItem' must be set to true,
* and then you can process the item. At the end of your processing,
* you need to modify by reference the 'res' key to add your status.
*
* All the data except for the type is in the payload, this makes the
* queue flexible to manage many different kind of tasks.
*
*/
class IDF_Queue extends Pluf_Model
{
public $_model = __CLASS__;
function init()
{
$this->_a['table'] = 'idf_queue';
$this->_a['model'] = __CLASS__;
$this->_a['cols'] = array(
// It is mandatory to have an "id" column.
'id' =>
array(
'type' => 'Pluf_DB_Field_Sequence',
'blank' => true,
),
'status' =>
array(
'type' => 'Pluf_DB_Field_Integer',
'blank' => false,
'choices' => array(
'pending' => 0,
'in_progress' => 1,
'need_retry' => 2,
'done' => 3,
'error' => 4,
),
'default' => 0,
),
'trials' =>
array(
'type' => 'Pluf_DB_Field_Integer',
'default' => 0,
),
'type' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => false,
'size' => 50,
),
'payload' =>
array(
'type' => 'Pluf_DB_Field_Serialized',
'blank' => false,
),
'results' =>
array(
'type' => 'Pluf_DB_Field_Serialized',
'blank' => false,
),
'lasttry_dtime' =>
array(
'type' => 'Pluf_DB_Field_Datetime',
'blank' => true,
),
'creation_dtime' =>
array(
'type' => 'Pluf_DB_Field_Datetime',
'blank' => true,
),
);
}
function preSave($create=false)
{
if ($create) {
$this->creation_dtime = gmdate('Y-m-d H:i:s');
$this->lasttry_dtime = gmdate('Y-m-d H:i:s');
$this->results = array();
}
}
/**
* The current item is going to be processed.
*/
function processItem()
{
/**
* [signal]
*
* IDF_Queue::processItem
*
* [sender]
*
* IDF_Queue
*
* [description]
*
* This signal allows an application to run an asynchronous
* job. The handler gets the queue item and the results from
* the previous run. If the handler key is not set, then the
* job was not run. If set it can be either true (already done)
* or false (error at last run).
*
* [parameters]
*
* array('item' => $item, 'res' => $res)
*
*/
$params = array('item' => $this, 'res' => $this->results);
Pluf_Signal::send('IDF_Queue::processItem',
'IDF_Queue', $params);
$this->status = 3; // Success
foreach ($params['res'] as $handler=>$ok) {
if (!$ok) {
$this->status = 2; // Set to need retry
$this->trials += 1;
break;
}
}
$this->results = $params['res'];
$this->lasttry_dtime = gmdate('Y-m-d H:i:s');
$this->update();
}
}

Archive Download the corresponding diff file

Page rendered in 0.08640s using 13 queries.