elasticsearch-cron
v1.0.0
Published
A utility for scheduling elasticsearch queries
Downloads
27
Maintainers
Readme
elasticsearch-cron
Description
An event driven utitily for scheduling elasticsearch jobs extending the capabilities of the official elasticseach javascript client. Ever wanted to query elastic search periodically and perform a task with the result or want to watch a particular keyword but considers elaticwatch too complex and expensive
Features
- Generalized, pluggable architecture.
- Configurable, automatic discovery of cluster nodes
- Persistent, Keep-Alive connections
- Load balancing (with pluggable selection strategy) across all available nodes.
- Takes all native client config
Use in Node.js
npm install elasticsearch-cron
Supported Elasticsearch Versions
Elasticsearch-cron provides support for, and is regularly tested against, Elasticsearch releases 0.90.12 and greater. We also test against the latest changes in several branches in the Elasticsearch repository. To tell the client which version of Elastisearch you are using, and therefore the API it should provide, set the apiVersion
config param. More info
Examples
Create a client instance
const EsCron = require('elasticsearch-cron');
const client = new EsCron({
host: 'localhost:9200',
log: 'trace'
});
Scheduling a cron search
syntax
let search = client.search(query,
cron-pattern,
size(defaults to 1000),
index(optional),
type(optional))
example. query every 30seconds
let search = client.search({
"match":
{
"log": "BitunnelRouteException"
}
},
'*/30 * * * * *');
This task intelligently runs in the background varying the time range for queries in each run. See internal query structure
{
query: {
"bool": {
"must": [
{
"match": {
"log": "BitunnelRouteException"
}
},
{
"range": {
"@timestamp": {
"gte": lastRun,
"lte": currentTime
}
}
}
]
}
}
}
It will query elasticsearch for all changes since the last run date. Eg.
If you set a cron task to run at 12am
everyday. The last run for a job on 12am monday
will be 12am sunday
. That way, all the changes in the 24-hour is captured.
Parse result on each run
search.on('run', (data) => {
console.log(`Came back with the result ${data}`);
});
Catch errors
search.on('error', (ex) => {
console.log(`An error occured, ${ex}`);
})
Supported cron format
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)
Supports mixed use of ranges and range increments (L, W and # characters are not supported currently). See tests for examples. See crontab for more information
Contributing
We accept contributions via Pull Requests on Github.
Pull Requests
Document any change in behaviour - Make sure the
README.md
and any other relevant documentation are kept up-to-date.Consider our release cycle - We try to follow SemVer v2.0.0. Randomly breaking public APIs is not an option.
Create feature branches - Don't ask us to pull from your master branch.
One pull request per feature - If you want to do more than one thing, send multiple pull requests.
Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
Issues
Check issues for current issues.
Credits
License
The MIT License (MIT). Please see LICENSE for more information.