angularjs - NodeJS Periodic tasks -
firts of all, i'll explain problem:
i'm developing ecommerce website. 1 of features possibility customers create purchasing rules. these rules customer can set start date, periodicity , product purchase. result product purchased every [periodicity] days [start date].
the system developed nodejs back-end, mongodb database , angularjs front-end.
i've found projects scheduling tasks in nodejs. 2 of them are:
both of them great tools have same problem 2 of them. problem need create scheduled tasks stop them. these tools clear how schedule function executed on time, how can stop them @ moment?
the objects provided node-schedule , node-cron have cancel() or stop() method stop scheduling, invoke method need have object.
my question if there way "store" scheduled tasks in database in order able stop them @ moment , anywhere other function created.
and if not possible, if there tool better i've mentioned need.
thank reading , appreciated.
ok, i've found better solution problem adding bunch of scheduled task, 1 purchasing rule. purchasing_rules table looks this:
+--------------------+ | purchase_rules | +--------------------+ | customer_id | | product_id | | quantity | | start_date | | periodicity (days) | +--------------------+ my solution add field next run, table like:
+--------------------+ | purchase_rules | +--------------------+ | customer_id | | product_id | | quantity | | start_date | | periodicity (days) | | next_run | +--------------------+ by default [next_run] [start_date] + [periodictiy].
and now, magic:
i use node-schedule schedule job every day @ hour. job following:
- look in purchase_rules table rule next_run = today
- for every rule found:
- purchase desired [quantity] of product [product_id] customer [customer_id]
- make [next_run] = [next_run] + [periodicity]
- finish
this way database not accesed many times agenda package , "stopping" purchasing_rule have remove database.
i hope find useful day.
Comments
Post a Comment