1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
|
/* 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;
}
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;
}
}
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)
{
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;
}
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);
}
}
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;
}
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;
}
}
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;
}
}
|