Pluf Framework

Sign in or create your account | Project List | Help

Pluf Framework Git Source Tree

Root/src/Pluf/Queue.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of Plume Framework, a simple PHP Application Framework.
6# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
7#
8# Plume Framework is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# Plume Framework is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24/**
25 * Simple queue system to delay the processing of tasks.
26 *
27 * What you do is that you push in the queue an object and what was
28 * done on the object. Then later one, a simple script will go through
29 * the queue and will check if something has to be done for the given
30 * object.
31 *
32 * For example, you have articles with for each articles a list of
33 * authors. You want to index the article with also the name of the
34 * authors. So it means that you need to update the index when an
35 * author is changed and when an article is changed. But you do not
36 * want to do the indexing of all the articles of an author when you
37 * update the author information (if this take 0.5s per article, with
38 * 100 articles, you would have to wait nearly 1 minute!).
39 *
40 * So when you update an author you push in the queue: "author x has
41 * been updated". Then you have a script that will go in the queue,
42 * find that the author has been updated and index each of his
43 * articles.
44 */
45class Pluf_Queue extends Pluf_Model
46{
47    public $_model = __CLASS__;
48
49    function init()
50    {
51        $this->_a['verbose'] = __('message queue');
52        $this->_a['table'] = 'pluf_queue';
53        $this->_a['model'] = __CLASS__;
54        $this->_a['cols'] = array(
55                             // It is mandatory to have an "id" column.
56                            'id' =>
57                            array(
58                                  'type' => 'Pluf_DB_Field_Sequence',
59                                  //It is automatically added.
60                                  'blank' => true,
61                                  ),
62                            'model_class' =>
63                            array(
64                                  'type' => 'Pluf_DB_Field_Varchar',
65                                  'blank' => false,
66                                  'size' => 150,
67                                  'verbose' => __('model class'),
68                                  ),
69                            'model_id' =>
70                            array(
71                                  'type' => 'Pluf_DB_Field_Integer',
72                                  'blank' => false,
73                                  'verbose' => __('model id'),
74                                  ),
75                            'action' =>
76                            array(
77                                  'type' => 'Pluf_DB_Field_Varchar',
78                                  'blank' => false,
79                                  'size' => 150,
80                                  'verbose' => __('action'),
81                                  ),
82                            'lock' =>
83                            array(
84                                  'type' => 'Pluf_DB_Field_Integer',
85                                  'blank' => false,
86                                  'verbose' => __('lock status'),
87                                  'default' => 0,
88                                  'choices' => array(
89                                                     __('Free') => 0,
90                                                     __('In progress') => 1,
91                                                     __('Completed') => 2,
92                                                     ),
93                                  ),
94                            'creation_dtime' =>
95                            array(
96                                  'type' => 'Pluf_DB_Field_Datetime',
97                                  'blank' => true,
98                                  'verbose' => __('created at'),
99                                  ),
100                            'modif_dtime' =>
101                            array(
102                                  'type' => 'Pluf_DB_Field_Datetime',
103                                  'blank' => true,
104                                  'verbose' => __('modified at'),
105                                  ),
106                            );
107        $this->_a['idx'] = array(
108                            'lock_idx' =>
109                            array(
110                                  'type' => 'normal',
111                                  'col' => 'lock',
112                                  ),
113                            );
114    }
115
116    function preSave($create=false)
117    {
118        if ($this->id == '') {
119            $this->creation_dtime = gmdate('Y-m-d H:i:s');
120        }
121        $this->modif_dtime = gmdate('Y-m-d H:i:s');
122    }
123
124    /**
125     * Add an object to the queue.
126     *
127     * @param Pluf_Model Your model
128     * @param string Action for the object
129     */
130    public static function addTo($object, $action='')
131    {
132        $q = new Pluf_Queue();
133        $q->model_class = $object->_model;
134        $q->model_id = $object->id;
135        $q->lock = 0;
136        $q->action = $action;
137        $q->create();
138    }
139}
140
141

Archive Download this file

Branches:
develop
master