nickva commented on a change in pull request #2051: CouchDB background jobs
URL: https://github.com/apache/couchdb/pull/2051#discussion_r293383487
 
 

 ##########
 File path: src/couch_jobs/src/couch_jobs.erl
 ##########
 @@ -0,0 +1,338 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_jobs).
+
+-export([
+    % Job creation
+    add/3,
+    add/4,
+    remove/1,
+    resubmit/1,
+    get_job_data/1,
+    get_job_state/1,
+
+    % Job processing
+    accept/1,
+    accept/2,
+    finish/2,
+    finish/3,
+    resubmit/2,
+    resubmit/3,
+    is_resubmitted/1,
+    update/2,
+    update/3,
+
+    % Subscriptions
+    subscribe/1,
+    subscribe/2,
+    unsubscribe/1,
+    wait/2,
+    wait/3,
+
+    % Type timeouts
+    set_type_timeout/2,
+    clear_type_timeout/1,
+    get_type_timeout/1
+]).
+
+
+-include("couch_jobs.hrl").
+
+
+-define(MIN_ACCEPT_WAIT_MSEC, 100).
+
+
+%% Job Creation API
+
+-spec add(job_type(), job_id(), job_data()) -> {ok, job()} | {error, any()}.
+add(Type, JobId, JobData) ->
+    add(Type, JobId, JobData, 0).
+
+
+-spec add(job_type(), job_id(), job_data(), scheduled_time()) ->
+    ok | {error, any()}.
+add(Type, JobId, JobData, ScheduledTime) when is_map(JobData),
+        is_integer(ScheduledTime) ->
+    couch_jobs_fdb:tx(couch_jobs_fdb:get_jtx(), fun(JTx) ->
+        couch_jobs_fdb:add(JTx, Type, JobId, JobData, ScheduledTime)
+    end).
+
+
+-spec remove(job()) -> ok | {error, any()}.
+remove(#{job := true} = Job) ->
+    couch_jobs_fdb:tx(couch_jobs_fdb:get_jtx(), fun(JTx) ->
+        couch_jobs_fdb:remove(JTx, Job)
+    end).
+
+
+-spec resubmit(job()) -> {ok, job()} | {error, any()}.
 
 Review comment:
   `add/3,4` will return the `Job` object. Then that job object can be used for 
resubmit, to get the job's data, state and finally to remove the job.
   
   However I think we would also need a `get_job(Type, JobId) -> `Job` object 
because a request would come in and we'd want to know if a job already exists 
and if so to resubmit it if it is running.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to