pluf2

pluf2 Git Source Tree


Root/src/Pluf/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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser 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 ***** */
 
/**
 * Simple queue system to delay the processing of tasks.
 *
 * What you do is that you push in the queue an object and what was
 * done on the object. Then later one, a simple script will go through
 * the queue and will check if something has to be done for the given
 * object.
 *
 * For example, you have articles with for each articles a list of
 * authors. You want to index the article with also the name of the
 * authors. So it means that you need to update the index when an
 * author is changed and when an article is changed. But you do not
 * want to do the indexing of all the articles of an author when you
 * update the author information (if this take 0.5s per article, with
 * 100 articles, you would have to wait nearly 1 minute!).
 *
 * So when you update an author you push in the queue: "author x has
 * been updated". Then you have a script that will go in the queue,
 * find that the author has been updated and index each of his
 * articles.
 */
class Pluf_Queue extends Pluf_Model
{
    public $_model = __CLASS__;
 
    function init()
    {
        $this->_a['verbose'] = __('message queue');
        $this->_a['table'] = 'pluf_queue';
        $this->_a['model'] = __CLASS__;
        $this->_a['cols'] = array(
                             // It is mandatory to have an "id" column.
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  //It is automatically added.
                                  'blank' => true,
                                  ),
                            'model_class' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 150,
                                  'verbose' => __('model class'),
                                  ),
                            'model_id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  'verbose' => __('model id'),
                                  ),
                            'action' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 150,
                                  'verbose' => __('action'),
                                  ),
                            'lock' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  'verbose' => __('lock status'),
                                  'default' => 0,
                                  'choices' => array(
                                                     __('Free') => 0,
                                                     __('In progress') => 1,
                                                     __('Completed') => 2,
                                                     ),
                                  ),
                            'creation_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('created at'),
                                  ),
                            'modif_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('modified at'),
                                  ),
                            );
        $this->_a['idx'] = array(                          
                            'lock_idx' =>
                            array(
                                  'type' => 'normal',
                                  'col' => 'lock',
                                  ),
                            );
    }
 
    function preSave($create=false)
    {
        if ($this->id == '') {
            $this->creation_dtime = gmdate('Y-m-d H:i:s');
        }
        $this->modif_dtime = gmdate('Y-m-d H:i:s');
    }
 
    /**
     * Add an object to the queue.
     *
     * @param Pluf_Model Your model
     * @param string Action for the object
     */
    public static function addTo($object, $action='')
    {
        $q = new Pluf_Queue();
        $q->model_class = $object->_model;
        $q->model_id = $object->id;
        $q->lock = 0;
        $q->action = $action;
        $q->create();
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.08413s using 11 queries.