Solved

How do I make sure a file is processed before it is altered?

Posted on 2011-02-16
12
232 Views
Last Modified: 2012-06-27
A remote server sends data to me (POST). I have a PHP script (receive.php) that receives the data and store it in a file. Then, a totally different script (process.php) reads that file, process its content and empty the file (this way it will prepare the file for the next write and it won't process the same data twice).

But the remote server may send data to my receive.php script while process.php script is still processing the file. How do I solve this problem?
0
Comment
Question by:cubic77
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34905897
0
 
LVL 4

Expert Comment

by:florjan
ID: 34905921
Php file operators. So check if file is not null. If it isnt write to other files. You could have a for sentence for less code. If you need anymore help I'll help you as soon as I come home.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34905956
A queue is your best bet.

Instead of allowing the scripts to access the file directly, get them to write into a table which holds the file's name and the next operation as well as any data. A separate script can then be run on a schedule to inspect the queue and process any work

-- UNTESTED --

CREATE TABLE processQueue (

  id INT NOT NULL auto_increment,
  filename VARCHAR(255) NOT NULL,
  command VARCHAR(32) NOT NULL,
  data TEXT NOT NULL,
  PRIMARY KEY(id)  

);


Then INSERT INTO processQueue ( filename, command, data ) VALUES (........ etc.....);

Have your scheduled script pull off the oldest record

SELECT * FROM processQueue ORDER BY filename, id LIMIT 1

apply the changes and then remove the work record

DELETE FROM processQueue where id = $id

This ensures that all operations on a file are performed sequentially. Have the job run every few minutes and if the queue is empty then just terminate the job. If you know that the changes will take fraction of a second then you might decide to process up to 10 commands or whatever works for you before you run out of processor time.

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 8

Expert Comment

by:rationalboss
ID: 34906867
Using a database will be helpful. But if you don't want to use a database, you can try the following.

Since you might still want to allow your application to receive updates even though there is still a file being processed, just allow the page to continue receiving requests. (that means, not locking the file)

My idea is to create a temporary directory that contains files you want processed. For example, you can create a queue/ directory. Each time a file is received, you put a file in that directory with a random file name. You can use file_put_contents() or move_uploaded_file() or simply rename(), you weren't able to mention how you receive files in receive.php.


Now, the other file which processes these files, loops to check for files in that directory:
<?php
$dir = "queue/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false && filetype($dir . $file) == 'file') {
            // this is a file in the queue directory. Now, do something with $dir.$file
            // and make sure you delete the file after to indicate you're done processing it:
            // unlink($dir.$file);
        }
        closedir($dh);
    }
}
?>

... and delete the file after processing it. This way, you can accept multiple files for processing and have it processed sequentially and you don't need to use a database. The readdir() function would return the files in the order in which they are created in the file system.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34907173
This sounds like a strange application design.  Regarding this, "a totally different script (process.php) reads that file" -- how does that script get started?  And why would you want to re-use the file name?  Why not have the receive.php script collect the data, write the file, and then start the process.php script?  You would want to use a different file name (maybe using a timestamp) for each received data set.  Once process.php has completed its work, it could unlink() the file.
0
 

Author Comment

by:cubic77
ID: 34907873
@Ray_Paseur

> Why not have the receive.php script collect the data, write the file, and then start the process.php script?
Actually this is what I am doing right now.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34908374
If you're doing it that way and you use a different file name for each new file, you should be on firm ground, at least with this end of the application.
0
 

Author Comment

by:cubic77
ID: 34915112
>If you're doing it that way and you use a different file name for each new file, you should be on firm ground
Indeed it may be the best approach. Also, this way I can remove the need of using a data base to store my data. I can read the data directly from those files.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34915456
That may be true.  Not sure what kind of data you have, but larger "blobs" of information like images or video are better in the file system, whereas smaller, succinct, searchable pieces seem to be most useful in a data base.  It's part art and part science ;)
0
 

Accepted Solution

by:
cubic77 earned 0 total points
ID: 34915495
I totally agree with databases. However, not storing the data in a DB has an important advantage to me. I can move the application (PHP script) and associated data to any server I want in less than 60 seconds. No DB setup nightmare.
0
 

Author Closing Comment

by:cubic77
ID: 36123171
no other solid solution provided
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question