ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If I store it in a DB/File, I assume you want to setup cron job every minute to kick off foobar, then foobar will grab the list of jobs and execute them.
This won't work in my situation because the reason I am trying to implement the queue is to avoid waiting. I want the jobs to be done as them come in.
Thanks for your advise. Please let me know if you have some more.
I see. So I can write a server that listen for jobs/query/remove coming from Web interface, and maintain the queue inside the server. I assume that's RPC? Any lead on that if I write it in Perl?
It sounds like maybe you want some sort of message queue system. IBM has one called MQ that comes with WebSphere. I don't know the details of it, but I have looked at it briefly. It may be a bit "full-featured" for your needs, though.
There is also a POSIX message queue supported by gcc that I had used at one time, but I don't remember the specifics of it. Do a google search on "Linux message queue" and you will likely find some stuff...
I'll see if I can find some of the example code I had written when looking at that stuff for you.
Edit:
I found a simple test program that uses the message queue stuff. It does it all in one app, but the send/recv stuff could be done in separate apps. I haven't looked at this code in years, so I can't guarantee it's the most clean...
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <errno.h>
#include <iostream>
// Message buffer struct
struct msgbuff_test1
{
long mtype; // Type of message
char mtext[1024]; // You can specify the size you need
};
const int MSGTYPE_TEST1 = 1; // ID for this message type
int g_qid;
using namespace std;
int main()
{
int rval;
// Create/get the queue
g_qid = msgget(0, S_IRWXU);
if (g_qid == -1)
{
cerr << "msgget() failed - " << errno << endl;
return -1;
}
else
cout << "MsgQueue created" << endl;
// Put a message on the queue
msgbuff_test1 s;
s.mtype=MSGTYPE_TEST1;
strcpy(s.mtext, "test");
rval = msgsnd(g_qid, &s, sizeof(msgbuff_test1), 0);
if (rval == -1)
{
cerr << "Error putting message on the queue - " << errno << endl;
return -1;
}
else
cout << "Message placed on the queue" << endl;
// Get a message off the queue
msgbuff_test1 r;
rval = msgrcv(g_qid, &r, sizeof(msgbuff_test1), MSGTYPE_TEST1, 0);
if (rval == -1)
{
cerr << "Error getting message from the queue - " << errno << endl;
}
else
cout << "Message retrieved from the queue - " << r.mtext << endl;
return 0;
}
Interesting question. I wouldn't give up on the database idea (just yet), and the odds are pretty good that you can do most (perhaps all) of it in Perl.
1. You've got (at least) two guys:
a) The web interface for monitoring and writing to the queue
b) Some "worker agent" who processes items in the queue
2. Let's assume you implement the queue with a database:
The web side becomes very simple: you can write it as a Perl
CGI (probably using the Perl CGI module to make your life easy).
Your "database" consists of two tables: one for the "work items", the
other holds the "head pointer" and the "tail pointer".
If you use the Perl DBI module, the database logic is a piece of cake.
If your database supports transactions (begin tran/commit/rollback), then
you've also solved any potential concurrency problems (at least on the web side)
3. If your database supports triggers, and if the trigger can launch a system
command, then you've also solved the "worker agent" part of the equation: you
just process each new command as it comes in, and the system does the queueing
for you.
MS-SQL Server, for example, meets all of these criteria.
Oh yeah - and you can easily open a security hole the size of the Grand Canyon
if you're not careful ;-)!
4. But let's say that instead you wanted to write the "worker agent" in Perl, too.
OK - it would probably be a separate process.
5. The easy thing would be for it to simply poll the database for any new
jobs to process.
6. Alternatively, you could create a message queue with the IPC:SysV module.
You would *not* use the message queue to communicate the job details
(remember - that's in the database).
Instead, you would have the "worker agent" *block* on the message queue
whenever it becomes empty. It will then sleep until somebody (i.e. your web
CGI) sends a "wakeup signal". At which point the "worker agent" wakes up
and (using the Perl DBI module) reads all of the work items (we know there's
at least one, and potentially mode) in the queue.
The only advantage of the message queue (or any other "signal" you might
devise) is that it saves you the overhead of polling.
That's true if you're just putting work items in and taking them out.
But you'll recall that another requirement was the ability to monitor (and perhaps administer) what's *in* the queue.
Throw concurrency issues into the mix, add the fact that Perl:BI is Good (and that the author wants to write in Perl, if at all possible) ... and suddenly databases don't look so unattractive anymore.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.