1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534 |
- /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- *
- * Gearmand client and server library.
- *
- * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
- * Copyright (C) 2008 Brian Aker, Eric Day
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *
- * * The names of its contributors may not be used to endorse or
- * promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include "gear_config.h"
- #include <libgearman/common.h>
- #include "libgearman/uuid.hpp"
- #include <libgearman/function/base.hpp>
- #include <libgearman/function/make.hpp>
- #include "libgearman/assert.hpp"
- #include "libgearman/log.hpp"
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <memory>
- #include <unistd.h>
- #include <fcntl.h>
- #include <cerrno>
- /**
- * @addtogroup gearman_worker_static Static Worker Declarations
- * @ingroup gearman_worker
- * @{
- */
- static inline struct _worker_function_st *_function_exist(Worker* worker, const char *function_name, const size_t function_length)
- {
- struct _worker_function_st *function;
- for (function= worker->function_list; function;
- function= function->next)
- {
- if (function_length == function->function_length())
- {
- if (memcmp(function_name, function->function_name(), function_length) == 0)
- {
- break;
- }
- }
- }
- return function;
- }
- /**
- * Allocate a worker structure.
- */
- static gearman_worker_st *_worker_allocate(gearman_worker_st *worker, bool is_clone);
- /**
- * Initialize common packets for later use.
- */
- static gearman_return_t _worker_packet_init(Worker*);
- /**
- * Callback function used when parsing server lists.
- */
- static gearman_return_t _worker_add_server(const char *host, in_port_t port, void *context);
- /**
- * Allocate and add a function to the register list.
- */
- static gearman_return_t _worker_function_create(Worker *worker,
- const char *function_name, const size_t function_length,
- const gearman_function_t &function,
- uint32_t timeout,
- void *context);
- /**
- * Free a function.
- */
- static void _worker_function_free(Worker* worker,
- struct _worker_function_st *function);
- /** @} */
- /*
- * Public Definitions
- */
- gearman_worker_st *gearman_worker_create(gearman_worker_st *worker_shell)
- {
- worker_shell= _worker_allocate(worker_shell, false);
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- gearman_return_t ret;
- if (gearman_failed((ret= _worker_packet_init(worker))))
- {
- gearman_worker_free(worker_shell);
- return NULL;
- }
- }
- return worker_shell;
- }
- gearman_worker_st *gearman_worker_clone(gearman_worker_st *worker_shell,
- const gearman_worker_st *source_shell)
- {
- if (source_shell == NULL)
- {
- return gearman_worker_create(worker_shell);
- }
- worker_shell= _worker_allocate(worker_shell, true);
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- Worker* source= source_shell->impl();
- worker->options.change= source->options.change;
- worker->options.grab_uniq= source->options.grab_uniq;
- worker->options.grab_all= source->options.grab_all;
- worker->options.timeout_return= source->options.timeout_return;
- worker->ssl(source->ssl());
- gearman_universal_clone(worker->universal, source->universal);
- if (gearman_failed(_worker_packet_init(worker)))
- {
- gearman_worker_free(worker_shell);
- return NULL;
- }
- for (struct _worker_function_st* function= source->function_list;
- function;
- function= function->next)
- {
- _worker_function_create(worker,
- function->function_name(), function->function_length(),
- function->function(),
- function->timeout(),
- function->context());
- }
- }
- return worker_shell;
- }
- void gearman_worker_free(gearman_worker_st *worker_shell)
- {
- #ifndef NDEBUG
- if (worker_shell)
- {
- assert(worker_shell->impl());
- }
- #endif
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- gearman_worker_unregister_all(worker_shell);
- if (worker->options.packet_init)
- {
- gearman_packet_free(&worker->grab_job);
- gearman_packet_free(&worker->pre_sleep);
- }
- worker->job(NULL);
- if (worker->work_result)
- {
- gearman_free(worker->universal, worker->work_result);
- }
- while (worker->function_list)
- {
- _worker_function_free(worker, worker->function_list);
- }
- gearman_job_free_all(worker_shell);
- gearman_universal_free(worker->universal);
- delete worker;
- }
- }
- const char *gearman_worker_error(const gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- return worker_shell->impl()->universal.error();
- }
- return NULL;
- }
- int gearman_worker_errno(gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- return worker_shell->impl()->universal.last_errno();
- }
- return EINVAL;
- }
- gearman_worker_options_t gearman_worker_options(const gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- int options;
- memset(&options, 0, sizeof(gearman_worker_options_t));
- if (gearman_is_allocated(worker_shell))
- {
- options|= int(GEARMAN_WORKER_ALLOCATED);
- }
- if (worker->options.non_blocking)
- options|= int(GEARMAN_WORKER_NON_BLOCKING);
- if (worker->options.packet_init)
- options|= int(GEARMAN_WORKER_PACKET_INIT);
- if (worker->options.change)
- options|= int(GEARMAN_WORKER_CHANGE);
- if (worker->options.grab_uniq)
- options|= int(GEARMAN_WORKER_GRAB_UNIQ);
- if (worker->options.grab_all)
- options|= int(GEARMAN_WORKER_GRAB_ALL);
- if (worker->options.timeout_return)
- options|= int(GEARMAN_WORKER_TIMEOUT_RETURN);
- if (worker->ssl())
- options|= int(GEARMAN_WORKER_SSL);
- if (worker->has_identifier())
- options|= int(GEARMAN_WORKER_IDENTIFIER);
- return gearman_worker_options_t(options);
- }
- return gearman_worker_options_t();
- }
- void gearman_worker_set_options(gearman_worker_st *worker,
- gearman_worker_options_t options)
- {
- if (worker and worker->impl())
- {
- gearman_worker_options_t usable_options[]= {
- GEARMAN_WORKER_NON_BLOCKING,
- GEARMAN_WORKER_GRAB_UNIQ,
- GEARMAN_WORKER_GRAB_ALL,
- GEARMAN_WORKER_TIMEOUT_RETURN,
- GEARMAN_WORKER_SSL,
- GEARMAN_WORKER_IDENTIFIER,
- GEARMAN_WORKER_MAX
- };
- for (gearman_worker_options_t* ptr= usable_options; *ptr != GEARMAN_WORKER_MAX ; ++ptr)
- {
- if (options & *ptr)
- {
- gearman_worker_add_options(worker, *ptr);
- }
- else
- {
- gearman_worker_remove_options(worker, *ptr);
- }
- }
- }
- }
- void gearman_worker_add_options(gearman_worker_st *worker_shell,
- gearman_worker_options_t options)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- if (options & GEARMAN_WORKER_NON_BLOCKING)
- {
- gearman_universal_add_options(worker->universal, GEARMAN_UNIVERSAL_NON_BLOCKING);
- worker->options.non_blocking= true;
- }
- if (options & GEARMAN_WORKER_GRAB_UNIQ)
- {
- worker->grab_job.command= GEARMAN_COMMAND_GRAB_JOB_UNIQ;
- gearman_return_t rc= gearman_packet_pack_header(&(worker->grab_job));
- (void)(rc);
- assert(gearman_success(rc));
- worker->options.grab_uniq= true;
- }
- if (options & GEARMAN_WORKER_GRAB_ALL)
- {
- worker->grab_job.command= GEARMAN_COMMAND_GRAB_JOB_ALL;
- gearman_return_t rc= gearman_packet_pack_header(&(worker->grab_job));
- (void)(rc);
- assert(gearman_success(rc));
- worker->options.grab_all= true;
- }
- if (options & GEARMAN_WORKER_TIMEOUT_RETURN)
- {
- worker->options.timeout_return= true;
- }
- if (options & GEARMAN_WORKER_SSL)
- {
- worker->ssl(true);
- }
- if (options & GEARMAN_WORKER_IDENTIFIER)
- {
- char uuid_buffer[GEARMAN_MAX_IDENTIFIER];
- size_t length= GEARMAN_MAX_IDENTIFIER;
- safe_uuid_generate(uuid_buffer, length);
- worker->universal.identifier(uuid_buffer, length);
- }
- }
- }
- void gearman_worker_remove_options(gearman_worker_st *worker_shell,
- gearman_worker_options_t options)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- if (options & GEARMAN_WORKER_NON_BLOCKING)
- {
- gearman_universal_remove_options(worker->universal, GEARMAN_UNIVERSAL_NON_BLOCKING);
- worker->options.non_blocking= false;
- }
- if (options & GEARMAN_WORKER_TIMEOUT_RETURN)
- {
- worker->options.timeout_return= false;
- gearman_universal_set_timeout(worker->universal, GEARMAN_WORKER_WAIT_TIMEOUT);
- }
- if (options & GEARMAN_WORKER_GRAB_UNIQ)
- {
- worker->grab_job.command= GEARMAN_COMMAND_GRAB_JOB;
- (void)gearman_packet_pack_header(&(worker->grab_job));
- worker->options.grab_uniq= false;
- }
- if (options & GEARMAN_WORKER_GRAB_ALL)
- {
- worker->grab_job.command= GEARMAN_COMMAND_GRAB_JOB;
- (void)gearman_packet_pack_header(&(worker->grab_job));
- worker->options.grab_all= false;
- }
- if (options & GEARMAN_WORKER_IDENTIFIER)
- {
- worker->universal.identifier(NULL, 0);
- }
- }
- }
- int gearman_worker_timeout(gearman_worker_st *worker)
- {
- if (worker and worker->impl())
- {
- return gearman_universal_timeout(worker->impl()->universal);
- }
- return 0;
- }
- void gearman_worker_set_timeout(gearman_worker_st *worker, int timeout)
- {
- if (worker and worker->impl())
- {
- gearman_worker_add_options(worker, GEARMAN_WORKER_TIMEOUT_RETURN);
- gearman_universal_set_timeout(worker->impl()->universal, timeout);
- }
- }
- void *gearman_worker_context(const gearman_worker_st *worker)
- {
- if (worker and worker->impl())
- {
- return worker->impl()->context;
- }
- return NULL;
- }
- void gearman_worker_set_context(gearman_worker_st *worker, void *context)
- {
- if (worker and worker->impl())
- {
- worker->impl()->context= context;
- }
- }
- void gearman_worker_set_log_fn(gearman_worker_st *worker,
- gearman_log_fn *function, void *context,
- gearman_verbose_t verbose)
- {
- if (worker and worker->impl())
- {
- gearman_set_log_fn(worker->impl()->universal, function, context, verbose);
- }
- }
- void gearman_worker_set_workload_malloc_fn(gearman_worker_st *worker,
- gearman_malloc_fn *function,
- void *context)
- {
- if (worker and worker->impl())
- {
- gearman_set_workload_malloc_fn(worker->impl()->universal, function, context);
- }
- }
- void gearman_worker_set_workload_free_fn(gearman_worker_st *worker,
- gearman_free_fn *function,
- void *context)
- {
- if (worker and worker->impl())
- {
- gearman_set_workload_free_fn(worker->impl()->universal, function, context);
- }
- }
- gearman_return_t gearman_worker_add_server(gearman_worker_st *worker,
- const char *host, in_port_t port)
- {
- if (worker and worker->impl())
- {
- if (gearman_connection_create(worker->impl()->universal, host, port) == NULL)
- {
- return gearman_universal_error_code(worker->impl()->universal);
- }
- return GEARMAN_SUCCESS;
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- gearman_return_t gearman_worker_add_servers(gearman_worker_st *worker, const char *servers)
- {
- if (worker and worker->impl())
- {
- return gearman_parse_servers(servers, _worker_add_server, worker);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- void gearman_worker_remove_servers(gearman_worker_st *worker)
- {
- if (worker and worker->impl())
- {
- gearman_free_all_cons(worker->impl()->universal);
- }
- }
- gearman_return_t gearman_worker_wait(gearman_worker_st *worker)
- {
- if (worker and worker->impl())
- {
- return gearman_wait(worker->impl()->universal);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- gearman_return_t gearman_worker_register(gearman_worker_st *worker_shell,
- const char *function_name,
- uint32_t timeout)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- gearman_function_t null_func= gearman_function_create_null();
- return _worker_function_create(worker, function_name, strlen(function_name), null_func, timeout, NULL);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- bool gearman_worker_function_exist(gearman_worker_st *worker_shell,
- const char *function_name,
- size_t function_length)
- {
- if (worker_shell and worker_shell->impl())
- {
- struct _worker_function_st *function;
- Worker* worker= worker_shell->impl();
- function= _function_exist(worker, function_name, function_length);
- return (function && function->options.remove == false) ? true : false;
- }
- return false;
- }
- static inline gearman_return_t _worker_unregister(gearman_worker_st *worker_shell,
- const char *function_name, size_t function_length)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- _worker_function_st *function= _function_exist(worker, function_name, function_length);
- if (function == NULL or function->options.remove)
- {
- return GEARMAN_NO_REGISTERED_FUNCTION;
- }
- if (function->options.packet_in_use)
- {
- gearman_packet_free(&(function->packet()));
- function->options.packet_in_use= false;
- }
- const void *args[1];
- size_t args_size[1];
- args[0]= function->name();
- args_size[0]= function->length();
- gearman_return_t ret= gearman_packet_create_args(worker->universal, function->packet(),
- GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_CANT_DO,
- args, args_size, 1);
- if (gearman_failed(ret))
- {
- function->options.packet_in_use= false;
- return ret;
- }
- function->options.packet_in_use= true;
- function->options.change= true;
- function->options.remove= true;
- worker->options.change= true;
- return GEARMAN_SUCCESS;
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- gearman_return_t gearman_worker_unregister(gearman_worker_st *worker,
- const char *function_name)
- {
- return _worker_unregister(worker, function_name, strlen(function_name));
- }
- gearman_return_t gearman_worker_unregister_all(gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- struct _worker_function_st *function;
- uint32_t count= 0;
- if (worker->function_list == NULL)
- {
- return GEARMAN_NO_REGISTERED_FUNCTIONS;
- }
- /* Lets find out if we have any functions left that are valid */
- for (function= worker->function_list; function;
- function= function->next)
- {
- if (function->options.remove == false)
- {
- count++;
- }
- }
- if (count == 0)
- {
- return GEARMAN_NO_REGISTERED_FUNCTIONS;
- }
- gearman_packet_free(&(worker->function_list->packet()));
- gearman_return_t ret= gearman_packet_create_args(worker->universal,
- worker->function_list->packet(),
- GEARMAN_MAGIC_REQUEST,
- GEARMAN_COMMAND_RESET_ABILITIES,
- NULL, NULL, 0);
- if (gearman_failed(ret))
- {
- worker->function_list->options.packet_in_use= false;
- return ret;
- }
- while (worker->function_list->next)
- {
- _worker_function_free(worker, worker->function_list->next);
- }
- worker->function_list->options.change= true;
- worker->function_list->options.remove= true;
- worker->options.change= true;
- return GEARMAN_SUCCESS;
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- #if __GNUC__ >= 7
- #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
- #endif
- gearman_job_st *gearman_worker_grab_job(gearman_worker_st *worker_shell,
- gearman_job_st *job,
- gearman_return_t *ret_ptr)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- struct _worker_function_st *function;
- uint32_t active;
- bool no_job= false;
- if (worker->in_work() == false)
- {
- worker->universal.reset_error();
- }
- gearman_return_t unused;
- if (ret_ptr == NULL)
- {
- ret_ptr= &unused;
- }
- *ret_ptr= GEARMAN_MAX_RETURN;
- if (worker->universal.con_list == NULL)
- {
- *ret_ptr= GEARMAN_NO_SERVERS;
- return NULL;
- }
- while (1)
- {
- switch (worker->state)
- {
- case GEARMAN_WORKER_STATE_START:
- /* If there are any new functions changes, send them now. */
- if (worker->options.change)
- {
- worker->function= worker->function_list;
- while (worker->function)
- {
- if (not (worker->function->options.change))
- {
- worker->function= worker->function->next;
- continue;
- }
- for (worker->con= (&worker->universal)->con_list; worker->con;
- worker->con= worker->con->next_connection())
- {
- if (worker->con->socket_descriptor_is_valid() == false)
- {
- continue;
- }
- case GEARMAN_WORKER_STATE_FUNCTION_SEND:
- *ret_ptr= worker->con->send_packet(worker->function->packet(), true);
- if (gearman_failed(*ret_ptr))
- {
- if (*ret_ptr == GEARMAN_IO_WAIT)
- {
- worker->state= GEARMAN_WORKER_STATE_FUNCTION_SEND;
- }
- else if (*ret_ptr == GEARMAN_LOST_CONNECTION)
- {
- continue;
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- }
- if (worker->function->options.remove)
- {
- function= worker->function->prev;
- _worker_function_free(worker, worker->function);
- if (function == NULL)
- {
- worker->function= worker->function_list;
- }
- else
- {
- worker->function= function;
- }
- }
- else
- {
- worker->function->options.change= false;
- worker->function= worker->function->next;
- }
- }
- worker->options.change= false;
- }
- if (not worker->function_list)
- {
- gearman_error(worker->universal, GEARMAN_NO_REGISTERED_FUNCTIONS, "no functions have been registered");
- *ret_ptr= GEARMAN_NO_REGISTERED_FUNCTIONS;
- return NULL;
- }
- for (worker->con= (&worker->universal)->con_list; worker->con;
- worker->con= worker->con->next_connection())
- {
- /* If the connection to the job server is not active, start it. */
- if (worker->con->socket_descriptor_is_valid() == false)
- {
- for (worker->function= worker->function_list;
- worker->function;
- worker->function= worker->function->next)
- {
- case GEARMAN_WORKER_STATE_CONNECT:
- *ret_ptr= worker->con->send_packet(worker->function->packet(), true);
- if (gearman_failed(*ret_ptr))
- {
- if (*ret_ptr == GEARMAN_IO_WAIT)
- {
- worker->state= GEARMAN_WORKER_STATE_CONNECT;
- }
- else if (*ret_ptr == GEARMAN_COULD_NOT_CONNECT or *ret_ptr == GEARMAN_LOST_CONNECTION)
- {
- break;
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- }
- if (*ret_ptr == GEARMAN_COULD_NOT_CONNECT)
- {
- continue;
- }
- }
- /* fall-thru */
- case GEARMAN_WORKER_STATE_GRAB_JOB_SEND:
- if (worker->con->socket_descriptor_is_valid() == false)
- {
- continue;
- }
- *ret_ptr= worker->con->send_packet(worker->grab_job, true);
- if (gearman_failed(*ret_ptr))
- {
- if (*ret_ptr == GEARMAN_IO_WAIT)
- {
- worker->state= GEARMAN_WORKER_STATE_GRAB_JOB_SEND;
- }
- else if (*ret_ptr == GEARMAN_LOST_CONNECTION)
- {
- continue;
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- if (worker->job() == NULL)
- {
- assert(job == NULL);
- worker->job(gearman_job_create(worker, job));
- if (worker->job() == NULL)
- {
- *ret_ptr= GEARMAN_MEMORY_ALLOCATION_FAILURE;
- return NULL;
- }
- assert(worker->job()->impl());
- }
- while (1)
- {
- /* fall-thru */
- case GEARMAN_WORKER_STATE_GRAB_JOB_RECV:
- assert(worker);
- assert(worker->job());
- assert(worker->job()->impl());
- (void)worker->con->receiving(worker->job()->impl()->assigned, *ret_ptr, true);
- if (gearman_failed(*ret_ptr))
- {
- if (*ret_ptr == GEARMAN_IO_WAIT)
- {
- worker->state= GEARMAN_WORKER_STATE_GRAB_JOB_RECV;
- }
- else
- {
- worker->job(NULL);
- if (*ret_ptr == GEARMAN_LOST_CONNECTION)
- {
- break;
- }
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- if (worker->job()->impl()->assigned.command == GEARMAN_COMMAND_NOOP)
- {
- gearman_log_debug(worker->universal, "Received NOOP");
- }
- if (worker->job()->impl()->assigned.command == GEARMAN_COMMAND_JOB_ASSIGN or
- worker->job()->impl()->assigned.command == GEARMAN_COMMAND_JOB_ASSIGN_ALL or
- worker->job()->impl()->assigned.command == GEARMAN_COMMAND_JOB_ASSIGN_UNIQ)
- {
- worker->job()->impl()->options.assigned_in_use= true;
- worker->job()->impl()->con= worker->con;
- worker->state= GEARMAN_WORKER_STATE_GRAB_JOB_SEND;
- job= worker->take_job();
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return job;
- }
- if (worker->job()->impl()->assigned.command == GEARMAN_COMMAND_NO_JOB or
- worker->job()->impl()->assigned.command == GEARMAN_COMMAND_OPTION_RES)
- {
- no_job= true;
- gearman_packet_free(&(worker->job()->impl()->assigned));
- break;
- }
- if (worker->job()->impl()->assigned.command != GEARMAN_COMMAND_NOOP)
- {
- gearman_universal_set_error(worker->universal, GEARMAN_UNEXPECTED_PACKET, GEARMAN_AT,
- "unexpected packet:%s",
- gearman_command_info(worker->job()->impl()->assigned.command)->name);
- gearman_packet_free(&(worker->job()->impl()->assigned));
- worker->job(NULL);
- *ret_ptr= GEARMAN_UNEXPECTED_PACKET;
- return NULL;
- }
- gearman_packet_free(&(worker->job()->impl()->assigned));
- }
- }
- if (worker->in_work() == false and no_job)
- {
- *ret_ptr= GEARMAN_NO_JOBS;
- break;
- }
- /* fall-thru */
- case GEARMAN_WORKER_STATE_PRE_SLEEP:
- for (worker->con= (&worker->universal)->con_list; worker->con;
- worker->con= worker->con->next_connection())
- {
- if (worker->con->socket_descriptor_is_valid() == false)
- {
- continue;
- }
- *ret_ptr= worker->con->send_packet(worker->pre_sleep, true);
- if (gearman_failed(*ret_ptr))
- {
- if (*ret_ptr == GEARMAN_IO_WAIT)
- {
- worker->state= GEARMAN_WORKER_STATE_PRE_SLEEP;
- }
- else if (*ret_ptr == GEARMAN_LOST_CONNECTION)
- {
- continue;
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- }
- worker->state= GEARMAN_WORKER_STATE_START;
- /* Set a watch on all active connections that we sent a PRE_SLEEP to. */
- active= 0;
- for (worker->con= worker->universal.con_list; worker->con; worker->con= worker->con->next_connection())
- {
- if (worker->con->socket_descriptor_is_valid())
- {
- worker->con->set_events(POLLIN);
- active++;
- }
- }
- if ((&worker->universal)->options.non_blocking)
- {
- *ret_ptr= GEARMAN_NO_JOBS;
- return NULL;
- }
- if (active == 0)
- {
- if (*ret_ptr == GEARMAN_COULD_NOT_CONNECT)
- {
- return NULL;
- }
- else if (worker->universal.timeout < 0)
- {
- gearman_nap(GEARMAN_WORKER_WAIT_TIMEOUT);
- }
- else
- {
- if (worker->universal.timeout > 0)
- {
- gearman_nap(worker->universal);
- }
- if (worker->options.timeout_return)
- {
- *ret_ptr= gearman_error(worker->universal, GEARMAN_TIMEOUT, "Option timeout return reached");
- return NULL;
- }
- }
- }
- else
- {
- *ret_ptr= gearman_wait(worker->universal);
- if (gearman_failed(*ret_ptr) and (*ret_ptr != GEARMAN_TIMEOUT or worker->options.timeout_return))
- {
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- }
- break;
- }
- if (*ret_ptr == GEARMAN_NO_JOBS)
- {
- break;
- }
- }
- }
- assert(*ret_ptr != GEARMAN_MAX_RETURN);
- return NULL;
- }
- void gearman_job_free_all(gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- while (worker->job_list)
- {
- gearman_job_free(worker->job_list->shell());
- }
- }
- }
- gearman_return_t gearman_worker_add_function(gearman_worker_st *worker_shell,
- const char *function_name,
- uint32_t timeout,
- gearman_worker_fn *worker_fn,
- void *context)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- if (function_name == NULL)
- {
- return gearman_error(worker->universal, GEARMAN_INVALID_ARGUMENT, "function name not given");
- }
- if (worker_fn == NULL)
- {
- return gearman_error(worker->universal, GEARMAN_INVALID_ARGUMENT, "function not given");
- }
- gearman_function_t local= gearman_function_create_v1(worker_fn);
- return _worker_function_create(worker,
- function_name, strlen(function_name),
- local,
- timeout,
- context);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- gearman_return_t gearman_worker_define_function(gearman_worker_st *worker_shell,
- const char *function_name, const size_t function_name_length,
- const gearman_function_t function,
- const uint32_t timeout,
- void *context)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- if (function_name == NULL or function_name_length == 0)
- {
- return gearman_error(worker->universal, GEARMAN_INVALID_ARGUMENT, "function name not given");
- }
- return _worker_function_create(worker,
- function_name, function_name_length,
- function,
- timeout,
- context);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- void gearman_worker_reset_error(Worker& worker)
- {
- universal_reset_error(worker.universal);
- }
- void gearman_worker_reset_error(gearman_worker_st *worker)
- {
- if (worker and worker->impl())
- {
- universal_reset_error(worker->impl()->universal);
- }
- }
- #if __GNUC__ >= 7
- #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
- #endif
- gearman_return_t gearman_worker_work(gearman_worker_st *worker_shell)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- bool shutdown= false;
- universal_reset_error(worker->universal);
- switch (worker->work_state)
- {
- case GEARMAN_WORKER_WORK_UNIVERSAL_GRAB_JOB:
- {
- worker->in_work(true);
- gearman_return_t ret;
- worker->work_job(gearman_worker_grab_job(worker->shell(), NULL, &ret));
- worker->in_work(false);
- if (gearman_failed(ret))
- {
- if (ret == GEARMAN_COULD_NOT_CONNECT)
- {
- worker->universal.reset();
- }
- return ret;
- }
- assert(worker->has_work_job());
- for (worker->work_function= worker->function_list;
- worker->work_function;
- worker->work_function= worker->work_function->next)
- {
- if (strcmp(gearman_job_function_name(worker->work_job()), worker->work_function->name()) == 0)
- {
- break;
- }
- }
- if (not worker->work_function)
- {
- worker->work_job(NULL);
- return gearman_error(worker->universal, GEARMAN_INVALID_FUNCTION_NAME, "Function not found");
- }
- if (not worker->work_function->has_callback())
- {
- worker->work_job(NULL);
- return gearman_error(worker->universal, GEARMAN_INVALID_FUNCTION_NAME, "Neither a gearman_worker_fn, or gearman_function_fn callback was supplied");
- }
- worker->work_result_size= 0;
- }
- /* fall-thru */
- case GEARMAN_WORKER_WORK_UNIVERSAL_FUNCTION:
- {
- switch (worker->work_function->callback(worker->work_job(),
- static_cast<void *>(worker->work_function->context())))
- {
- case GEARMAN_FUNCTION_FATAL:
- if (gearman_job_send_fail_fin(worker->work_job()->impl()) == GEARMAN_LOST_CONNECTION) // If we fail this, we have no connection, @note this causes us to lose the current error
- {
- worker->work_job()->impl()->_error_code= GEARMAN_LOST_CONNECTION;
- break;
- }
- worker->work_state= GEARMAN_WORKER_WORK_UNIVERSAL_FAIL;
- return worker->work_job()->impl()->_error_code;
- case GEARMAN_FUNCTION_ERROR: // retry
- worker->universal.reset();
- worker->work_job()->impl()->_error_code= GEARMAN_LOST_CONNECTION;
- break;
- case GEARMAN_FUNCTION_SHUTDOWN:
- shutdown= true;
- case GEARMAN_FUNCTION_SUCCESS:
- break;
- }
- if (worker->work_job()->impl()->_error_code == GEARMAN_LOST_CONNECTION)
- {
- break;
- }
- }
- /* fall-thru */
- case GEARMAN_WORKER_WORK_UNIVERSAL_COMPLETE:
- {
- worker->work_job()->impl()->_error_code= gearman_job_send_complete_fin(worker->work_job()->impl(),
- worker->work_result, worker->work_result_size);
- if (worker->work_job()->impl()->_error_code == GEARMAN_IO_WAIT)
- {
- worker->work_state= GEARMAN_WORKER_WORK_UNIVERSAL_COMPLETE;
- return gearman_error(worker->universal, worker->work_job()->impl()->_error_code,
- "A failure occurred after worker had successful complete, unless gearman_job_send_complete() was called directly by worker, client has not been informed of success.");
- }
- if (worker->work_result)
- {
- gearman_free(worker->universal, worker->work_result);
- worker->work_result= NULL;
- }
- // If we lost the connection, we retry the work, otherwise we error
- if (worker->work_job()->impl()->_error_code == GEARMAN_LOST_CONNECTION)
- {
- break;
- }
- else if (worker->work_job()->impl()->_error_code == GEARMAN_SHUTDOWN)
- { }
- else if (gearman_failed(worker->work_job()->impl()->_error_code))
- {
- worker->work_state= GEARMAN_WORKER_WORK_UNIVERSAL_FAIL;
- return worker->work_job()->impl()->_error_code;
- }
- }
- break;
- case GEARMAN_WORKER_WORK_UNIVERSAL_FAIL:
- {
- if (gearman_failed(worker->work_job()->impl()->_error_code= gearman_job_send_fail_fin(worker->work_job()->impl())))
- {
- if (worker->work_job()->impl()->_error_code == GEARMAN_LOST_CONNECTION)
- {
- break;
- }
- return worker->work_job()->impl()->_error_code;
- }
- }
- break;
- }
- worker->work_job(NULL);
- worker->work_state= GEARMAN_WORKER_WORK_UNIVERSAL_GRAB_JOB;
- if (shutdown)
- {
- return GEARMAN_SHUTDOWN;
- }
- return GEARMAN_SUCCESS;
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- gearman_return_t gearman_worker_echo(gearman_worker_st *worker,
- const void *workload,
- size_t workload_size)
- {
- if (worker and worker->impl())
- {
- return gearman_echo(worker->impl()->universal, workload, workload_size);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- /*
- * Static Definitions
- */
- static gearman_worker_st *_worker_allocate(gearman_worker_st *worker_shell, bool is_clone)
- {
- Worker *worker= new (std::nothrow) Worker(worker_shell);
- if (worker)
- {
- if (is_clone == false)
- {
- if (getenv("GEARMAN_SERVERS"))
- {
- if (gearman_worker_add_servers(worker->shell(), getenv("GEARMAN_SERVERS")))
- {
- gearman_worker_free(worker->shell());
- return NULL;
- }
- }
- }
- if (worker->universal.wakeup(true) == false)
- {
- delete worker;
- return NULL;
- }
- return worker->shell();
- }
- #if defined(DEBUG) && DEBUG
- perror("new Worker");
- #endif
- return NULL;
- }
- static gearman_return_t _worker_packet_init(Worker* worker)
- {
- gearman_return_t ret= gearman_packet_create_args(worker->universal, worker->grab_job,
- GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_GRAB_JOB_ALL,
- NULL, NULL, 0);
- if (gearman_failed(ret))
- {
- return ret;
- }
- ret= gearman_packet_create_args(worker->universal, worker->pre_sleep,
- GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_PRE_SLEEP,
- NULL, NULL, 0);
- if (gearman_failed(ret))
- {
- gearman_packet_free(&(worker->grab_job));
- return ret;
- }
- worker->options.packet_init= true;
- return GEARMAN_SUCCESS;
- }
- static gearman_return_t _worker_add_server(const char *host, in_port_t port, void *context)
- {
- return gearman_worker_add_server(static_cast<gearman_worker_st *>(context), host, port);
- }
- static gearman_return_t _worker_function_create(Worker *worker,
- const char *function_name_, const size_t function_length_,
- const gearman_function_t &function_arg,
- uint32_t timeout,
- void *context)
- {
- const void *args[2];
- size_t args_size[2];
- if (function_length_ == 0 or function_name_ == NULL or function_length_ > GEARMAN_FUNCTION_MAX_SIZE)
- {
- if (function_length_ > GEARMAN_FUNCTION_MAX_SIZE)
- {
- gearman_error(worker->universal, GEARMAN_INVALID_ARGUMENT, "function name longer then GEARMAN_MAX_FUNCTION_SIZE");
- }
- else
- {
- gearman_error(worker->universal, GEARMAN_INVALID_ARGUMENT, "invalid function");
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- _worker_function_st *function= make(worker->universal._namespace,
- function_name_, function_length_,
- function_arg, context, timeout);
- if (function == NULL)
- {
- gearman_perror(worker->universal, errno, "_worker_function_st::new()");
- return GEARMAN_MEMORY_ALLOCATION_FAILURE;
- }
- gearman_return_t ret;
- if (timeout > 0)
- {
- char timeout_buffer[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
- snprintf(timeout_buffer, sizeof(timeout_buffer), "%u", timeout);
- timeout_buffer[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH]= 0;
- args[0]= function->name();
- args_size[0]= function->length() + 1;
- args[1]= timeout_buffer;
- args_size[1]= strlen(timeout_buffer);
- ret= gearman_packet_create_args(worker->universal, function->packet(),
- GEARMAN_MAGIC_REQUEST,
- GEARMAN_COMMAND_CAN_DO_TIMEOUT,
- args, args_size, 2);
- }
- else
- {
- args[0]= function->name();
- args_size[0]= function->length();
- ret= gearman_packet_create_args(worker->universal, function->packet(),
- GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_CAN_DO,
- args, args_size, 1);
- }
- if (gearman_failed(ret))
- {
- delete function;
- return ret;
- }
- if (worker->function_list)
- {
- worker->function_list->prev= function;
- }
- function->next= worker->function_list;
- function->prev= NULL;
- worker->function_list= function;
- worker->function_count++;
- worker->options.change= true;
- return GEARMAN_SUCCESS;
- }
- static void _worker_function_free(Worker* worker,
- struct _worker_function_st *function)
- {
- if (worker->function_list == function)
- {
- worker->function_list= function->next;
- }
- if (function->prev)
- {
- function->prev->next= function->next;
- }
- if (function->next)
- {
- function->next->prev= function->prev;
- }
- worker->function_count--;
- delete function;
- }
- gearman_return_t gearman_worker_set_memory_allocators(gearman_worker_st *worker,
- gearman_malloc_fn *malloc_fn,
- gearman_free_fn *free_fn,
- gearman_realloc_fn *realloc_fn,
- gearman_calloc_fn *calloc_fn,
- void *context)
- {
- if (worker and worker->impl())
- {
- return gearman_set_memory_allocator(worker->impl()->universal.allocator, malloc_fn, free_fn, realloc_fn, calloc_fn, context);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- bool gearman_worker_set_server_option(gearman_worker_st *worker_shell, const char *option_arg, size_t option_arg_size)
- {
- if (worker_shell and worker_shell->impl())
- {
- Worker* worker= worker_shell->impl();
- gearman_string_t option= { option_arg, option_arg_size };
- if (gearman_success(gearman_server_option(worker->universal, option)))
- {
- if (gearman_request_option(worker->universal, option))
- {
- return true;
- }
- }
- }
- return false;
- }
- void gearman_worker_set_namespace(gearman_worker_st *self, const char *namespace_key, size_t namespace_key_size)
- {
- if (self and self->impl())
- {
- gearman_universal_set_namespace(self->impl()->universal, namespace_key, namespace_key_size);
- }
- }
- gearman_id_t gearman_worker_id(gearman_worker_st *self)
- {
- if (self == NULL)
- {
- gearman_id_t handle= { INVALID_SOCKET, INVALID_SOCKET };
- return handle;
- }
- return gearman_universal_id(self->impl()->universal);
- }
- gearman_return_t gearman_worker_set_identifier(gearman_worker_st *worker,
- const char *id, size_t id_size)
- {
- if (worker and worker->impl())
- {
- return gearman_set_identifier(worker->impl()->universal, id, id_size);
- }
- return GEARMAN_INVALID_ARGUMENT;
- }
- const char *gearman_worker_namespace(gearman_worker_st* worker)
- {
- if (worker and worker->impl())
- {
- return gearman_univeral_namespace(worker->impl()->universal);
- }
- return NULL;
- }
- gearman_job_st* Worker::take_job()
- {
- gearman_job_st* tmp= _job->shell();
- _job= NULL;
- return tmp;
- }
- gearman_job_st* Worker::job()
- {
- if (_job)
- {
- return _job->shell();
- }
- return NULL;
- }
- void Worker::job(gearman_job_st* job_)
- {
- if (job_)
- {
- assert(job_->impl());
- assert(_job == NULL);
- _job= job_->impl();
- }
- else if (_job)
- {
- gearman_job_free(_job->shell());
- _job= NULL;
- }
- }
- gearman_job_st* Worker::work_job()
- {
- assert(_work_job);
- return _work_job->shell();
- }
- void Worker::work_job(gearman_job_st* work_job_)
- {
- if (work_job_)
- {
- assert(_work_job == NULL);
- _work_job= work_job_->impl();
- }
- else if (_work_job)
- {
- gearman_job_free(_work_job->shell());
- _work_job= NULL;
- }
- }
|