Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

raises exception if background and wait_until_complete flags are not set... #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v2.0.3, 2014-10-28 -- Minor bug fix release
* GearmanClient - Raises exception if background=False and wait_until_complete=False when calling submit_jobs/submit_multiple_jobs

v2.0.X, 2012-XX-XX -- Major bug fix release
* GearmanWorker - Dispatch to the right function instead of looping
* GearmanClient - Let the server handle the special '-' unique [GH-18]
Expand Down
2 changes: 1 addition & 1 deletion gearman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Gearman API - Client, worker, and admin client interfaces
"""

__version__ = '2.0.2'
__version__ = '2.0.3'

from gearman.admin_client import GearmanAdminClient
from gearman.client import GearmanClient
Expand Down
6 changes: 5 additions & 1 deletion gearman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from gearman.connection_manager import GearmanConnectionManager
from gearman.client_handler import GearmanClientCommandHandler
from gearman.constants import PRIORITY_NONE, PRIORITY_LOW, PRIORITY_HIGH, JOB_UNKNOWN, JOB_PENDING
from gearman.errors import ConnectionError, ExceededConnectionAttempts, ServerUnavailable
from gearman.errors import ConnectionError, ExceededConnectionAttempts, InvalidFlagsToSubmitJob, ServerUnavailable

gearman_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,6 +45,10 @@ def submit_multiple_jobs(self, jobs_to_submit, background=False, wait_until_comp
"""
assert type(jobs_to_submit) in (list, tuple, set), "Expected multiple jobs, received 1?"

# Check if the flags are properly set
if background == False and wait_until_complete == False:
raise InvalidFlagsToSubmitJob('Set background=True if wait_until_complete=False to send background jobs')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a reason why this combination is invalid.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - this is definitely a valid combination. The "background" flag simply specifies if this is a foreground or background job. The "wait_until_complete" flag specifies whether we're blocking until we get our response. It is possible (and probably encouraged) to not block on a foreground job.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If jobs are sent as foreground, the gearmand server will not put them in
the queue (e.g. MySQL).

The server starts run these jobs after 'wait_until_jobs_accepted'.

However, because the submit_job method returns to the caller shortly after,

what will happen is that most of these foreground jobs will be dropped by
the server and not executed.

(I have an example code confirmed this.)


# Convert all job dicts to job request objects
requests_to_submit = [self._create_request_from_dictionary(job_info, background=background, max_retries=max_retries) for job_info in jobs_to_submit]

Expand Down
3 changes: 3 additions & 0 deletions gearman/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ class InvalidWorkerState(GearmanError):

class InvalidAdminClientState(GearmanError):
pass

class InvalidFlagsToSubmitJob(GearmanError):
pass
24 changes: 19 additions & 5 deletions tests/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from gearman.client_handler import GearmanClientCommandHandler

from gearman.constants import PRIORITY_NONE, PRIORITY_HIGH, PRIORITY_LOW, JOB_UNKNOWN, JOB_PENDING, JOB_CREATED, JOB_FAILED, JOB_COMPLETE
from gearman.errors import ExceededConnectionAttempts, ServerUnavailable, InvalidClientState
from gearman.errors import ExceededConnectionAttempts, ServerUnavailable, InvalidClientState, InvalidFlagsToSubmitJob
from gearman.protocol import submit_cmd_for_background_priority, GEARMAN_COMMAND_STATUS_RES, GEARMAN_COMMAND_GET_STATUS, GEARMAN_COMMAND_JOB_CREATED, \
GEARMAN_COMMAND_WORK_STATUS, GEARMAN_COMMAND_WORK_FAIL, GEARMAN_COMMAND_WORK_COMPLETE, GEARMAN_COMMAND_WORK_DATA, GEARMAN_COMMAND_WORK_WARNING

Expand Down Expand Up @@ -144,7 +144,21 @@ def fail_then_create_jobs(rx_conns, wr_conns, ex_conns):
self.assertEquals(current_request.state, JOB_UNKNOWN)
self.assertEquals(current_request.connection_attempts, current_request.max_connection_attempts)

def test_multiple_fg_job_submission(self):
def test_invalid_flags_to_submit_job(self):
submitted_job_count = 5
expected_job_list = [self.generate_job() for _ in xrange(submitted_job_count)]
def mark_jobs_created(rx_conns, wr_conns, ex_conns):
for current_job in expected_job_list:
self.command_handler.recv_command(GEARMAN_COMMAND_JOB_CREATED, job_handle=current_job.handle)

return rx_conns, wr_conns, ex_conns

self.connection_manager.handle_connection_activity = mark_jobs_created
job_dictionaries = [current_job.to_dict() for current_job in expected_job_list]

self.assertRaises(InvalidFlagsToSubmitJob, self.connection_manager.submit_multiple_jobs, job_dictionaries, background=False, wait_until_complete=False)

def test_multiple_bg_job_submission(self):
submitted_job_count = 5
expected_job_list = [self.generate_job() for _ in xrange(submitted_job_count)]
def mark_jobs_created(rx_conns, wr_conns, ex_conns):
Expand All @@ -158,16 +172,16 @@ def mark_jobs_created(rx_conns, wr_conns, ex_conns):
job_dictionaries = [current_job.to_dict() for current_job in expected_job_list]

# Test multiple job submission
job_requests = self.connection_manager.submit_multiple_jobs(job_dictionaries, wait_until_complete=False)
job_requests = self.connection_manager.submit_multiple_jobs(job_dictionaries, background=True, wait_until_complete=False)
for current_request, expected_job in zip(job_requests, expected_job_list):
current_job = current_request.job
self.assert_jobs_equal(current_job, expected_job)

self.assertEqual(current_request.priority, PRIORITY_NONE)
self.assertEqual(current_request.background, False)
self.assertEqual(current_request.background, True)
self.assertEqual(current_request.state, JOB_CREATED)

self.assertFalse(current_request.complete)
self.assertTrue(current_request.complete)

def test_single_bg_job_submission(self):
expected_job = self.generate_job()
Expand Down