Last active
September 8, 2016 12:31
-
-
Save matthill/635f3f572246dc37e11e242bdb1ee733 to your computer and use it in GitHub Desktop.
Beanstalk library crashes when beanstalk is stopped/started
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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