Skip to content

Instantly share code, notes, and snippets.

@matthill
Last active September 8, 2016 12:31
Show Gist options
  • Save matthill/635f3f572246dc37e11e242bdb1ee733 to your computer and use it in GitHub Desktop.
Save matthill/635f3f572246dc37e11e242bdb1ee733 to your computer and use it in GitHub Desktop.
Beanstalk library crashes when beanstalk is stopped/started
/*
* File: beanstalk_test.cpp
* Author: mhill
*
* Created on September 8, 2016, 7:20 AM
*/
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include "beanstalk.hpp"
using namespace std;
void dataUploadThread(void* arg);
int main(int argc, char** argv) {
dataUploadThread(NULL);
return 0;
}
void dataUploadThread(void* arg)
{
bool daemon_active = true;
const std::string BEANSTALK_QUEUE_HOST = "localhost";
const std::string BEANSTALK_TUBE_NAME = "test";
const int BEANSTALK_PORT = 11300;
cout << "Starting function" << endl;
while(daemon_active)
{
try
{
cout << "Loop 1" << endl;
Beanstalk::Client client(BEANSTALK_QUEUE_HOST, BEANSTALK_PORT);
bool watch_success = client.watch(BEANSTALK_TUBE_NAME);
cout << "Watch success: " << watch_success << endl;
while (daemon_active)
{
cout << "Loop 2" << endl;
Beanstalk::Job job;
bool beanstalk_success = client.reserve(job, 1);
cout << "Beanstalk success: " << beanstalk_success << endl;
cout << "Got job: " << job.id() << endl;
if (job.id() > 0)
{
std::string json;
json = job.body();
bool uploadSuccess = rand() > 0.5;
if (uploadSuccess)
{
client.del(job.id());
cout << "success -- removing job";
}
else
{
client.release(job);
cout << "failed -- leaving job alone";
}
}
}
}
catch (const std::runtime_error& error)
{
cout << "Caught exception" << endl;
}
// wait 5 seconds
usleep(500000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment