├── docs ├── www │ ├── img │ │ ├── cronquery.jpg │ │ ├── cronquery.png │ │ └── cronquery.svg │ ├── js │ │ ├── docs.js │ │ ├── _sidebar-toggle.js │ │ └── _highlight.js │ ├── fonts │ │ ├── MavenPro-Black.ttf │ │ ├── MavenPro-Bold.ttf │ │ ├── MavenPro-Medium.ttf │ │ ├── MavenPro-Regular.ttf │ │ ├── MavenPro-SemiBold.ttf │ │ └── MavenPro-ExtraBold.ttf │ ├── scss │ │ ├── _logo.scss │ │ ├── _body.scss │ │ ├── _callout.scss │ │ ├── _table.scss │ │ ├── docs.scss │ │ ├── _header.scss │ │ ├── _font.scss │ │ ├── _variables.scss │ │ ├── _code.scss │ │ ├── _page.scss │ │ ├── _sidebar-toggle.scss │ │ └── _sidebar.scss │ ├── snippets │ │ ├── CreatingJob.cs │ │ ├── CRON.txt │ │ ├── RegisteringJob.cs │ │ └── SettingUpJob.json │ ├── 404.hbs │ ├── versions.hbs │ ├── _page.hbs │ ├── index.hbs │ └── cron.hbs ├── publish.sh ├── package.json ├── handlebars.js └── gulpfile.js ├── .codecov.yml ├── .config └── dotnet-tools.json ├── example ├── Example.csproj ├── appsettings.json ├── Jobs │ ├── MySecondJob.cs │ └── MyFirstJob.cs └── Program.cs ├── tools ├── install-dotnet.sh └── test.sh ├── src ├── CronQuery.Tests │ ├── Functional │ │ ├── appsettings.json │ │ ├── AppTest.cs │ │ └── TestProgram.cs │ ├── CronQuery.Tests.csproj │ ├── Fakes │ │ ├── LoggerFactoryFake.cs │ │ ├── LoggerFake.cs │ │ ├── TestServerExtensions.cs │ │ ├── Jobs │ │ │ ├── JobNotEnqueued.cs │ │ │ ├── JobWithError.cs │ │ │ ├── JobBadlyConfigured.cs │ │ │ ├── JobStopped.cs │ │ │ └── JobSuccessful.cs │ │ ├── ServiceProviderFake.cs │ │ └── OptionsMonitorFake.cs │ └── Unit │ │ ├── Cron │ │ ├── UnreachableConditionTest.cs │ │ ├── ExpressionWithHashTest.cs │ │ ├── ExpressionWithAsteriskTest.cs │ │ ├── ExpressionWithCommaTest.cs │ │ ├── InvalidExpressionTest.cs │ │ ├── ExpressionWithLAndWTest.cs │ │ ├── ExpressionWithSlashTest.cs │ │ ├── ExpressionWithDigitTest.cs │ │ ├── ExpressionWithWTest.cs │ │ ├── ExpressionWithDashAndSlashTest.cs │ │ ├── ExpressionWithDashAndSlashAndCommaTest.cs │ │ ├── ComplexExpressionTest.cs │ │ ├── ExpressionWithDashTest.cs │ │ └── ExpressionWithLTest.cs │ │ └── Runner │ │ ├── TimeZoneTest.cs │ │ ├── JobIntervalTest.cs │ │ └── JobRunnerTest.cs └── CronQuery.API │ ├── Mvc │ ├── Jobs │ │ ├── IJob.cs │ │ ├── JobCollection.cs │ │ ├── JobInterval.cs │ │ └── JobRunner.cs │ ├── Options │ │ ├── JobOptions.cs │ │ ├── JobRunnerOptions.cs │ │ └── TimeZoneOptions.cs │ └── DependencyInjection │ │ └── CronQueryExtensions.cs │ ├── CronQuery.API.csproj │ ├── Extensions │ ├── Int32Extensions.cs │ └── DateTimeExtensions.cs │ └── Cron │ ├── TimeUnit.cs │ ├── CronSyntax.cs │ ├── CronValue.cs │ ├── CronExpression.cs │ └── CronField.cs ├── .gitignore ├── LICENSE ├── README.md └── .circleci └── config.yml /docs/www/img/cronquery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/img/cronquery.jpg -------------------------------------------------------------------------------- /docs/www/img/cronquery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/img/cronquery.png -------------------------------------------------------------------------------- /docs/www/js/docs.js: -------------------------------------------------------------------------------- 1 | import 'bootstrap' 2 | 3 | import './_sidebar-toggle' 4 | import './_highlight' 5 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | branch: master 3 | coverage: 4 | range: 80..90 5 | round: down 6 | precision: 2 7 | -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-Black.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-Black.ttf -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-Bold.ttf -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-Medium.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-Medium.ttf -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-Regular.ttf -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-SemiBold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-SemiBold.ttf -------------------------------------------------------------------------------- /docs/www/fonts/MavenPro-ExtraBold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxjmoura/cronquery/HEAD/docs/www/fonts/MavenPro-ExtraBold.ttf -------------------------------------------------------------------------------- /docs/www/scss/_logo.scss: -------------------------------------------------------------------------------- 1 | div.logo { 2 | margin: $base-spacing 0; 3 | text-align: center; 4 | 5 | img { 6 | width: 96px; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /docs/www/snippets/CreatingJob.cs: -------------------------------------------------------------------------------- 1 | public class MyJob : IJob 2 | { 3 | public async Task RunAsync() 4 | { 5 | // Do your magic 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /docs/www/scss/_body.scss: -------------------------------------------------------------------------------- 1 | html { 2 | scroll-padding-top: calc($header-height + $base-spacing); 3 | } 4 | 5 | body.sidebar-show { 6 | overflow: hidden; 7 | } 8 | -------------------------------------------------------------------------------- /docs/www/scss/_callout.scss: -------------------------------------------------------------------------------- 1 | .callout { 2 | padding: 1.25rem; 3 | border: 1px solid $gray-200; 4 | border-left-width: .25rem; 5 | border-radius: .25rem; 6 | color: rgba($body-color, .85); 7 | } 8 | -------------------------------------------------------------------------------- /docs/www/scss/_table.scss: -------------------------------------------------------------------------------- 1 | table { 2 | @extend .table; 3 | 4 | thead tr th { 5 | border-top: none; 6 | } 7 | 8 | tbody tr:last-child td { 9 | border-bottom: none; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "dotnet-reportgenerator-globaltool": { 6 | "version": "5.2.0", 7 | "commands": [ 8 | "reportgenerator" 9 | ] 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /docs/www/snippets/CRON.txt: -------------------------------------------------------------------------------- 1 | ┌──── second (0-59) 2 | │ ┌───── minute (0-59) 3 | │ │ ┌───── hour (0-23) 4 | │ │ │ ┌───── day of month (1-31) 5 | │ │ │ │ ┌───── month (1-12) 6 | │ │ │ │ │ ┌───── day of week (0-6) 7 | │ │ │ │ │ │ 8 | * * * * * * 9 | -------------------------------------------------------------------------------- /example/Example.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/www/scss/docs.scss: -------------------------------------------------------------------------------- 1 | @import 'variables'; 2 | 3 | @import 'node_modules/bootstrap/scss/bootstrap'; 4 | 5 | @import 'body'; 6 | @import 'callout'; 7 | @import 'code'; 8 | @import 'header'; 9 | @import 'logo'; 10 | @import 'sidebar'; 11 | @import 'sidebar-toggle'; 12 | @import 'page'; 13 | @import 'table'; 14 | -------------------------------------------------------------------------------- /docs/www/snippets/RegisteringJob.cs: -------------------------------------------------------------------------------- 1 | using CronQuery.Mvc.DependencyInjection; 2 | 3 | var builder = WebApplication.CreateBuilder(args); 4 | 5 | builder.Services.AddCronQuery(builder.Configuration.GetSection("CronQuery")); 6 | 7 | builder.Services.AddTransient(); 8 | builder.Services.AddTransient(); 9 | -------------------------------------------------------------------------------- /docs/www/js/_sidebar-toggle.js: -------------------------------------------------------------------------------- 1 | import $ from 'jquery' 2 | 3 | const $body = $('body') 4 | const $sidebar = $('.sidebar') 5 | 6 | $('.sidebar-toggle').on('click', function (e) { 7 | $sidebar.scrollTop(0) 8 | 9 | const toggle = !$body.hasClass('sidebar-show') 10 | $body.toggleClass('sidebar-show', toggle) 11 | 12 | e.preventDefault() 13 | }) 14 | -------------------------------------------------------------------------------- /tools/install-dotnet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | version=$1 6 | tar_gz="https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$version/dotnet-sdk-$version-linux-x64.tar.gz" 7 | 8 | curl -SL -o dotnet.tar.gz $tar_gz 9 | sudo mkdir -p /usr/share/dotnet 10 | sudo tar -zxf dotnet.tar.gz -C /usr/share/dotnet 11 | sudo ln -sfn /usr/share/dotnet/dotnet /usr/bin/dotnet 12 | -------------------------------------------------------------------------------- /docs/www/scss/_header.scss: -------------------------------------------------------------------------------- 1 | header { 2 | background-color: red; 3 | position: fixed; 4 | top: 0; 5 | left: 0; 6 | right: 0; 7 | display: flex;; 8 | align-items: center; 9 | font-size: 1.3rem; 10 | padding: 0 1rem; 11 | height: $header-height; 12 | border-bottom: $border-width $border-color solid; 13 | background-color: $white; 14 | z-index: $zindex-fixed; 15 | } 16 | -------------------------------------------------------------------------------- /example/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "CronQuery": { 3 | "Running": true, 4 | "Timezone": "Bahia Standard Time", 5 | "Jobs": [ 6 | { 7 | "Name": "MyFirstJob", 8 | "Running": true, 9 | "Cron": "0/10 * * * * *" 10 | }, 11 | { 12 | "Name": "MySecondJob", 13 | "Running": true, 14 | "Cron": "30 * * * * *" 15 | } 16 | ] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /docs/www/404.hbs: -------------------------------------------------------------------------------- 1 | {{#> www/page }} 2 | 3 | {{#*inline "head-block"}} 4 | 5 | {{/inline}} 6 | 7 | {{#*inline "body-block"}} 8 | 9 |

10 | Page not found! 11 |

12 | 13 |

14 | Please, use the side navigation to find the page you are looking for. 15 |

16 | 17 | {{/inline}} 18 | 19 | {{/www/page}} 20 | -------------------------------------------------------------------------------- /docs/www/js/_highlight.js: -------------------------------------------------------------------------------- 1 | import $ from 'jquery' 2 | import hljs from 'highlight.js/lib/core' 3 | import csharp from 'highlight.js/lib/languages/csharp' 4 | import js from 'highlight.js/lib/languages/javascript' 5 | import json from 'highlight.js/lib/languages/json' 6 | 7 | hljs.registerLanguage('csharp', csharp) 8 | hljs.registerLanguage('js', js) 9 | hljs.registerLanguage('json', json) 10 | 11 | $('pre code').each(function (i, code) { 12 | hljs.highlightBlock(code) 13 | }) 14 | -------------------------------------------------------------------------------- /docs/www/snippets/SettingUpJob.json: -------------------------------------------------------------------------------- 1 | { 2 | "CronQuery": { 3 | "Running": true, 4 | "TimeZone": "E. South America Standard Time", 5 | "Jobs": [ 6 | { 7 | "Name": "MyFirstJob", 8 | "Running": true, 9 | "Cron": "* * * * * 1-6" 10 | }, 11 | { 12 | "Name": "MySecondJob", 13 | "Running": true, 14 | "Cron": "0 0 2 * * *" 15 | }, 16 | { 17 | "Name": "MyThirdJob", 18 | "Running": true, 19 | "Cron": "* * 14-18 * * 6/15" 20 | } 21 | ] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /docs/publish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd docs 4 | 5 | set -e 6 | 7 | rm -rf ./dist 8 | [[ `git worktree list | grep gh-pages` ]] && git worktree remove ./gh-pages --force 9 | [[ `git branch --list gh-pages` ]] && git branch -D gh-pages 10 | rm -rf ./gh-pages 11 | 12 | if [[ ! `git ls-remote origin --heads gh-pages` ]]; then 13 | git checkout --orphan gh-pages 14 | git rm -rf . 15 | git commit --allow-empty -m "Initial gh-pages commit" 16 | git push origin gh-pages 17 | fi 18 | 19 | npm install 20 | npm run publish 21 | 22 | git worktree add ./gh-pages gh-pages 23 | 24 | rm -rf ./gh-pages/.[!.git]* 25 | cp -r ./dist/* ./gh-pages/ 26 | 27 | cd ./gh-pages 28 | 29 | git add . 30 | git commit -m "Publish" 31 | git push -f origin gh-pages 32 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Functional/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "CronQuery": { 3 | "Running": true, 4 | "Jobs": [ 5 | { 6 | "Name": "JobSuccessful", 7 | "Running": true, 8 | "Cron": "* * * * * *" 9 | }, 10 | { 11 | "Name": "JobNotEnqueued", 12 | "Running": true, 13 | "Cron": "* * * * * *" 14 | }, 15 | { 16 | "Name": "JobBadlyConfigured", 17 | "Running": true, 18 | "Cron": "IN V A L I D" 19 | }, 20 | { 21 | "Name": "JobStopped", 22 | "Running": false, 23 | "Cron": "* * * * * *" 24 | }, 25 | { 26 | "Name": "JobWithError", 27 | "Running": true, 28 | "Cron": "* * * * * *" 29 | } 30 | ] 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tools/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | basedir="$(dirname $0)/.." 6 | solution_dir="$basedir/src" 7 | api_project_dir="CronQuery.API" 8 | test_project_dir="CronQuery.Tests" 9 | 10 | cd $solution_dir/$test_project_dir 11 | 12 | dotnet test \ 13 | /p:AltCover="true" \ 14 | /p:AltCoverForce="true" \ 15 | /p:AltCoverThreshold="80" \ 16 | /p:AltCoverOpenCover="true" \ 17 | /p:AltCoverReport="coverage/opencover.xml" \ 18 | /p:AltCoverInputDirectory="$api_project_dir" \ 19 | /p:AltCoverAttributeFilter="ExcludeFromCodeCoverage" \ 20 | /p:AltCoverAssemblyExcludeFilter="^(?!(CronQuery)).*$|CronQuery.Tests" 21 | 22 | dotnet reportgenerator \ 23 | "-reports:coverage/opencover.xml" \ 24 | "-reporttypes:Html;HtmlSummary" \ 25 | "-targetdir:coverage/report" 26 | -------------------------------------------------------------------------------- /docs/www/scss/_font.scss: -------------------------------------------------------------------------------- 1 | @font-face { 2 | font-family: "Maven Pro"; 3 | src: url("/fonts/MavenPro-Regular.ttf"); 4 | font-weight: 400; 5 | } 6 | 7 | @font-face { 8 | font-family: "Maven Pro"; 9 | src: url("/fonts/MavenPro-Medium.ttf"); 10 | font-weight: 500; 11 | } 12 | 13 | @font-face { 14 | font-family: "Maven Pro"; 15 | src: url("/fonts/MavenPro-SemiBold.ttf"); 16 | font-weight: 600; 17 | } 18 | 19 | @font-face { 20 | font-family: "Maven Pro"; 21 | src: url("/fonts/MavenPro-Bold.ttf"); 22 | font-weight: 700; 23 | } 24 | 25 | @font-face { 26 | font-family: "Maven Pro"; 27 | src: url("/fonts/MavenPro-ExtraBold.ttf"); 28 | font-weight: 800; 29 | } 30 | 31 | @font-face { 32 | font-family: "Maven Pro"; 33 | src: url("/fonts/MavenPro-Black.ttf"); 34 | font-weight: 900; 35 | } 36 | -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "start": "gulp start", 5 | "publish": "gulp publish" 6 | }, 7 | "dependencies": { 8 | "@popperjs/core": "^2.11.7", 9 | "bootstrap": "^5.2.3", 10 | "highlight.js": "^11.7.0", 11 | "jquery": "^3.6.4" 12 | }, 13 | "devDependencies": { 14 | "@babel/core": "^7.21.4", 15 | "@babel/preset-env": "^7.21.4", 16 | "babelify": "^10.0.0", 17 | "browserify": "^17.0.0", 18 | "glob": "^10.2.1", 19 | "gulp": "^4.0.2", 20 | "gulp-autoprefixer": "^8.0.0", 21 | "gulp-bro": "^2.0.0", 22 | "gulp-connect": "^5.7.0", 23 | "gulp-htmlmin": "^5.0.1", 24 | "gulp-rename": "^2.0.0", 25 | "gulp-sass": "^5.1.0", 26 | "gulp-uglify": "^3.0.2", 27 | "handlebars": "^4.7.7", 28 | "jsdom": "^21.1.1", 29 | "sass": "^1.62.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # project directories 2 | packages/ 3 | node_modules/ 4 | bin/ 5 | obj/ 6 | temp/ 7 | dist/ 8 | mock/ 9 | wwwroot/ 10 | publish/ 11 | gh-pages/ 12 | Properties/ 13 | Settings/ 14 | TestResults/ 15 | tests/e2e/videos/ 16 | tests/e2e/screenshots/ 17 | coverage/ 18 | 19 | # log files 20 | *.log 21 | *.sqlite 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | # compiled source 27 | *.cache 28 | *.com 29 | *.class 30 | *.dll 31 | *.dll.config 32 | *.exe 33 | *.pdb 34 | 35 | # editor directories and files 36 | .idea 37 | .vs 38 | .vscode 39 | *.ntvs* 40 | *.njsproj 41 | *.sln 42 | *.suo 43 | *.sw? 44 | *.user 45 | UpgradeLog.XML 46 | _UpgradeReport_Files/ 47 | /_ReSharper.* 48 | *.ReSharper.* 49 | 50 | # system files 51 | .DS_Store 52 | Thumbs.db 53 | 54 | # package files 55 | *.nupkg 56 | *.7z 57 | *.dmg 58 | *.gz 59 | *.iso 60 | *.jar 61 | *.rar 62 | *.tar 63 | *.zip 64 | -------------------------------------------------------------------------------- /docs/www/scss/_variables.scss: -------------------------------------------------------------------------------- 1 | $white: #fff; 2 | $gray-100: #f8f9fa; 3 | $gray-200: #e9ecef; 4 | $gray-300: #dee2e6; 5 | $gray-400: #ced4da; 6 | $gray-500: #adb5bd; 7 | $gray-600: #6c757d; 8 | $gray-700: #495057; 9 | $gray-800: #343a40; 10 | $gray-900: #212529; 11 | $black: #000; 12 | 13 | $white: #fff; 14 | $dark: #282c34; 15 | $primary: #5960a3; 16 | $success: #2bbbad; 17 | $warning: #ffbb33; 18 | $danger: #ff3547; 19 | $pink: #ec407a; 20 | 21 | $body-color: #2c3e50; 22 | 23 | $code-font-size: .85em; 24 | $code-color: #476582; 25 | $code-bg: #1b1f23; 26 | 27 | $hljs-title: $white; 28 | $hljs-attribute: $white; 29 | $hljs-comment: $gray-500; 30 | $hljs-string: #7ec699; 31 | $hljs-keyword: #cc99cd; 32 | $hljs-number: #f08d49; 33 | 34 | $font-family-base: 'Maven Pro', sans-serif; 35 | $font-size-base: 1rem; 36 | 37 | $base-spacing: 2rem; 38 | $header-height: 3.6rem; 39 | $page-max-width: 50rem; 40 | $sidebar-width: 20rem; 41 | -------------------------------------------------------------------------------- /docs/www/scss/_code.scss: -------------------------------------------------------------------------------- 1 | code { 2 | background-color: rgba($code-bg, .05); 3 | line-height: 1.7; 4 | padding: .25rem .5rem; 5 | margin: 0; 6 | border-radius: 3px; 7 | } 8 | 9 | pre { 10 | background-color: $dark; 11 | border-radius: 0.375rem; 12 | color: $white; 13 | padding: 1.25rem 1.5rem; 14 | margin: .85rem 0; 15 | 16 | code { 17 | padding: 0; 18 | margin: 0; 19 | 20 | .hljs-title { 21 | color: $hljs-title; 22 | } 23 | 24 | .hljs-attr { 25 | color: $hljs-attribute; 26 | } 27 | 28 | .hljs-string { 29 | color: $hljs-string; 30 | } 31 | 32 | .hljs-keyword, 33 | .hljs-literal { 34 | color: $hljs-keyword; 35 | } 36 | 37 | .hljs-number { 38 | color: $hljs-number; 39 | } 40 | 41 | .hljs-comment { 42 | color: $hljs-comment; 43 | } 44 | 45 | a .hljs-comment { 46 | color: $blue; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /docs/www/scss/_page.scss: -------------------------------------------------------------------------------- 1 | .page { 2 | padding-top: $header-height; 3 | padding-left: $sidebar-width; 4 | 5 | .content { 6 | line-height: 1.7; 7 | padding: 1rem $base-spacing; 8 | max-width: $page-max-width; 9 | margin: 0 auto; 10 | 11 | h1, 12 | h2, 13 | h3, 14 | h4, 15 | h5, 16 | h6 { 17 | font-weight: 700; 18 | margin-top: 3rem; 19 | 20 | > a { 21 | font-size: 1.75rem; 22 | text-decoration: none; 23 | } 24 | } 25 | 26 | h1, 27 | h2, 28 | h3, 29 | h4, 30 | h5, 31 | h6, 32 | p, 33 | ul, 34 | ol, 35 | pre, 36 | .callout, 37 | .table-responsive { 38 | margin-bottom: $base-spacing; 39 | } 40 | 41 | table { 42 | margin-bottom: 0; 43 | } 44 | 45 | small { 46 | color: $gray-600; 47 | } 48 | } 49 | } 50 | 51 | @media (max-width: map-get($grid-breakpoints, 'md')) { 52 | .page { 53 | padding-left: 0; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /docs/www/scss/_sidebar-toggle.scss: -------------------------------------------------------------------------------- 1 | .sidebar-toggle { 2 | display: none; 3 | margin: 0 1rem; 4 | 5 | .icon-bar { 6 | background: $body-color; 7 | display: block; 8 | width: 1.625rem; 9 | height: .1875rem; 10 | border-radius: .0625rem; 11 | margin: .25rem 0 .25rem 0; 12 | transition: all 0.2s; 13 | 14 | &:nth-of-type(1) { 15 | transform: rotate(0); 16 | transform-origin: 10% 10%; 17 | } 18 | 19 | &:nth-of-type(2) { 20 | opacity: 1; 21 | } 22 | 23 | &:nth-of-type(3) { 24 | transform: rotate(0); 25 | transform-origin: 10% 90%; 26 | } 27 | } 28 | } 29 | 30 | .sidebar-show .icon-bar { 31 | &:nth-of-type(1) { 32 | transform: rotate(45deg); 33 | } 34 | 35 | &:nth-of-type(2) { 36 | opacity: 0; 37 | } 38 | 39 | &:nth-of-type(3) { 40 | transform: rotate(-45deg); 41 | } 42 | } 43 | 44 | @media (max-width: map-get($grid-breakpoints, 'md')) { 45 | .sidebar-toggle { 46 | display: block; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /docs/www/scss/_sidebar.scss: -------------------------------------------------------------------------------- 1 | .sidebar { 2 | position: fixed; 3 | top: $header-height; 4 | left: 0; 5 | bottom: 0; 6 | width: $sidebar-width; 7 | background-color: $white; 8 | overflow-y: auto; 9 | z-index: $zindex-fixed; 10 | transition: width .2s ease-in-out; 11 | background-color: $gray-100; 12 | 13 | ul { 14 | list-style-type: none; 15 | line-height: 1.8; 16 | margin: $base-spacing 0; 17 | padding: 0 $base-spacing; 18 | white-space: nowrap; 19 | 20 | li { 21 | font-weight: 500; 22 | margin-bottom: .75rem; 23 | 24 | a { 25 | color: $body-color; 26 | font-weight: 400; 27 | text-decoration: none; 28 | 29 | &:hover { 30 | color: $primary; 31 | text-decoration: none; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | @media (max-width: map-get($grid-breakpoints, 'md')) { 39 | .sidebar { 40 | width: 0; 41 | } 42 | 43 | .sidebar-show .sidebar { 44 | width: $sidebar-width; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Marx J. Moura 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Jobs/IJob.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Jobs; 26 | 27 | public interface IJob 28 | { 29 | Task RunAsync(); 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | CronQuery 3 |

4 | CronQuery 5 |

6 |

7 | Lightweight job runner for ASP.NET Core 8 |
9 | 10 | Explore CronQuery docs 11 | 12 |

13 |

14 | 15 | ## Status 16 | 17 | [![CircleCI](https://circleci.com/gh/marxjmoura/cronquery.svg?style=shield)](https://circleci.com/gh/marxjmoura/cronquery) 18 | [![codecov](https://codecov.io/gh/marxjmoura/cronquery/branch/master/graph/badge.svg)](https://codecov.io/gh/marxjmoura/cronquery) 19 | [![NuGet Version](https://img.shields.io/nuget/v/cronquery.svg)](https://img.shields.io/nuget/v/cronquery.svg) 20 | [![NuGet Downloads](https://img.shields.io/nuget/dt/cronquery.svg)](https://www.nuget.org/packages/cronquery) 21 | 22 | ## Changelog 23 | 24 | The changes for each release are documented in the [release notes](https://github.com/marxjmoura/cronquery/releases). 25 | 26 | ## Bugs and features 27 | 28 | Please, fell free to [open a new issue](https://github.com/marxjmoura/cronquery/issues/new) on GitHub. 29 | 30 | ## License 31 | 32 | [MIT](https://github.com/marxjmoura/cronquery/blob/master/LICENSE) 33 | 34 | Copyright (c) 2018-present, [Marx J. Moura](https://github.com/marxjmoura) 35 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version : 2.1 2 | 3 | executors: 4 | ubuntu: 5 | machine: 6 | image: ubuntu-2004:202201-02 7 | 8 | jobs: 9 | build: 10 | executor: ubuntu 11 | steps: 12 | - checkout 13 | - run: ./tools/install-dotnet.sh 8.0.100 14 | - run: dotnet tool install --local dotnet-reportgenerator-globaltool --version 5.2.0 15 | - run: ./tools/test.sh 16 | - run: curl -s https://codecov.io/bash > ./codecov 17 | - run: chmod +x ./codecov 18 | - run: ./codecov -f "./src/CronQuery.Tests/coverage/opencover.xml" -t $CODECOV_TOKEN 19 | - store_artifacts: 20 | path: src/CronQuery.Tests/coverage/report 21 | destination: coverage 22 | deploy: 23 | executor: ubuntu 24 | steps: 25 | - checkout 26 | - run: ./tools/install-dotnet.sh 8.0.100 27 | - run: dotnet pack src/CronQuery.API/CronQuery.API.csproj -o dist 28 | - run: dotnet nuget push dist/CronQuery.${CIRCLE_TAG/v/}.nupkg -k $NUGET_TOKEN -s https://api.nuget.org/v3/index.json 29 | 30 | workflows: 31 | version: 2.1 32 | build_and_deploy: 33 | jobs: 34 | - build: 35 | filters: 36 | tags: 37 | only: /.*/ 38 | - deploy: 39 | requires: 40 | - build 41 | filters: 42 | tags: 43 | only: /^v\d\.\d\.\d/ 44 | branches: 45 | ignore: /.*/ 46 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/CronQuery.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | 12.0 5 | enable 6 | enable 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | all 19 | runtime; build; native; contentfiles; analyzers 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | PreserveNewest 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Options/JobOptions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Options; 26 | 27 | public sealed class JobOptions 28 | { 29 | public bool Running { get; set; } 30 | 31 | public string Name { get; set; } = null!; 32 | 33 | public string Cron { get; set; } = null!; 34 | } 35 | -------------------------------------------------------------------------------- /example/Jobs/MySecondJob.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace Example.Jobs; 26 | 27 | using System; 28 | using System.Threading.Tasks; 29 | using CronQuery.Mvc.Jobs; 30 | 31 | public class MySecondJob : IJob 32 | { 33 | public Task RunAsync() 34 | { 35 | throw new NotImplementedException(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Options/JobRunnerOptions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Options; 26 | 27 | public sealed class JobRunnerOptions 28 | { 29 | public bool Running { get; set; } 30 | 31 | public string TimeZone { get; set; } = null!; 32 | 33 | public ICollection Jobs { get; } = new List(); 34 | } 35 | -------------------------------------------------------------------------------- /docs/www/versions.hbs: -------------------------------------------------------------------------------- 1 | {{#> www/page }} 2 | 3 | {{#*inline "body-block"}} 4 | 5 |

6 | Major versions of CronQuery only support the current versions of .NET Long Term Support (LTS). 7 |

8 | 9 |

10 | # Releases 11 |

12 | 13 |

14 | The changes and breaking changes for each release are documented on the 15 | GitHub release notes page. 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 |
VersionTarget FrameworkRelease Month
v3.1.NET 8December, 2023
v3.0.NET 6April, 2023
v2.x.NET Core 3.1March, 2020
v1.x.NET Core 2.2January, 2019
48 |
49 | 50 |
51 | Install previous versions of CronQuery 52 | if you need to use end-of-life versions of .NET 53 |
54 | 55 | {{/inline}} 56 | 57 | {{/www/page}} 58 | -------------------------------------------------------------------------------- /src/CronQuery.API/CronQuery.API.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net8.0 4 | 12.0 5 | enable 6 | enable 7 | CronQuery 8 | CronQuery 9 | 3.1.0 10 | 3.1.0 11 | Marx J. Moura 12 | Marx J. Moura 13 | Copyright (c) 2018 Marx J. Moura 14 | Lightweight job runner for ASP.NET Core. 15 | git 16 | https://github.com/marxjmoura/cronquery 17 | CronQuery 18 | https://marxjmoura.github.io/cronquery 19 | https://github.com/marxjmoura/cronquery/blob/master/LICENSE 20 | https://github.com/marxjmoura/cronquery/releases 21 | false 22 | job runner;background tasks;scheduler 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /example/Jobs/MyFirstJob.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace Example.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using System; 29 | using System.Threading.Tasks; 30 | 31 | public class MyFirstJob : IJob 32 | { 33 | public Task RunAsync() 34 | { 35 | Console.WriteLine($"My first job: {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); 36 | 37 | return Task.CompletedTask; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/LoggerFactoryFake.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes; 26 | 27 | using Microsoft.Extensions.Logging; 28 | 29 | public sealed class LoggerFactoryFake : ILoggerFactory 30 | { 31 | public LoggerFake Logger { get; } = new LoggerFake(); 32 | 33 | public void AddProvider(ILoggerProvider provider) { } 34 | 35 | public ILogger CreateLogger(string categoryName) 36 | { 37 | return Logger; 38 | } 39 | 40 | public void Dispose() { } 41 | } 42 | -------------------------------------------------------------------------------- /example/Program.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | using CronQuery.Mvc.DependencyInjection; 26 | using Example.Jobs; 27 | using Microsoft.AspNetCore.Builder; 28 | using Microsoft.Extensions.DependencyInjection; 29 | 30 | var builder = WebApplication.CreateBuilder(args); 31 | 32 | builder.Services.AddCronQuery(builder.Configuration.GetSection("CronQuery")); 33 | 34 | builder.Services.AddTransient(); 35 | builder.Services.AddTransient(); 36 | 37 | var api = builder.Build(); 38 | 39 | api.MapGet("/", () => "Jobs are running..."); 40 | 41 | api.Run(); 42 | -------------------------------------------------------------------------------- /docs/www/_page.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | CronQuery 14 | {{#> head-block}}{{/head-block}} 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | CronQuery 26 |
27 | 34 |
35 |
36 | 39 |

40 | CronQuery 41 |

42 | {{#> body-block}}{{/body-block}} 43 |
44 |
45 | 46 | {{#> scripts-block}}{{/scripts-block}} 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/LoggerFake.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes; 26 | 27 | using Microsoft.Extensions.Logging; 28 | 29 | public sealed class LoggerFake : ILogger 30 | { 31 | public ICollection Messages { get; } = new List(); 32 | 33 | public IDisposable BeginScope(TState state) => null!; 34 | 35 | public bool IsEnabled(LogLevel logLevel) => true; 36 | 37 | public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, 38 | Func formatter) 39 | { 40 | Messages.Add(formatter(state, exception)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/TestServerExtensions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using Microsoft.AspNetCore.TestHost; 29 | using Microsoft.Extensions.DependencyInjection; 30 | using Microsoft.Extensions.Logging; 31 | 32 | public static class TestServerExtensions 33 | { 34 | public static LoggerFake Logger(this TestServer server) => 35 | (LoggerFake)server.Host.Services.GetRequiredService().CreateLogger(string.Empty); 36 | 37 | public static TJob Job(this TestServer server) where TJob : IJob => 38 | server.Host.Services.GetRequiredService(); 39 | } 40 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/DependencyInjection/CronQueryExtensions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.DependencyInjection; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | using Microsoft.Extensions.Configuration; 30 | using Microsoft.Extensions.DependencyInjection; 31 | 32 | public static class CronQueryExtensions 33 | { 34 | public static void AddCronQuery(this IServiceCollection services, IConfigurationSection configuration) 35 | { 36 | services.Configure(configuration); 37 | 38 | services.AddSingleton(); 39 | services.AddSingleton(serviceProvider => new JobCollection(services)); 40 | 41 | services.AddHostedService(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /docs/handlebars.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const through = require('through2') 4 | const handlebars = require('handlebars') 5 | const PluginError = require('plugin-error') 6 | 7 | const ROOTDIR = process.cwd() 8 | const PLUGIN_NAME = 'gulp-site-engine' 9 | 10 | handlebars.registerHelper('baseUrl', function () { 11 | return process.env.NODE_ENV === 'production' ? 'https://marxjmoura.github.io/cronquery' : '' 12 | }) 13 | 14 | handlebars.registerHelper('include', function (file) { 15 | try { 16 | const source = fs.readFileSync(file, 'utf8') 17 | const template = handlebars.compile(source) 18 | 19 | return template({}) 20 | } catch (error) { 21 | console.error(PLUGIN_NAME, error.message) 22 | return '' 23 | } 24 | }) 25 | 26 | function transform(file, encoding, callback) { 27 | if (file.isNull()) { 28 | // nothing to do 29 | return callback(null, file) 30 | } 31 | 32 | if (file.isStream()) { 33 | this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!')) 34 | } 35 | 36 | const filedir = path.parse(file.path).dir 37 | const filename = path.parse(file.path).name 38 | const isPartial = filename.startsWith('_') 39 | const source = file.contents.toString() 40 | 41 | if (isPartial) { 42 | const partialPrefix = filedir.replace(ROOTDIR + '/', '') 43 | const partialSuffix = filename.replace('_', '') 44 | const partialName = path.join(partialPrefix, partialSuffix) 45 | 46 | handlebars.registerPartial(partialName, source) 47 | } else { 48 | try { 49 | const template = handlebars.compile(source) 50 | const compiledHTML = template({}) 51 | 52 | file.contents = Buffer.from(compiledHTML) 53 | } catch (err) { 54 | this.emit('error', new PluginError(PLUGIN_NAME, err)) 55 | } 56 | 57 | this.push(file) 58 | } 59 | 60 | return callback() 61 | } 62 | 63 | module.exports = function () { 64 | return through.obj(transform) 65 | } 66 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/Jobs/JobNotEnqueued.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | 30 | public sealed class JobNotEnqueued : IJob 31 | { 32 | public Task RunAsync() 33 | { 34 | return Task.CompletedTask; 35 | } 36 | 37 | public static JobRunnerOptions Options 38 | { 39 | get 40 | { 41 | var options = new JobRunnerOptions(); 42 | options.Running = true; 43 | options.Jobs.Add(new JobOptions 44 | { 45 | Running = true, 46 | Name = nameof(JobNotEnqueued), 47 | Cron = "* * * * * *" 48 | }); 49 | 50 | return options; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/Jobs/JobWithError.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | 30 | public sealed class JobWithError : IJob 31 | { 32 | public Task RunAsync() 33 | { 34 | throw new NotImplementedException(); 35 | } 36 | 37 | public static JobRunnerOptions Options 38 | { 39 | get 40 | { 41 | var options = new JobRunnerOptions(); 42 | options.Running = true; 43 | options.Jobs.Add(new JobOptions 44 | { 45 | Running = true, 46 | Name = nameof(JobWithError), 47 | Cron = "* * * * * *" 48 | }); 49 | 50 | return options; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/Jobs/JobBadlyConfigured.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | 30 | public sealed class JobBadlyConfigured : IJob 31 | { 32 | public Task RunAsync() 33 | { 34 | return Task.CompletedTask; 35 | } 36 | 37 | public static JobRunnerOptions Options 38 | { 39 | get 40 | { 41 | var options = new JobRunnerOptions(); 42 | options.Running = true; 43 | options.Jobs.Add(new JobOptions 44 | { 45 | Running = true, 46 | Name = nameof(JobBadlyConfigured), 47 | Cron = "IN V A L I D" 48 | }); 49 | 50 | return options; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Jobs/JobCollection.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Jobs; 26 | 27 | using Microsoft.Extensions.DependencyInjection; 28 | using System.Collections; 29 | 30 | public sealed class JobCollection : IEnumerable 31 | { 32 | private readonly IList _descriptors; 33 | 34 | public JobCollection(IList descriptors) 35 | { 36 | _descriptors = descriptors; 37 | } 38 | 39 | public IEnumerator GetEnumerator() 40 | { 41 | return _descriptors 42 | .Where(service => typeof(IJob).IsAssignableFrom(service.ServiceType)) 43 | .GetEnumerator(); 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/CronQuery.API/Extensions/Int32Extensions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Extensions; 26 | 27 | internal static class Int32Extensions 28 | { 29 | public static IEnumerable Step(this IEnumerable source, int step) 30 | { 31 | if (step == 0) 32 | { 33 | return source; 34 | } 35 | 36 | return source.Where((value, index) => index % step == 0); 37 | } 38 | 39 | public static IEnumerable To(this int from, int to) 40 | { 41 | if (from <= to) 42 | { 43 | while (from <= to) 44 | { 45 | yield return from++; 46 | } 47 | } 48 | else 49 | { 50 | while (from >= to) 51 | { 52 | yield return from--; 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/Jobs/JobStopped.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | 30 | public sealed class JobStopped : IJob 31 | { 32 | public bool Executed { get; private set; } 33 | 34 | public Task RunAsync() 35 | { 36 | Executed = true; 37 | 38 | return Task.CompletedTask; 39 | } 40 | 41 | public static JobRunnerOptions Options 42 | { 43 | get 44 | { 45 | var options = new JobRunnerOptions(); 46 | options.Running = true; 47 | options.Jobs.Add(new JobOptions 48 | { 49 | Running = false, 50 | Name = nameof(JobStopped), 51 | Cron = "* * * * * *" 52 | }); 53 | 54 | return options; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/Jobs/JobSuccessful.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes.Jobs; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Mvc.Options; 29 | 30 | public sealed class JobSuccessful : IJob, IDisposable 31 | { 32 | public bool Executed { get; private set; } 33 | 34 | public void Dispose() { } 35 | 36 | public Task RunAsync() 37 | { 38 | Executed = true; 39 | 40 | return Task.CompletedTask; 41 | } 42 | 43 | public static JobRunnerOptions Options 44 | { 45 | get 46 | { 47 | var options = new JobRunnerOptions(); 48 | options.Running = true; 49 | options.Jobs.Add(new JobOptions 50 | { 51 | Running = true, 52 | Name = nameof(JobSuccessful), 53 | Cron = "* * * * * *" 54 | }); 55 | 56 | return options; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/UnreachableConditionTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class UnreachableConditionTest 31 | { 32 | [Fact] 33 | public void ShouldNotEvaluateNearestWeekdayOnlyOnSunday() 34 | { 35 | var expression = new CronExpression("0 0 8 15W * 0"); 36 | var current = new DateTime(2019, 01, 01, 00, 00, 00); 37 | var expected = DateTime.MinValue; 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldNotEvaluateOnlyMonthsThatNotReachTheGivenDay() 44 | { 45 | var expression = new CronExpression("0 0 8 31 2,4,6 *"); 46 | var current = new DateTime(2019, 01, 01, 00, 00, 00); 47 | var expected = DateTime.MinValue; 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithHashTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithHashTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextDayOfWeekInTheSameMonth() 34 | { 35 | var expression = new CronExpression("* * * * * 1#3"); 36 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 37 | var expected = new DateTime(2018, 12, 17, 00, 00, 00); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextDayOfWeekInTheNextMonth() 44 | { 45 | var expression = new CronExpression("* * * * * 1#3"); 46 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 47 | var expected = new DateTime(2019, 01, 21, 00, 00, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Functional/AppTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Functional; 26 | 27 | using CronQuery.Tests.Fakes; 28 | using CronQuery.Tests.Fakes.Jobs; 29 | using Xunit; 30 | 31 | public sealed class AppTest 32 | { 33 | [Fact] 34 | public async Task Run() 35 | { 36 | var server = TestProgram.CreateServer(); 37 | 38 | await Task.Delay(1500); // Waiting for the jobs 39 | await server.Host.StopAsync(); 40 | 41 | Assert.True(server.Job().Executed); 42 | Assert.False(server.Job().Executed); 43 | 44 | Assert.Contains(server.Logger().Messages, message => 45 | message == $"Job '{nameof(JobWithError)}' failed during running."); 46 | 47 | Assert.Contains(server.Logger().Messages, message => 48 | message == $"Job {nameof(JobNotEnqueued)} is not in the queue."); 49 | 50 | Assert.Contains(server.Logger().Messages, message => 51 | message == $"Invalid cron expression for '{nameof(JobBadlyConfigured)}'."); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/ServiceProviderFake.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using Microsoft.Extensions.DependencyInjection; 29 | 30 | public sealed class ServiceProviderFake : IServiceProvider, IServiceScopeFactory, IServiceScope 31 | { 32 | private readonly IDictionary _instances; 33 | 34 | public ServiceProviderFake() 35 | { 36 | _instances = new Dictionary(); 37 | } 38 | 39 | public IServiceProvider ServiceProvider => this; 40 | 41 | public IServiceScope CreateScope() => this; 42 | 43 | public void Dispose() { } 44 | 45 | public object? GetService(Type serviceType) 46 | { 47 | if (typeof(IJob).IsAssignableFrom(serviceType)) 48 | { 49 | if (!_instances.ContainsKey(serviceType)) 50 | { 51 | _instances.Add(serviceType, Activator.CreateInstance(serviceType)!); 52 | } 53 | 54 | return _instances[serviceType]; 55 | } 56 | 57 | return null; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/CronQuery.API/Cron/TimeUnit.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Cron; 26 | 27 | internal sealed class TimeUnit 28 | { 29 | private TimeUnit(int minValue, int maxValue) 30 | { 31 | MinValue = minValue; 32 | MaxValue = maxValue; 33 | } 34 | 35 | public int MinValue { get; private set; } 36 | public int MaxValue { get; private set; } 37 | public bool IsSecond { get; private set; } 38 | public bool IsMinute { get; private set; } 39 | public bool IsHour { get; private set; } 40 | public bool IsDay { get; private set; } 41 | public bool IsMonth { get; private set; } 42 | public bool IsDayOfWeek { get; private set; } 43 | 44 | public static TimeUnit Second => new TimeUnit(0, 59) { IsSecond = true }; 45 | public static TimeUnit Minute => new TimeUnit(0, 59) { IsMinute = true }; 46 | public static TimeUnit Hour => new TimeUnit(0, 23) { IsHour = true }; 47 | public static TimeUnit Day => new TimeUnit(1, 31) { IsDay = true }; 48 | public static TimeUnit Month => new TimeUnit(1, 12) { IsMonth = true }; 49 | public static TimeUnit DayOfWeek => new TimeUnit(0, 6) { IsDayOfWeek = true }; 50 | } 51 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Fakes/OptionsMonitorFake.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Fakes; 26 | 27 | using CronQuery.Mvc.Options; 28 | using Microsoft.Extensions.Options; 29 | 30 | public sealed class OptionsMonitorFake : IOptionsMonitor, IDisposable 31 | { 32 | private readonly JobRunnerOptions _options; 33 | 34 | private Action _listener = null!; 35 | 36 | public OptionsMonitorFake(JobRunnerOptions options) 37 | { 38 | _options = options; 39 | } 40 | 41 | public JobRunnerOptions CurrentValue => _options; 42 | 43 | public void Change(Action change) 44 | { 45 | change(_options); 46 | 47 | if (_listener != null) 48 | { 49 | _listener(_options, string.Empty); 50 | } 51 | } 52 | 53 | public void Dispose() 54 | { 55 | _listener = null!; 56 | } 57 | 58 | public JobRunnerOptions Get(string name) 59 | { 60 | return _options; 61 | } 62 | 63 | public IDisposable OnChange(Action listener) 64 | { 65 | _listener = listener; 66 | 67 | return this; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Options/TimeZoneOptions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Options; 26 | 27 | using System.Text.RegularExpressions; 28 | 29 | public sealed class TimeZoneOptions 30 | { 31 | const string UtcOffsetRegex = @"^UTC[+-]\d{2}:\d{2}$"; 32 | 33 | private readonly string _timeZone; 34 | 35 | public TimeZoneOptions(string timeZone) 36 | { 37 | _timeZone = timeZone; 38 | } 39 | 40 | public TimeZoneInfo ToTimeZoneInfo() 41 | { 42 | if (string.IsNullOrWhiteSpace(_timeZone)) 43 | { 44 | return TimeZoneInfo.Utc; 45 | } 46 | else if (Regex.IsMatch(_timeZone, UtcOffsetRegex)) 47 | { 48 | var timeSpanString = Regex.Replace(_timeZone, "UTC[+]?", string.Empty); 49 | 50 | return TimeZoneInfo.CreateCustomTimeZone( 51 | id: "CronQuery", 52 | baseUtcOffset: TimeSpan.Parse(timeSpanString), 53 | displayName: $"({_timeZone}) CronQuery", 54 | standardDisplayName: "CronQuery Custom Time" 55 | ); 56 | } 57 | else 58 | { 59 | return TimeZoneInfo.FindSystemTimeZoneById(_timeZone); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Runner/TimeZoneTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License* 3 | * Copyright (c) 2018 Marx J. Moura 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | 24 | namespace CronQuery.Tests.Unit.Runner; 25 | 26 | using CronQuery.Mvc.Options; 27 | using Xunit; 28 | 29 | public sealed class TimeZoneTest 30 | { 31 | private readonly DateTime _mockUtcTime; 32 | 33 | public TimeZoneTest() 34 | { 35 | _mockUtcTime = new DateTime(2000, 01, 01, 00, 00, 00, DateTimeKind.Utc); 36 | } 37 | 38 | [Theory] 39 | [InlineData(null, "2000-01-01T00:00:00")] 40 | [InlineData("", "2000-01-01T00:00:00")] 41 | [InlineData(" ", "2000-01-01T00:00:00")] 42 | [InlineData("UTC+00:00", "2000-01-01T00:00:00")] 43 | [InlineData("UTC+01:00", "2000-01-01T01:00:00")] 44 | [InlineData("UTC-03:00", "1999-12-31T21:00:00")] 45 | [InlineData("UTC+01:30", "2000-01-01T01:30:00")] 46 | [InlineData("Europe/Budapest", "2000-01-01T01:00:00")] 47 | [InlineData("America/Sao_Paulo", "1999-12-31T22:00:00")] 48 | public void ShouldConvertToLocalTime(string timeZone, string expected) 49 | { 50 | var timeZoneOptions = new TimeZoneOptions(timeZone); 51 | var timeZoneInfo = timeZoneOptions.ToTimeZoneInfo(); 52 | var localDateTime = TimeZoneInfo.ConvertTime(_mockUtcTime, timeZoneInfo); 53 | var iso8601Format = localDateTime.ToString("yyyy-MM-ddTHH:mm:ss"); 54 | 55 | Assert.Equal(expected, iso8601Format); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /docs/gulpfile.js: -------------------------------------------------------------------------------- 1 | const gulp = require('gulp') 2 | const autoprefixer = require('gulp-autoprefixer') 3 | const babelify = require('babelify') 4 | const bro = require('gulp-bro') 5 | const connect = require('gulp-connect') 6 | const htmlmin = require('gulp-htmlmin') 7 | const rename = require("gulp-rename") 8 | const sass = require('gulp-sass')(require('sass')) 9 | const uglify = require('gulp-uglify') 10 | const handlebars = require('./handlebars') 11 | 12 | function printError(error) { 13 | console.log(error) 14 | } 15 | 16 | function scss() { 17 | return gulp.src('www/scss/docs.scss') 18 | .pipe(sass({ outputStyle: 'compressed' }).on('error', printError)) 19 | .pipe(autoprefixer({ overrideBrowserslist: ['last 2 versions'], cascade: false })) 20 | .pipe(rename({ extname: '.min.css' })) 21 | .pipe(gulp.dest('dist')) 22 | } 23 | 24 | function js() { 25 | return gulp.src('www/js/docs.js') 26 | .pipe(bro({ transform: [babelify.configure({ presets: ['@babel/preset-env'] })] })) 27 | .pipe(uglify().on('error', printError)) 28 | .pipe(rename({ extname: '.min.js' })) 29 | .pipe(gulp.dest('dist')) 30 | } 31 | 32 | function hbs() { 33 | return gulp.src(['www/**/_*.hbs', 'www/**/*.hbs'], { base: './www' }) 34 | .pipe(handlebars()) 35 | .pipe(rename({ extname: '.html' })) 36 | .pipe(htmlmin({ collapseWhitespace: true }).on('error', printError)) 37 | .pipe(gulp.dest('dist')) 38 | } 39 | 40 | function fonts() { 41 | return gulp.src('www/**/*.ttf') 42 | .pipe(gulp.dest('dist')) 43 | } 44 | 45 | function img() { 46 | return gulp.src(['www/**/*.{svg,jpg,png,ico}', '!www/**/_*.*']) 47 | .pipe(gulp.dest('dist')) 48 | } 49 | 50 | function build(done) { 51 | gulp.parallel(scss, js, hbs, fonts, img)(done) 52 | } 53 | 54 | function watch(done) { 55 | gulp.watch('www/**/*.scss', gulp.series(scss)) 56 | gulp.watch('www/**/*.js', gulp.series(js)) 57 | gulp.watch('www/**/*.hbs', gulp.series(hbs)) 58 | gulp.watch('www/**/*.{svg,jpg,png,ico}', gulp.series(fonts)) 59 | gulp.watch('www/**/*.ttf', gulp.series(img)) 60 | 61 | done() 62 | } 63 | 64 | function serve(done) { 65 | connect.server({ root: 'dist', port: 8888, fallback: 'dist/404.html' }) 66 | connect.serverClose() 67 | 68 | done() 69 | } 70 | 71 | function start(done) { 72 | process.env.NODE_ENV = 'development' 73 | gulp.series(serve, watch, build)(done) 74 | } 75 | 76 | function publish(done) { 77 | process.env.NODE_ENV = 'production' 78 | gulp.series(build)(done) 79 | } 80 | 81 | exports.start = start 82 | exports.publish = publish 83 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Jobs/JobInterval.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Jobs; 26 | 27 | using CronQuery.Cron; 28 | using System.Reactive.Linq; 29 | 30 | public sealed class JobInterval : IDisposable 31 | { 32 | private readonly CronExpression _cron; 33 | private readonly TimeZoneInfo _timezone; 34 | private readonly Func _work; 35 | 36 | private IDisposable _subscription = null!; 37 | 38 | public JobInterval(CronExpression cron, TimeZoneInfo timezone, Func work) 39 | { 40 | _cron = cron ?? throw new ArgumentNullException(nameof(cron)); 41 | _timezone = timezone ?? throw new ArgumentNullException(nameof(timezone)); 42 | _work = work ?? throw new ArgumentNullException(nameof(work)); 43 | } 44 | 45 | public void Dispose() 46 | { 47 | if (_subscription != null) 48 | { 49 | _subscription.Dispose(); 50 | } 51 | } 52 | 53 | public void Run() 54 | { 55 | var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, _timezone); 56 | var nextTime = _cron.Next(now); 57 | 58 | if (nextTime == DateTime.MinValue) 59 | { 60 | return; 61 | } 62 | 63 | var interval = nextTime - now; 64 | 65 | _subscription = Observable.Timer(interval) 66 | .Select(tick => Observable.FromAsync(_work)) 67 | .Concat() 68 | .Subscribe( 69 | onNext: tick => { /* noop */ }, 70 | onCompleted: Run 71 | ); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithAsteriskTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithAsteriskTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextSecond() 34 | { 35 | var expression = new CronExpression("* * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 30, 20); 37 | var expected = new DateTime(2018, 12, 30, 08, 30, 21); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinute() 44 | { 45 | var expression = new CronExpression("* * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 47 | var expected = new DateTime(2018, 12, 30, 08, 31, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextHour() 54 | { 55 | var expression = new CronExpression("* * * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 59, 59); 57 | var expected = new DateTime(2018, 12, 30, 09, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextDay() 64 | { 65 | var expression = new CronExpression("* * * * * *"); 66 | var current = new DateTime(2018, 12, 30, 23, 59, 59); 67 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextMonth() 74 | { 75 | var expression = new CronExpression("* * * * * *"); 76 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 77 | var expected = new DateTime(2019, 01, 01, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithCommaTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithCommaTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextMinute() 34 | { 35 | var expression = new CronExpression("10,20,30 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 30, 10); 37 | var expected = new DateTime(2018, 12, 30, 08, 30, 20); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinuteAfterLastSecond() 44 | { 45 | var expression = new CronExpression("10,20,30 * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 30, 30); 47 | var expected = new DateTime(2018, 12, 30, 08, 31, 10); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextHourAfterLastMinute() 54 | { 55 | var expression = new CronExpression("* 10,20,30 * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 57 | var expected = new DateTime(2018, 12, 30, 09, 10, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextDayAfterLastHour() 64 | { 65 | var expression = new CronExpression("* * 6,18 * * *"); 66 | var current = new DateTime(2018, 12, 30, 18, 59, 59); 67 | var expected = new DateTime(2018, 12, 31, 06, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextMonthAfterLastDay() 74 | { 75 | var expression = new CronExpression("* * * 15,20,25 * *"); 76 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 77 | var expected = new DateTime(2019, 01, 15, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Functional/TestProgram.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Functional; 26 | 27 | using CronQuery.Mvc.DependencyInjection; 28 | using CronQuery.Tests.Fakes; 29 | using CronQuery.Tests.Fakes.Jobs; 30 | using Microsoft.AspNetCore.Builder; 31 | using Microsoft.AspNetCore.Hosting; 32 | using Microsoft.AspNetCore.TestHost; 33 | using Microsoft.Extensions.Configuration; 34 | using Microsoft.Extensions.DependencyInjection; 35 | using Microsoft.Extensions.Logging; 36 | using System.Diagnostics; 37 | 38 | public static class TestProgram 39 | { 40 | public static TestServer CreateServer() 41 | { 42 | var host = new WebHostBuilder() 43 | .UseEnvironment("Testing") 44 | .ConfigureAppConfiguration((builderContext, config) => 45 | { 46 | var targetFramework = "net6.0"; 47 | var mode = Debugger.IsAttached ? "Debug" : "Release"; 48 | var buildPath = Path.Combine("bin", mode, targetFramework); 49 | var projectPath = AppContext.BaseDirectory.Replace($"{buildPath}/", string.Empty); 50 | var appsettings = Path.Combine(projectPath, "Functional", "appsettings.json"); 51 | 52 | config.AddJsonFile(appsettings, optional: false, reloadOnChange: true); 53 | }) 54 | .ConfigureServices((builder, services) => 55 | { 56 | services.AddRouting(); 57 | 58 | services.AddCronQuery(builder.Configuration.GetSection("CronQuery")); 59 | 60 | services.AddSingleton(); 61 | 62 | services.AddSingleton(); 63 | services.AddSingleton(); 64 | services.AddSingleton(); 65 | services.AddSingleton(); 66 | }) 67 | .Configure(builder => 68 | { 69 | builder.UseRouting(); 70 | builder.UseEndpoints(api => api.MapGet("/", () => "Testing jobs...")); 71 | }); 72 | 73 | return new(host); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Runner/JobIntervalTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Runner; 26 | 27 | using CronQuery.Cron; 28 | using CronQuery.Mvc.Jobs; 29 | using Xunit; 30 | 31 | public sealed class JobIntervalTest 32 | { 33 | [Fact] 34 | public void ShouldRunWork() 35 | { 36 | var fires = 0; 37 | var cron = new CronExpression("* * * * * *"); 38 | var interval = new JobInterval(cron, TimeZoneInfo.Utc, () => Task.FromResult(++fires)); 39 | 40 | interval.Run(); 41 | 42 | Task.Delay(2500).GetAwaiter().GetResult(); // Waiting for the job 43 | 44 | Assert.Equal(2, fires); 45 | } 46 | 47 | [Fact] 48 | public void ShouldNotRunWorkAfterDispose() 49 | { 50 | var fires = 0; 51 | var cron = new CronExpression("* * * * * *"); 52 | var interval = new JobInterval(cron, TimeZoneInfo.Utc, () => Task.FromResult(++fires)); 53 | 54 | interval.Run(); 55 | 56 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 57 | 58 | interval.Dispose(); 59 | 60 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 61 | 62 | Assert.Equal(1, fires); 63 | } 64 | 65 | [Fact] 66 | public void ShouldNotRunWorkForUnreachableCondition() 67 | { 68 | var fires = 0; 69 | var cron = new CronExpression("* * * 30 2 *"); 70 | var interval = new JobInterval(cron, TimeZoneInfo.Utc, () => Task.FromResult(++fires)); 71 | 72 | interval.Run(); 73 | 74 | Assert.Equal(0, fires); 75 | } 76 | 77 | [Fact] 78 | public void ShouldNotInstanceWithNullCron() 79 | { 80 | Assert.Throws(() => new JobInterval(null!, TimeZoneInfo.Utc, () => Task.CompletedTask)); 81 | } 82 | 83 | [Fact] 84 | public void ShouldNotInstanceWithNullNullTimezone() 85 | { 86 | var cron = new CronExpression("* * * 30 2 *"); 87 | 88 | Assert.Throws(() => new JobInterval(cron, null!, () => Task.CompletedTask)); 89 | } 90 | 91 | [Fact] 92 | public void ShouldNotInstanceWithNullWork() 93 | { 94 | var cron = new CronExpression("* * * 30 2 *"); 95 | 96 | Assert.Throws(() => new JobInterval(cron, TimeZoneInfo.Utc, null!)); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/InvalidExpressionTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class InvalidExpressionTest 31 | { 32 | [Fact] 33 | public void ShouldNotEvaluateLowerThan6Fields() 34 | { 35 | var expression = new CronExpression("* * * * *"); 36 | var current = DateTime.UtcNow; 37 | var expected = DateTime.MinValue; 38 | 39 | Assert.False(expression.IsValid); 40 | Assert.Equal(expected, expression.Next(current)); 41 | } 42 | 43 | [Fact] 44 | public void ShouldNotEvaluateMoreThan6Fields() 45 | { 46 | var expression = new CronExpression("* * * * * * *"); 47 | var current = DateTime.UtcNow; 48 | var expected = DateTime.MinValue; 49 | 50 | Assert.False(expression.IsValid); 51 | Assert.Equal(expected, expression.Next(current)); 52 | } 53 | 54 | [Fact] 55 | public void ShouldNotEvaluateInvalidExpression() 56 | { 57 | var expression = new CronExpression("IN V A L I D"); 58 | var current = DateTime.UtcNow; 59 | var expected = DateTime.MinValue; 60 | 61 | Assert.False(expression.IsValid); 62 | Assert.Equal(expected, expression.Next(current)); 63 | } 64 | 65 | [Fact] 66 | public void ShouldIgnoreSecondOutOfRange() 67 | { 68 | var expression = new CronExpression("99 * * * * *"); 69 | var current = new DateTime(2019, 01, 01, 00, 00, 00); 70 | var expected = new DateTime(2019, 01, 01, 00, 00, 59); 71 | 72 | Assert.Equal(expected, expression.Next(current)); 73 | } 74 | 75 | [Fact] 76 | public void ShouldIgnoreIncrementByZero() 77 | { 78 | var expression = new CronExpression("*/0 * * * * *"); 79 | var current = new DateTime(2019, 01, 01, 00, 00, 00); 80 | var expected = new DateTime(2019, 01, 01, 00, 00, 01); 81 | 82 | Assert.Equal(expected, expression.Next(current)); 83 | } 84 | 85 | [Fact] 86 | public void ShouldIgnoreCharacterNotAllowed() 87 | { 88 | var expression = new CronExpression("* * 10#5 * * *"); 89 | var current = new DateTime(2019, 01, 01, 00, 00, 00); 90 | var expected = DateTime.MinValue; 91 | 92 | Assert.Equal(expected, expression.Next(current)); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithLAndWTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithLAndWTest 31 | { 32 | [Fact] 33 | public void ShouldGetNearestWeekdayFromWeekday() 34 | { 35 | var expression = new CronExpression("* * * LW * *"); 36 | var current = new DateTime(2018, 12, 01, 08, 00, 00); 37 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMonthNearestWeekdayFromWeekday() 44 | { 45 | var expression = new CronExpression("* * * LW * *"); 46 | var current = new DateTime(2019, 01, 31, 23, 59, 59); 47 | var expected = new DateTime(2019, 02, 28, 00, 00, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNearestWeekdayFromSaturday() 54 | { 55 | var expression = new CronExpression("* * * LW * *"); 56 | var current = new DateTime(2019, 08, 01, 08, 00, 00); 57 | var expected = new DateTime(2019, 08, 30, 00, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextMonthNearestWeekdayFromSaturday() 64 | { 65 | var expression = new CronExpression("* * * LW * *"); 66 | var current = new DateTime(2019, 08, 30, 23, 59, 59); 67 | var expected = new DateTime(2019, 09, 30, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNearestWeekdayFromSunday() 74 | { 75 | var expression = new CronExpression("* * * LW * *"); 76 | var current = new DateTime(2019, 03, 01, 08, 00, 00); 77 | var expected = new DateTime(2019, 03, 29, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextMonthNearestWeekdayFromSunday() 84 | { 85 | var expression = new CronExpression("* * * LW * *"); 86 | var current = new DateTime(2019, 03, 29, 23, 59, 59); 87 | var expected = new DateTime(2019, 04, 30, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/CronQuery.API/Cron/CronSyntax.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Cron; 26 | 27 | using System.Text.RegularExpressions; 28 | 29 | internal sealed class CronSyntax 30 | { 31 | const string Asterisk = @"^\*$"; // Matches: * 32 | const string Dash = @"^\d{1,2}-\d{1,2}$"; // Matches: 00-00 33 | const string Hash = @"^\d{1,2}#[1-5]$"; // Matches: 00#0 34 | const string LAndW = @"^(\d)?L$|L-\d{1,2}|^LW$|^\d{1,2}W$"; // Matches: L, 0L, L-00, LW or 00W 35 | const string Slash = @"^(\*|\d{1,2}(-\d{1,2})?)/\d{1,2}$"; // Matches: */00, 00/00 or 00-00/00 36 | const string ListValue = @"\d{1,2}|\d{1,2}-\d{1,2}|\d{1,2}(-\d{1,2})?/\d{1,2}"; // Matches: 00, 00-00, 00/00 or 00-00/00 37 | 38 | const int SecondPosition = 0; 39 | const int MinutePosition = 1; 40 | const int HourPosition = 2; 41 | const int DayPosition = 3; 42 | const int MonthPosition = 4; 43 | const int DayOfWeekPosition = 5; 44 | 45 | private readonly IEnumerable _expression; 46 | 47 | public CronSyntax(IEnumerable expression) 48 | { 49 | _expression = expression; 50 | } 51 | 52 | public bool IsValid() 53 | { 54 | if (_expression.Count() != 6) 55 | { 56 | return false; 57 | } 58 | 59 | if (!AllowedCharacters("*-,/", _expression.ElementAt(SecondPosition))) return false; 60 | if (!AllowedCharacters("*-,/", _expression.ElementAt(MinutePosition))) return false; 61 | if (!AllowedCharacters("*-,/", _expression.ElementAt(HourPosition))) return false; 62 | if (!AllowedCharacters("*-,/LW", _expression.ElementAt(DayPosition))) return false; 63 | if (!AllowedCharacters("*-,/", _expression.ElementAt(MonthPosition))) return false; 64 | if (!AllowedCharacters("*-,/L#", _expression.ElementAt(DayOfWeekPosition))) return false; 65 | 66 | return IsWellFormed(); 67 | } 68 | 69 | private bool AllowedCharacters(string allowedCharacters, string subExpression) 70 | { 71 | var characters = Regex.Replace(subExpression, @"\d", string.Empty); 72 | 73 | if (!characters.Any()) 74 | { 75 | return true; 76 | } 77 | 78 | return !characters.Where(character => !allowedCharacters.Contains(character)).Any(); 79 | } 80 | 81 | private bool IsWellFormed() 82 | { 83 | var list = $@"^{ListValue}(,({ListValue}))*$"; 84 | var pattern = $@"{Asterisk}|{Dash}|{Hash}|{Slash}|{LAndW}|{list}"; 85 | var regex = new Regex(pattern, RegexOptions.Compiled); 86 | 87 | return _expression.All(expression => regex.IsMatch(expression)); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithSlashTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithSlashTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextSecond() 34 | { 35 | var expression = new CronExpression("*/10 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 15, 20); 37 | var expected = new DateTime(2018, 12, 30, 08, 15, 30); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinuteAfterLastSecond() 44 | { 45 | var expression = new CronExpression("*/10 * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 15, 50); 47 | var expected = new DateTime(2018, 12, 30, 08, 16, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextHourAfterLastMinute() 54 | { 55 | var expression = new CronExpression("* */10 * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 50, 59); 57 | var expected = new DateTime(2018, 12, 30, 09, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextDayAfterLastHour() 64 | { 65 | var expression = new CronExpression("* * */10 * * *"); 66 | var current = new DateTime(2018, 12, 30, 20, 59, 59); 67 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextMonthAfterLastDay() 74 | { 75 | var expression = new CronExpression("* * * */7 * *"); 76 | var current = new DateTime(2018, 12, 29, 23, 59, 59); 77 | var expected = new DateTime(2019, 01, 01, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextYearAfterLastMonth() 84 | { 85 | var expression = new CronExpression("* * * * */6 *"); 86 | var current = new DateTime(2018, 07, 31, 23, 59, 59); 87 | var expected = new DateTime(2019, 01, 01, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextDayOfWeek() 94 | { 95 | var expression = new CronExpression("* * * * * */2"); 96 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 97 | var expected = new DateTime(2018, 12, 27, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithDigitTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithDigitTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextMinuteAfterLastSecond() 34 | { 35 | var expression = new CronExpression("30 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 30, 30); 37 | var expected = new DateTime(2018, 12, 30, 08, 31, 30); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextHourAfterLastMinute() 44 | { 45 | var expression = new CronExpression("* 30 * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 47 | var expected = new DateTime(2018, 12, 30, 09, 30, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextDayAfterLastHour() 54 | { 55 | var expression = new CronExpression("* * 8 * * *"); 56 | var current = new DateTime(2018, 12, 30, 23, 59, 59); 57 | var expected = new DateTime(2018, 12, 31, 08, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextMonthAfterLastDay() 64 | { 65 | var expression = new CronExpression("* * * 15 * *"); 66 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 67 | var expected = new DateTime(2019, 01, 15, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextMonthAfterLastDayInMonth() 74 | { 75 | var expression = new CronExpression("* * * 31 * *"); 76 | var current = new DateTime(2019, 01, 31, 23, 59, 59); 77 | var expected = new DateTime(2019, 03, 31, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextYearAfterLastMonth() 84 | { 85 | var expression = new CronExpression("* * * * 6 *"); 86 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 87 | var expected = new DateTime(2019, 06, 01, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextDayOfWeek() 94 | { 95 | var expression = new CronExpression("* * * * * 3"); 96 | var current = new DateTime(2018, 12, 28, 23, 59, 59); 97 | var expected = new DateTime(2019, 01, 02, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithWTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithWTest 31 | { 32 | [Fact] 33 | public void ShouldGetNearestWeekdayFromSaturday() 34 | { 35 | var expression = new CronExpression("* * * 15W * *"); 36 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 37 | var expected = new DateTime(2018, 12, 14, 00, 00, 00); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMonthNearestWeekdayFromSaturday() 44 | { 45 | var expression = new CronExpression("* * * 15W * *"); 46 | var current = new DateTime(2018, 12, 17, 23, 59, 59); 47 | var expected = new DateTime(2019, 01, 15, 00, 00, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNearestWeekdayFromSaturdayFirstDayOfMonth() 54 | { 55 | var expression = new CronExpression("* * * 1W * *"); 56 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 57 | var expected = new DateTime(2018, 12, 03, 00, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextMonthNearestWeekdayFromSaturdayFirstDayOfMonth() 64 | { 65 | var expression = new CronExpression("* * * 1W * *"); 66 | var current = new DateTime(2019, 03, 30, 23, 59, 59); 67 | var expected = new DateTime(2019, 04, 01, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNearestWeekdayFromSunday() 74 | { 75 | var expression = new CronExpression("* * * 9W * *"); 76 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 77 | var expected = new DateTime(2018, 12, 10, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextMonthNearestWeekdayFromSunday() 84 | { 85 | var expression = new CronExpression("* * * 9W * *"); 86 | var current = new DateTime(2018, 12, 10, 23, 59, 59); 87 | var expected = new DateTime(2019, 01, 09, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNearestWeekdayFromSundayLastDayOfMonth() 94 | { 95 | var expression = new CronExpression("* * * 31W * *"); 96 | var current = new DateTime(2019, 03, 01, 23, 59, 59); 97 | var expected = new DateTime(2019, 03, 29, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void ShouldGetNextMonthNearestWeekdayFromSundayLastDayOfMonth() 104 | { 105 | var expression = new CronExpression("* * * 31W * *"); 106 | var current = new DateTime(2019, 03, 31, 23, 59, 59); 107 | var expected = new DateTime(2019, 05, 31, 00, 00, 00); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/CronQuery.API/Cron/CronValue.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Cron; 26 | 27 | using CronQuery.Extensions; 28 | using System.Text.RegularExpressions; 29 | 30 | internal sealed class CronValue 31 | { 32 | const int NoValue = -1; 33 | 34 | private readonly TimeUnit _timeUnit; 35 | 36 | public CronValue(string expression, TimeUnit timeUnit) 37 | { 38 | _timeUnit = timeUnit; 39 | 40 | IsAsterisk = expression == "*"; 41 | IsL = expression == "L"; 42 | HasAsterisk = expression.Contains("*"); 43 | HasDash = expression.Contains("-"); 44 | HasSlash = expression.Contains("/"); 45 | HasHash = expression.Contains("#"); 46 | HasL = expression.Contains("L"); 47 | HasW = expression.Contains("W"); 48 | 49 | var symbol = HasSlash ? '/' : HasDash ? '-' : HasHash ? '#' : '\0'; 50 | var parameters = expression.Split(symbol); 51 | var nestedParameters = HasSlash && HasDash ? parameters.First().Split('-') : null!; 52 | 53 | var start = 54 | IsAsterisk ? timeUnit.MinValue : // Expression is * 55 | IsL ? NoValue : // Expression is L 56 | HasL && HasDash ? ToInt32(parameters.Last()) : // Expression is L-00 57 | HasL && HasW ? NoValue : // Expression is LW 58 | HasHash ? ToInt32(parameters.First()) : // Expression is 00#0 59 | HasSlash && HasAsterisk ? timeUnit.MinValue : // Expression is */00 60 | HasSlash && HasDash ? ToInt32(nestedParameters.First()) : // Expression is 00-00/00 61 | HasSlash || HasDash || HasW ? ToInt32(parameters.First()) : // Expression is 00/00 or 00-00 or 00W 62 | Math.Max(ToInt32(expression), timeUnit.MinValue); // Expression is 00 63 | 64 | var end = 65 | IsAsterisk ? timeUnit.MaxValue : // Expression is * 66 | IsL ? NoValue : // Expression is L 67 | HasL && HasDash ? ToInt32(parameters.Last()) : // Expression is L-00 68 | HasL && HasW ? NoValue : // Expression is LW 69 | HasHash ? start : // Expression is 00#0 70 | HasSlash && HasAsterisk ? timeUnit.MaxValue : // Expression is */00 71 | HasSlash && HasDash ? ToInt32(nestedParameters.Last()) : // Expression is 00-00/00 72 | HasSlash ? timeUnit.MaxValue : // Expression is 00/00 73 | HasW ? ToInt32(parameters.First()) : // Expression is 00W 74 | HasDash ? ToInt32(parameters.Last()) : // Expression is 00-00 75 | Math.Min(ToInt32(expression), timeUnit.MaxValue); // Expression is 00 76 | 77 | Nth = HasHash ? ToInt32(parameters.Last()) : 0; 78 | Step = HasSlash ? ToInt32(parameters.Last()) : 1; 79 | Values = start.To(end).Step(Step) 80 | .Where(value => value >= _timeUnit.MinValue) 81 | .Where(value => value <= _timeUnit.MaxValue) 82 | .ToList(); 83 | } 84 | 85 | public IEnumerable Values { get; } 86 | public int Nth { get; } 87 | public int Step { get; } 88 | public bool IsAsterisk { get; } 89 | public bool IsL { get; } 90 | public bool HasAsterisk { get; } 91 | public bool HasL { get; } 92 | public bool HasW { get; } 93 | public bool HasDash { get; } 94 | public bool HasSlash { get; } 95 | public bool HasHash { get; } 96 | 97 | private int ToInt32(string value) 98 | { 99 | var digits = Regex.Replace(value, @"[^\d]+", string.Empty); 100 | return Convert.ToInt32(digits); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/CronQuery.API/Cron/CronExpression.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Cron; 26 | 27 | using CronQuery.Extensions; 28 | 29 | public sealed class CronExpression 30 | { 31 | private readonly CronField _second = null!; 32 | private readonly CronField _minute = null!; 33 | private readonly CronField _hour = null!; 34 | private readonly CronField _day = null!; 35 | private readonly CronField _month = null!; 36 | private readonly CronField _dayOfWeek = null!; 37 | 38 | public CronExpression(string expression) 39 | { 40 | var subExpressions = (expression ?? string.Empty).ToUpper().Split(' '); 41 | var syntax = new CronSyntax(subExpressions); 42 | 43 | IsValid = syntax.IsValid(); 44 | 45 | if (IsValid) 46 | { 47 | _second = new CronField(subExpressions[0], TimeUnit.Second); 48 | _minute = new CronField(subExpressions[1], TimeUnit.Minute); 49 | _hour = new CronField(subExpressions[2], TimeUnit.Hour); 50 | _day = new CronField(subExpressions[3], TimeUnit.Day); 51 | _month = new CronField(subExpressions[4], TimeUnit.Month); 52 | _dayOfWeek = new CronField(subExpressions[5], TimeUnit.DayOfWeek); 53 | } 54 | } 55 | 56 | public bool IsValid { get; } 57 | 58 | public DateTime Next(DateTime query) 59 | { 60 | if (!IsValid || IsUnreachableCondition()) 61 | { 62 | return DateTime.MinValue; 63 | } 64 | 65 | var time = _second.Next(query); 66 | 67 | if (time <= query || !_minute.Matches(time)) 68 | { 69 | time = _second.Reset(time); 70 | time = _minute.Next(time); 71 | } 72 | 73 | if (time <= query || !_hour.Matches(time)) 74 | { 75 | time = _second.Reset(time); 76 | time = _minute.Reset(time); 77 | time = _hour.Next(time); 78 | } 79 | 80 | while (time <= query || !_day.Matches(time) || !_month.Matches(time) || !_dayOfWeek.Matches(time)) 81 | { 82 | time = _second.Reset(time); 83 | time = _minute.Reset(time); 84 | time = _hour.Reset(time); 85 | 86 | time = _day.Next(time); 87 | 88 | if (!_dayOfWeek.Matches(time)) 89 | { 90 | time = _dayOfWeek.Next(time); 91 | } 92 | 93 | if (time <= query || !_month.Matches(time)) 94 | { 95 | time = _day.Reset(time); 96 | time = _month.Next(time); 97 | } 98 | 99 | if (time <= query) 100 | { 101 | time = _month.Reset(time); 102 | time = time.AddYears(1); 103 | } 104 | } 105 | 106 | return time; 107 | } 108 | 109 | private bool IsUnreachableCondition() 110 | { 111 | // E.g. * * * 15W * 0 112 | 113 | var hasW = _day.Values.Any(cron => cron.HasW); 114 | 115 | var onlyWeekend = !_dayOfWeek.Values 116 | .SelectMany(cron => cron.Values) 117 | .Any(value => 1.To(5).Contains(value)); 118 | 119 | if (hasW && onlyWeekend) return true; 120 | 121 | // E.g. * * * 31 2,4,6 * 122 | 123 | var days = _day.Values.SelectMany(cron => cron.Values); 124 | var months = _month.Values.SelectMany(cron => cron.Values); 125 | 126 | var monthsNotReachGivenDays = days.Any() && !days.Any(day => 127 | months.Any(month => DateTime.DaysInMonth(1970, month) >= day)); 128 | 129 | return monthsNotReachGivenDays; 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/CronQuery.API/Extensions/DateTimeExtensions.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Extensions; 26 | 27 | using CronQuery.Cron; 28 | 29 | internal static class DateTimeExtensions 30 | { 31 | public static DateTime Last(this DateTime dateTime, int daysBefore = 0) 32 | { 33 | var daysInMonth = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); 34 | var day = daysInMonth > daysBefore ? daysInMonth - daysBefore : 1; 35 | 36 | return dateTime.Set(day: day); 37 | } 38 | 39 | public static DateTime NearestWeekday(this DateTime dateTime) 40 | { 41 | if (dateTime.DayOfWeek == DayOfWeek.Saturday) 42 | { 43 | return dateTime.Day == 1 ? 44 | dateTime.Next(DayOfWeek.Monday) : 45 | dateTime.Previous(DayOfWeek.Friday); 46 | } 47 | 48 | if (dateTime.DayOfWeek == DayOfWeek.Sunday) 49 | { 50 | var lastDay = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); 51 | 52 | return dateTime.Day == lastDay ? 53 | dateTime.Previous(DayOfWeek.Friday) : 54 | dateTime.Next(DayOfWeek.Monday); 55 | } 56 | 57 | return dateTime; 58 | } 59 | 60 | public static DateTime Next(this DateTime dateTime, DayOfWeek dayOfWeek) 61 | { 62 | var diff = dayOfWeek - dateTime.DayOfWeek; 63 | 64 | if (diff <= 0) 65 | { 66 | diff += 7; 67 | } 68 | 69 | return dateTime.AddDays(diff); 70 | } 71 | 72 | public static DateTime Next(this DateTime dateTime, DayOfWeek dayOfWeek, int occurrence) 73 | { 74 | var firstDate = dateTime.Set(day: 1); 75 | 76 | dateTime = firstDate.AddDays((7 - ((int)firstDate.DayOfWeek - (int)dayOfWeek)) % 7); 77 | dateTime = dateTime.AddDays(7 * (occurrence - 1)); 78 | 79 | return dateTime; 80 | } 81 | 82 | public static DateTime Previous(this DateTime dateTime, DayOfWeek dayOfWeek) 83 | { 84 | while (dateTime.DayOfWeek != dayOfWeek) 85 | { 86 | dateTime = dateTime.AddDays(-1); 87 | } 88 | 89 | return dateTime; 90 | } 91 | 92 | public static int Get(this DateTime dateTime, TimeUnit timeUnit) 93 | { 94 | if (timeUnit.IsDayOfWeek) return (int)dateTime.DayOfWeek; 95 | if (timeUnit.IsMonth) return dateTime.Month; 96 | if (timeUnit.IsDay) return dateTime.Day; 97 | if (timeUnit.IsHour) return dateTime.Hour; 98 | if (timeUnit.IsMinute) return dateTime.Minute; 99 | 100 | return dateTime.Second; 101 | } 102 | 103 | public static DateTime Set(this DateTime dateTime, TimeUnit timeUnit, int value) 104 | { 105 | if (timeUnit.IsMonth) return dateTime.Set(month: value); 106 | if (timeUnit.IsDay) return dateTime.Set(day: value); 107 | if (timeUnit.IsHour) return dateTime.Set(hour: value); 108 | if (timeUnit.IsMinute) return dateTime.Set(minute: value); 109 | 110 | return dateTime.Set(second: value); 111 | } 112 | 113 | public static DateTime Set(this DateTime dateTime, 114 | int? year = null, int? month = null, int? day = null, 115 | int? hour = null, int? minute = null, int? second = null) 116 | { 117 | return new DateTime( 118 | year ?? dateTime.Year, 119 | month ?? dateTime.Month, 120 | day ?? dateTime.Day, 121 | hour ?? dateTime.Hour, 122 | minute ?? dateTime.Minute, 123 | second ?? dateTime.Second, 124 | dateTime.Millisecond, 125 | dateTime.Kind); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /docs/www/index.hbs: -------------------------------------------------------------------------------- 1 | {{#> www/page }} 2 | 3 | {{#*inline "body-block"}} 4 | 5 |

6 | CronQuery is an open source 7 | and lightweight job runner for ASP.NET Core. 8 |

9 | 10 |

11 | # Installation 12 |

13 | 14 |

15 | CronQuery is installed via NuGet package. 16 |

17 | 18 | 30 | 31 |
32 |
33 |
dotnet add package CronQuery
34 |
35 |
36 |
Install-Package CronQuery
37 |
38 |
39 | 40 |

41 | # Creating jobs 42 |

43 | 44 |

45 | Jobs are created by implementing the interface IJob. 46 |

47 | 48 |
{{ include 'www/snippets/CreatingJob.cs' }}
49 | 50 |

51 | # Registering jobs 52 |

53 | 54 |

55 | Jobs are registered in the Program.cs class. 56 |

57 | 58 |
{{ include 'www/snippets/RegisteringJob.cs' }}
59 | 60 |
61 | Jobs are registered using the ASP.NET Core dependency injection. 62 | This means that is possible to use dependency injection in your jobs. 63 |
64 | 65 |

66 | # Setting up jobs 67 |

68 | 69 |

70 | Schedule your jobs using CRON expressions of six fields to a 71 | specific timezone (UTC is default). Save the configuration in your 72 | appsettings.json like the example below: 73 |

74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 |
JobConfiguration
MyFirstJobRuns every second on every day, except Sunday.
MySecondJobRuns every day at 2:00 A.M.
MyThirdJobRuns every second between 2:00 P.M. and 6:59 P.M. only on Saturday every 15 days.
95 | 96 |
{{ include 'www/snippets/SettingUpJob.json' }}
97 | 98 |

99 | Whenever you save the appsettings.json CronQuery immediately assumes the new configuration. 100 |

101 | 102 |
103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 115 | 118 | 119 | 120 | 123 | 126 | 127 | 128 | 131 | 134 | 135 | 136 | 139 | 142 | 143 | 144 | 147 | 150 | 151 | 152 |
PropertyDescription
113 | CronQuery.Running 114 | 116 | Turn on (true) or turn off (false) the CronQuery runner. 117 |
121 | CronQuery.TimeZone 122 | 124 | System time zone ID or a custom UTC offset, e.g. UTC-03:00, UTC+01:00. 125 |
129 | CronQuery.Jobs[].Name 130 | 132 | Job class name. 133 |
137 | CronQuery.Jobs[].Running 138 | 140 | Turn on (true) or turn off (false) the job. 141 |
145 | CronQuery.Jobs[].Cron 146 | 148 | CRON expression that triggers the job. 149 |
153 |
154 | 155 |
156 | You can see a full example in the 157 | GitHub repo. 158 |
159 | 160 | {{/inline}} 161 | 162 | {{/www/page}} 163 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithDashAndSlashTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithDashAndSlashTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextSecond() 34 | { 35 | var expression = new CronExpression("20-50/10 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 15, 30); 37 | var expected = new DateTime(2018, 12, 30, 08, 15, 40); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinuteAfterLastSecond() 44 | { 45 | var expression = new CronExpression("20-50/10 * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 15, 50); 47 | var expected = new DateTime(2018, 12, 30, 08, 16, 20); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextMinute() 54 | { 55 | var expression = new CronExpression("* 20-50/10 * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 57 | var expected = new DateTime(2018, 12, 30, 08, 40, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextHourAfterLastMinute() 64 | { 65 | var expression = new CronExpression("* 20-50/10 * * * *"); 66 | var current = new DateTime(2018, 12, 30, 08, 50, 59); 67 | var expected = new DateTime(2018, 12, 30, 09, 20, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextHour() 74 | { 75 | var expression = new CronExpression("* * 8-18/2 * * *"); 76 | var current = new DateTime(2018, 12, 30, 08, 59, 59); 77 | var expected = new DateTime(2018, 12, 30, 10, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextDayAfterLastHour() 84 | { 85 | var expression = new CronExpression("* * 8-18/2 * * *"); 86 | var current = new DateTime(2018, 12, 30, 18, 59, 59); 87 | var expected = new DateTime(2018, 12, 31, 08, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextDay() 94 | { 95 | var expression = new CronExpression("* * * 5-25/5 * *"); 96 | var current = new DateTime(2018, 12, 10, 23, 59, 59); 97 | var expected = new DateTime(2018, 12, 15, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void ShouldGetNextMonthAfterLastDay() 104 | { 105 | var expression = new CronExpression("* * * 5-25/5 * *"); 106 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 107 | var expected = new DateTime(2019, 01, 05, 00, 00, 00); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | 112 | [Fact] 113 | public void ShouldGetNextMonth() 114 | { 115 | var expression = new CronExpression("* * * * 2-12/2 *"); 116 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 117 | var expected = new DateTime(2019, 02, 01, 00, 00, 00); 118 | 119 | Assert.Equal(expected, expression.Next(current)); 120 | } 121 | 122 | [Fact] 123 | public void ShouldGetNextDayOfWeek() 124 | { 125 | var expression = new CronExpression("* * * * * 1-5/2"); 126 | var current = new DateTime(2018, 12, 28, 23, 59, 59); 127 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 128 | 129 | Assert.Equal(expected, expression.Next(current)); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithDashAndSlashAndCommaTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithDashAndSlashAndCommaTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextSecond() 34 | { 35 | var expression = new CronExpression("5,20-50/10 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 15, 30); 37 | var expected = new DateTime(2018, 12, 30, 08, 15, 40); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinuteAfterLastSecond() 44 | { 45 | var expression = new CronExpression("5,20-50/10 * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 15, 50); 47 | var expected = new DateTime(2018, 12, 30, 08, 16, 05); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextMinute() 54 | { 55 | var expression = new CronExpression("* 5,20-50/10 * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 57 | var expected = new DateTime(2018, 12, 30, 08, 40, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextHourAfterLastMinute() 64 | { 65 | var expression = new CronExpression("* 5,20-50/10 * * * *"); 66 | var current = new DateTime(2018, 12, 30, 08, 50, 59); 67 | var expected = new DateTime(2018, 12, 30, 09, 05, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextHour() 74 | { 75 | var expression = new CronExpression("* * 5,8-18/2 * * *"); 76 | var current = new DateTime(2018, 12, 30, 08, 59, 59); 77 | var expected = new DateTime(2018, 12, 30, 10, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextDayAfterLastHour() 84 | { 85 | var expression = new CronExpression("* * 5,8-18/2 * * *"); 86 | var current = new DateTime(2018, 12, 30, 18, 59, 59); 87 | var expected = new DateTime(2018, 12, 31, 05, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextDay() 94 | { 95 | var expression = new CronExpression("* * * 1,5-25/5 * *"); 96 | var current = new DateTime(2018, 12, 10, 23, 59, 59); 97 | var expected = new DateTime(2018, 12, 15, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void ShouldGetNextMonthAfterLastDay() 104 | { 105 | var expression = new CronExpression("* * * 1,5-25/5 * *"); 106 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 107 | var expected = new DateTime(2019, 01, 01, 00, 00, 00); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | 112 | [Fact] 113 | public void ShouldGetNextMonth() 114 | { 115 | var expression = new CronExpression("* * * * 1,2-12/2 *"); 116 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 117 | var expected = new DateTime(2019, 01, 01, 00, 00, 00); 118 | 119 | Assert.Equal(expected, expression.Next(current)); 120 | } 121 | 122 | [Fact] 123 | public void ShouldGetNextDayOfWeek() 124 | { 125 | var expression = new CronExpression("* * * * * 0,1-5/2"); 126 | var current = new DateTime(2018, 12, 28, 23, 59, 59); 127 | var expected = new DateTime(2018, 12, 30, 00, 00, 00); 128 | 129 | Assert.Equal(expected, expression.Next(current)); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/CronQuery.API/Mvc/Jobs/JobRunner.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Mvc.Jobs; 26 | 27 | using CronQuery.Cron; 28 | using CronQuery.Mvc.Options; 29 | using Microsoft.Extensions.DependencyInjection; 30 | using Microsoft.Extensions.Hosting; 31 | using Microsoft.Extensions.Logging; 32 | using Microsoft.Extensions.Options; 33 | using System.Reactive.Linq; 34 | 35 | public sealed class JobRunner : IHostedService 36 | { 37 | private readonly ICollection _timers; 38 | private readonly IServiceProvider _serviceProvider; 39 | private readonly ILoggerFactory _loggerFactory; 40 | private readonly JobCollection _jobs; 41 | 42 | private JobRunnerOptions _options; 43 | 44 | public JobRunner(IOptionsMonitor options, JobCollection jobs, 45 | IServiceProvider serviceProvider, ILoggerFactory loggerFactory) 46 | { 47 | _timers = new List(); 48 | _options = options?.CurrentValue ?? throw new ArgumentNullException(nameof(options)); 49 | _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); 50 | _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); 51 | _jobs = jobs ?? throw new ArgumentNullException(nameof(jobs)); 52 | 53 | options.OnChange(Restart); 54 | } 55 | 56 | public async Task RunAsync(string jobName) 57 | { 58 | var service = _jobs.SingleOrDefault(service => service.ServiceType.Name == jobName); 59 | 60 | if (service == null) 61 | { 62 | _loggerFactory.CreateLogger(GetType().FullName!) 63 | .LogWarning($"Job {jobName} is not in the queue."); 64 | 65 | return; 66 | } 67 | 68 | using (var scope = _serviceProvider.CreateScope()) 69 | { 70 | var jobInstance = ((IJob)scope.ServiceProvider.GetRequiredService(service.ServiceType)); 71 | 72 | try 73 | { 74 | await jobInstance.RunAsync(); 75 | } 76 | catch (Exception error) 77 | { 78 | _loggerFactory.CreateLogger(GetType().FullName!) 79 | .LogError(error, $"Job '{service.ServiceType.Name}' failed during running."); 80 | } 81 | finally 82 | { 83 | if (jobInstance is IDisposable disposable) 84 | { 85 | disposable.Dispose(); 86 | } 87 | } 88 | } 89 | } 90 | 91 | public void Start() 92 | { 93 | if (!_options.Running) return; 94 | 95 | var timeZone = new TimeZoneOptions(_options.TimeZone).ToTimeZoneInfo(); 96 | 97 | foreach (var job in _options.Jobs) 98 | { 99 | if (!job.Running) 100 | { 101 | continue; 102 | } 103 | 104 | var cron = new CronExpression(job.Cron); 105 | 106 | if (!cron.IsValid) 107 | { 108 | _loggerFactory.CreateLogger(GetType().FullName!) 109 | .LogWarning($"Invalid cron expression for '{job.Name}'."); 110 | 111 | continue; 112 | } 113 | 114 | var timer = new JobInterval(cron, timeZone, async () => await RunAsync(job.Name)); 115 | 116 | _timers.Add(timer); 117 | 118 | timer.Run(); 119 | } 120 | } 121 | 122 | public void Stop() 123 | { 124 | foreach (var timer in _timers) 125 | { 126 | timer.Dispose(); 127 | } 128 | 129 | _timers.Clear(); 130 | } 131 | 132 | private void Restart(JobRunnerOptions options) 133 | { 134 | _options = options; 135 | 136 | Stop(); 137 | Start(); 138 | } 139 | 140 | Task IHostedService.StartAsync(CancellationToken cancellationToken) 141 | { 142 | Start(); 143 | 144 | return Task.CompletedTask; 145 | } 146 | 147 | Task IHostedService.StopAsync(CancellationToken cancellationToken) 148 | { 149 | Stop(); 150 | 151 | return Task.CompletedTask; 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/CronQuery.API/Cron/CronField.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Cron; 26 | 27 | using CronQuery.Extensions; 28 | 29 | internal sealed class CronField 30 | { 31 | private readonly TimeUnit _timeUnit; 32 | 33 | public CronField(string expression, TimeUnit timeUnit) 34 | { 35 | _timeUnit = timeUnit; 36 | 37 | Values = expression.Split(',') 38 | .Select(value => new CronValue(value, timeUnit)) 39 | .ToList(); 40 | } 41 | 42 | public IEnumerable Values { get; } 43 | 44 | public bool Matches(DateTime dateTime) 45 | { 46 | if (Values.Any(value => value.HasW)) 47 | { 48 | return dateTime == NearestWeekday(dateTime); 49 | } 50 | 51 | if (Values.Any(value => value.HasL)) 52 | { 53 | return dateTime == LastDayInMonth(dateTime); 54 | } 55 | 56 | if (Values.Any(value => value.HasHash)) 57 | { 58 | return dateTime == DayOfWeekOccurence(dateTime); 59 | } 60 | 61 | return Values.SelectMany(cron => cron.Values).Contains(dateTime.Get(_timeUnit)); 62 | } 63 | 64 | public DateTime Next(DateTime dateTime) 65 | { 66 | if (Values.Any(value => value.HasW)) 67 | { 68 | return NearestWeekday(dateTime); 69 | } 70 | 71 | if (Values.Any(value => value.HasL)) 72 | { 73 | return LastDayInMonth(dateTime); 74 | } 75 | 76 | if (Values.Any(value => value.HasHash)) 77 | { 78 | return DayOfWeekOccurence(dateTime); 79 | } 80 | 81 | return ListValue(dateTime); 82 | } 83 | 84 | public DateTime Reset(DateTime dateTime) 85 | { 86 | var values = Values.SelectMany(cron => cron.Values); 87 | 88 | if (!values.Any()) 89 | { 90 | return dateTime; 91 | } 92 | 93 | return dateTime.Set(_timeUnit, values.Min()); 94 | } 95 | 96 | private DateTime ListValue(DateTime dateTime) 97 | { 98 | var values = Values.SelectMany(cron => cron.Values); 99 | 100 | var next = values 101 | .Where(value => value > dateTime.Get(_timeUnit)) 102 | .Select(value => (int?)value) 103 | .FirstOrDefault(); 104 | 105 | next = next ?? values.Min(); 106 | 107 | if (_timeUnit.IsDay && next.Value > DateTime.DaysInMonth(dateTime.Year, dateTime.Month)) 108 | { 109 | dateTime = dateTime.AddMonths(1); 110 | next = values.Min(); 111 | } 112 | 113 | if (_timeUnit.IsMonth) 114 | { 115 | var daysInMonth = DateTime.DaysInMonth(dateTime.Year, next.Value); 116 | 117 | if (dateTime.Day > daysInMonth) 118 | { 119 | dateTime = dateTime.Set(day: daysInMonth); 120 | } 121 | } 122 | 123 | return dateTime.Set(_timeUnit, next.Value); 124 | } 125 | 126 | private DateTime LastDayInMonth(DateTime dateTime) 127 | { 128 | var cron = Values.Single(); 129 | 130 | if (_timeUnit.IsDay) 131 | { 132 | if (cron.IsL) // Expression is L 133 | { 134 | return dateTime.Last(); 135 | } 136 | 137 | return dateTime.Last(daysBefore: cron.Values.Single()); // Expression is L-[1-31] 138 | } 139 | 140 | return dateTime.Last().Previous((DayOfWeek)cron.Values.Single()); // Expression is L or [0-6]L 141 | } 142 | 143 | private DateTime NearestWeekday(DateTime dateTime) 144 | { 145 | var cron = Values.Single(); 146 | 147 | var day = cron.HasL ? 148 | DateTime.DaysInMonth(dateTime.Year, dateTime.Month) : // Expression is LW 149 | cron.Values.Single(); // Expression is [1-31]W 150 | 151 | if (day > DateTime.DaysInMonth(dateTime.Year, dateTime.Month)) 152 | { 153 | dateTime = dateTime.AddMonths(1); 154 | } 155 | 156 | return dateTime.Set(day: day).NearestWeekday(); 157 | } 158 | 159 | private DateTime DayOfWeekOccurence(DateTime dateTime) 160 | { 161 | var cron = Values.Single(); 162 | var dayOfWeek = (DayOfWeek)cron.Values.Single(); 163 | 164 | return dateTime.Next(dayOfWeek, cron.Nth); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ComplexExpressionTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ComplexExpressionTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextExactTimeInList() 34 | { 35 | var expression = new CronExpression("0 0 8 10,20,30 2-12/2 *"); 36 | var current = new DateTime(2019, 02, 20, 23, 59, 59); 37 | var expected = new DateTime(2019, 04, 10, 08, 00, 00); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextExactTimeOnTheLastDay() 44 | { 45 | var expression = new CronExpression("0 30 14 L * *"); 46 | var current = new DateTime(2019, 01, 31, 15, 26, 34); 47 | var expected = new DateTime(2019, 02, 28, 14, 30, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextExactTimeOnTheLastDayAndWeekday() 54 | { 55 | var expression = new CronExpression("0 0 8 LW * *"); 56 | var current = new DateTime(2019, 02, 20, 23, 59, 59); 57 | var expected = new DateTime(2019, 02, 28, 08, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void At12PmEveryDay() 64 | { 65 | var expression = new CronExpression("0 0 12 * * *"); 66 | var current = new DateTime(2018, 12, 31, 10, 36, 35); 67 | var expected = new DateTime(2018, 12, 31, 12, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void Every5MinutesFrom1PmAndFrom6Pm() 74 | { 75 | var expression = new CronExpression("0 0/5 13,18 * * *"); 76 | var current = new DateTime(2018, 02, 08, 02, 46, 03); 77 | var expected = new DateTime(2018, 02, 08, 13, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void At115PmAnd145PmEveryTuesdayInJune() 84 | { 85 | var expression = new CronExpression("0 15,45 13 * 6 2"); 86 | var current = new DateTime(2018, 12, 24, 17, 03, 53); 87 | var expected = new DateTime(2019, 06, 04, 13, 15, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void At930EveryDayExceptSunday() 94 | { 95 | var expression = new CronExpression("0 30 9 * * 1-5"); 96 | var current = new DateTime(2018, 09, 21, 20, 26, 07); 97 | var expected = new DateTime(2018, 09, 24, 09, 30, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void At930On15ThEveryMonth() 104 | { 105 | var expression = new CronExpression("0 30 9 15 * *"); 106 | var current = new DateTime(2018, 10, 07, 01, 43, 36); 107 | var expected = new DateTime(2018, 10, 15, 09, 30, 00); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | 112 | [Fact] 113 | public void At6PmOnTheLastDayEveryMonth() 114 | { 115 | var expression = new CronExpression("0 0 18 L * *"); 116 | var current = new DateTime(2018, 11, 12, 11, 19, 15); 117 | var expected = new DateTime(2018, 11, 30, 18, 00, 00); 118 | 119 | Assert.Equal(expected, expression.Next(current)); 120 | } 121 | 122 | [Fact] 123 | public void At6PmOnThe3ThLastDayEveryMonth() 124 | { 125 | var expression = new CronExpression("0 0 18 L-3 * *"); 126 | var current = new DateTime(2018, 07, 25, 00, 09, 59); 127 | var expected = new DateTime(2018, 07, 28, 18, 00, 00); 128 | 129 | Assert.Equal(expected, expression.Next(current)); 130 | } 131 | 132 | [Fact] 133 | public void At1030OnTheLastFridayEveryMonth() 134 | { 135 | var expression = new CronExpression("0 30 10 * * 5L"); 136 | var current = new DateTime(2018, 12, 31, 02, 34, 05); 137 | var expected = new DateTime(2019, 01, 25, 10, 30, 00); 138 | 139 | Assert.Equal(expected, expression.Next(current)); 140 | } 141 | 142 | [Fact] 143 | public void At10AmOnTheThirdMondayEveryMonth() 144 | { 145 | var expression = new CronExpression("0 0 10 * * 1#3"); 146 | var current = new DateTime(2018, 12, 04, 20, 56, 31); 147 | var expected = new DateTime(2018, 12, 17, 10, 00, 00); 148 | 149 | Assert.Equal(expected, expression.Next(current)); 150 | } 151 | 152 | [Fact] 153 | public void At12AmEvery5DaysStartingOn10Th() 154 | { 155 | var expression = new CronExpression("0 0 0 10/5 * *"); 156 | var current = new DateTime(2019, 01, 13, 05, 17, 15); 157 | var expected = new DateTime(2019, 01, 15, 00, 00, 00); 158 | 159 | Assert.Equal(expected, expression.Next(current)); 160 | } 161 | 162 | [Fact] 163 | public void Every5MinutesSpecificTimeAndDays() 164 | { 165 | var expression = new CronExpression("0 0/5 14,18,3-39,52 * 1,3,9 1-5"); 166 | var current = new DateTime(2019, 05, 21, 08, 12, 39); 167 | var expected = new DateTime(2019, 09, 02, 03, 00, 00); 168 | 169 | Assert.Equal(expected, expression.Next(current)); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithDashTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithDashTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextSecond() 34 | { 35 | var expression = new CronExpression("10-30 * * * * *"); 36 | var current = new DateTime(2018, 12, 30, 08, 30, 15); 37 | var expected = new DateTime(2018, 12, 30, 08, 30, 16); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextMinuteAfterLastSecond() 44 | { 45 | var expression = new CronExpression("10-30 * * * * *"); 46 | var current = new DateTime(2018, 12, 30, 08, 30, 30); 47 | var expected = new DateTime(2018, 12, 30, 08, 31, 10); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextHourAfterLastMinute() 54 | { 55 | var expression = new CronExpression("* 10-30 * * * *"); 56 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 57 | var expected = new DateTime(2018, 12, 30, 09, 10, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextDayAfterLastHour() 64 | { 65 | var expression = new CronExpression("* * 10-20 * * *"); 66 | var current = new DateTime(2018, 12, 30, 20, 59, 59); 67 | var expected = new DateTime(2018, 12, 31, 10, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextMonthAfterLastDay() 74 | { 75 | var expression = new CronExpression("* * * 10-20 * *"); 76 | var current = new DateTime(2018, 12, 20, 23, 59, 59); 77 | var expected = new DateTime(2019, 01, 10, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextYearAfterLastMonth() 84 | { 85 | var expression = new CronExpression("* * * * 6-12 *"); 86 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 87 | var expected = new DateTime(2019, 06, 01, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextDayOfWeek() 94 | { 95 | var expression = new CronExpression("* * * * * 1-5"); 96 | var current = new DateTime(2018, 12, 28, 23, 59, 59); 97 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void ShouldGetNextSecondCombinedWithComma() 104 | { 105 | var expression = new CronExpression("5,10-30 * * * * *"); 106 | var current = new DateTime(2018, 12, 30, 08, 30, 15); 107 | var expected = new DateTime(2018, 12, 30, 08, 30, 16); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | 112 | [Fact] 113 | public void ShouldGetNextMinuteAfterLastSecondCombinedWithComma() 114 | { 115 | var expression = new CronExpression("5,10-30 * * * * *"); 116 | var current = new DateTime(2018, 12, 30, 08, 30, 30); 117 | var expected = new DateTime(2018, 12, 30, 08, 31, 05); 118 | 119 | Assert.Equal(expected, expression.Next(current)); 120 | } 121 | 122 | [Fact] 123 | public void ShouldGetNextHourAfterLastMinuteCombinedWithComma() 124 | { 125 | var expression = new CronExpression("* 5,10-30 * * * *"); 126 | var current = new DateTime(2018, 12, 30, 08, 30, 59); 127 | var expected = new DateTime(2018, 12, 30, 09, 05, 00); 128 | 129 | Assert.Equal(expected, expression.Next(current)); 130 | } 131 | 132 | [Fact] 133 | public void ShouldGetNextDayAfterLastHourCombinedWithComma() 134 | { 135 | var expression = new CronExpression("* * 5,10-20 * * *"); 136 | var current = new DateTime(2018, 12, 30, 20, 59, 59); 137 | var expected = new DateTime(2018, 12, 31, 05, 00, 00); 138 | 139 | Assert.Equal(expected, expression.Next(current)); 140 | } 141 | 142 | [Fact] 143 | public void ShouldGetNextMonthAfterLastDayCombinedWithComma() 144 | { 145 | var expression = new CronExpression("* * * 5,10-20 * *"); 146 | var current = new DateTime(2018, 12, 20, 23, 59, 59); 147 | var expected = new DateTime(2019, 01, 05, 00, 00, 00); 148 | 149 | Assert.Equal(expected, expression.Next(current)); 150 | } 151 | 152 | [Fact] 153 | public void ShouldGetNextYearAfterLastMonthCombinedWithComma() 154 | { 155 | var expression = new CronExpression("* * * * 3,6-12 *"); 156 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 157 | var expected = new DateTime(2019, 03, 01, 00, 00, 00); 158 | 159 | Assert.Equal(expected, expression.Next(current)); 160 | } 161 | 162 | [Fact] 163 | public void ShouldGetNextDayOfWeekCombinedWithComma() 164 | { 165 | var expression = new CronExpression("* * * * * 0,1-5"); 166 | var current = new DateTime(2018, 12, 28, 23, 59, 59); 167 | var expected = new DateTime(2018, 12, 30, 00, 00, 00); 168 | 169 | Assert.Equal(expected, expression.Next(current)); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /docs/www/cron.hbs: -------------------------------------------------------------------------------- 1 | {{#> www/page }} 2 | 3 | {{#*inline "body-block"}} 4 | 5 |

6 | CronQuery is scheduled via CRON expressions. 7 |

8 | 9 |

10 | # CRON Expressions 11 |

12 | 13 |

14 | CRON is a time-based job scheduler. Jobs run periodically at fixed times configured using a six-field expression. 15 |

16 | 17 |
{{ include 'www/snippets/CRON.txt' }}
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 |
FieldAllowed valuesSpecial characters
Second0-59* - , /
Minute0-59* - , /
Hour0-23* - , /
Day of month1-31* - , / L W
Month1-12* - , /
Day of week0-6* - , / L #
61 |
62 | 63 |
64 | The days of the week start on Sunday. 65 |
66 | 67 |

68 | # Special characters 69 |

70 | 71 |

72 | The following table shows the special characters present in CRON syntax and their respective meanings. 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 | 112 | 113 | 114 | 115 | 116 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 |
CharacterDescriptionExample
* (all)Every time unitAsterisk in the hour means "every hour"
- (range)Range of values1-5, which is equivalent to 1,2,3,4,5
, (list)List of values2,4,6,8
/ (increment)Step values*/6 in the hour means "every 6 hours", which is equivalent to 0,6,12,18
L (last)Last day of month or last X day of week 109 | In month L means 31 for January or day 28 for February (non-leap years), 110 | in day of week 5L means "the last Friday of the month" 111 |
W (weekday)Weekday (Monday to Friday) nearest the given day 117 | If you specify 15W and 15th is a Saturday the trigger fires on Friday the 14th, 118 | however for 1W and 1th is a Saturday the trigger fires on Monday the 3rd 119 | (because it does not exceed the limit of days of the month), 120 | if the 15h is a Sunday the trigger fires on Monday the 16th 121 | and if 15th is a weekday the trigger fires that day 122 |
# (occurrence)The nth day of the month5#3 means the third Friday of the month
131 |
132 | 133 |

134 | # Supported combinations 135 |

136 | 137 |

138 | CRON allows the combination of subexpressions to produce a more complex CRON expression. 139 | The following list shows the combinations supported by CronQuery: 140 |

141 | 142 |
    143 |
  • 144 | *: 145 | Every time unit. 146 |
  • 147 |
  • 148 | 00-00: 149 | Range of values. 150 |
  • 151 |
  • 152 | */00: 153 | Every time unit skipping a given number of values. 154 |
  • 155 |
  • 156 | 00/00: 157 | Skipping a given number of values starting at specific value. 158 |
  • 159 |
  • 160 | 00-00/00: 161 | Skipping a given number of values in a given range of values. 162 |
  • 163 |
  • 164 | 00,00-00,00/00,00-00/00: 165 | List of values (you can produce variations). 166 | List can only contains single numbers, range, increment and range combined with increment. 167 |
  • 168 |
  • 169 | L (day of month only): 170 | Last day of the month (including weekend). 171 |
  • 172 |
  • 173 | LW (day of month only): 174 | Last weekday (Monday to Friday) of the month. 175 |
  • 176 |
  • 177 | L-00 (day of month only): 178 | Days (1-31) before the last day of month. 179 |
  • 180 |
  • 181 | 00W (day of month only): 182 | Weekday nearest the given day (1-31). 183 |
  • 184 |
  • 185 | 0L (day of week only): 186 | Last day of week (0-6) of the month. 187 |
  • 188 |
  • 189 | 00#0 (day of week only): 190 | The nth (1-5) day of the month. 191 |
  • 192 |
193 | 194 |
195 | Read zero as any numeric value that you pass to the expression. 196 |
197 | 198 |

199 | # Examples 200 |

201 | 202 |

203 | The following table shows some examples of CRON expressions and their respective meanings. 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 |
ExpressionDescription
0 0 12 * * *At 12:00 PM every day
0 0 18 L * *At 6:00 PM on the last day of every month
0 0 18 L-3 * *At 6:00 PM 3 days before the last day of the month
0 0 0 * */6 *At 12:00 AM every 6 months
0 0 0 5,15,25 * *At 12:00 AM, on day 5, 15, and 25 of the month
0 */5 * * * *Every 5 minutes
0 0 8-18/2 * * *Every 2 hours between 8:00 AM and 6:00 PM
0 0 10 * * 1#3At 10:00 AM on the third Monday of every month
249 |
250 | 251 |
252 | crontab.guru is a great tool to help you create CRON expressions. 253 |
254 | 255 | {{/inline}} 256 | 257 | {{/www/page}} 258 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Runner/JobRunnerTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Runner; 26 | 27 | using CronQuery.Mvc.Jobs; 28 | using CronQuery.Tests.Fakes; 29 | using CronQuery.Tests.Fakes.Jobs; 30 | using Microsoft.Extensions.DependencyInjection; 31 | using Xunit; 32 | 33 | public sealed class JobRunnerTest 34 | { 35 | private readonly LoggerFactoryFake _loggerFactory; 36 | private readonly ServiceCollection _serviceCollection; 37 | private readonly JobCollection _jobCollection; 38 | 39 | public JobRunnerTest() 40 | { 41 | _loggerFactory = new LoggerFactoryFake(); 42 | _serviceCollection = new ServiceCollection(); 43 | _jobCollection = new JobCollection(_serviceCollection); 44 | } 45 | 46 | [Fact] 47 | public void ShouldRunJobSuccessfully() 48 | { 49 | var options = new OptionsMonitorFake(JobSuccessful.Options); 50 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 51 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 52 | 53 | jobRunner.Start(); 54 | 55 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 56 | 57 | Assert.True(serviceProvider.GetService()!.Executed); 58 | } 59 | 60 | [Fact] 61 | public void ShouldNotRunStoppedJob() 62 | { 63 | var options = new OptionsMonitorFake(JobStopped.Options); 64 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 65 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 66 | 67 | jobRunner.Start(); 68 | 69 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 70 | 71 | Assert.False(serviceProvider.GetService()!.Executed); 72 | } 73 | 74 | [Fact] 75 | public void ShouldLogErrorForJobNotEnqueued() 76 | { 77 | var options = new OptionsMonitorFake(JobNotEnqueued.Options); 78 | var serviceProvider = _serviceCollection.BuildServiceProvider(); 79 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 80 | 81 | jobRunner.Start(); 82 | 83 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 84 | 85 | Assert.Contains(_loggerFactory.Logger.Messages, message => 86 | message == $"Job {nameof(JobNotEnqueued)} is not in the queue."); 87 | } 88 | 89 | [Fact] 90 | public void ShouldLogJobFail() 91 | { 92 | var options = new OptionsMonitorFake(JobWithError.Options); 93 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 94 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 95 | 96 | jobRunner.Start(); 97 | 98 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 99 | 100 | Assert.Contains(_loggerFactory.Logger.Messages, message => 101 | message == $"Job '{nameof(JobWithError)}' failed during running."); 102 | } 103 | 104 | [Fact] 105 | public void ShouldLogWhenCronIsInvalid() 106 | { 107 | var options = new OptionsMonitorFake(JobBadlyConfigured.Options); 108 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 109 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 110 | 111 | jobRunner.Start(); 112 | 113 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 114 | 115 | Assert.Contains(_loggerFactory.Logger.Messages, message => 116 | message == $"Invalid cron expression for '{nameof(JobBadlyConfigured)}'."); 117 | } 118 | 119 | [Fact] 120 | public void ShouldNotRunJobsWhenRunnerIsOff() 121 | { 122 | var options = new OptionsMonitorFake(JobSuccessful.Options); 123 | options.Change(options => options.Running = false); 124 | 125 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 126 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 127 | 128 | jobRunner.Start(); 129 | 130 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 131 | 132 | Assert.False(serviceProvider.GetService()!.Executed); 133 | } 134 | 135 | [Fact] 136 | public void ShouldAssumeNewConfigurationImmediately() 137 | { 138 | var options = new OptionsMonitorFake(JobSuccessful.Options); 139 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 140 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 141 | 142 | jobRunner.Start(); 143 | 144 | options.Change(options => options.Running = false); 145 | 146 | Task.Delay(1500).GetAwaiter().GetResult(); // Waiting for the job 147 | 148 | Assert.False(serviceProvider.GetService()!.Executed); 149 | } 150 | 151 | [Fact] 152 | public void ShouldNotInstanceWithNullOptions() 153 | { 154 | var serviceProvider = _serviceCollection.BuildServiceProvider(); 155 | 156 | Assert.Throws(() => 157 | { 158 | new JobRunner(null!, _jobCollection, serviceProvider, _loggerFactory); 159 | }); 160 | } 161 | 162 | [Fact] 163 | public void ShouldNotInstanceWithNullJobCollection() 164 | { 165 | var options = new OptionsMonitorFake(JobSuccessful.Options); 166 | var serviceProvider = _serviceCollection.BuildServiceProvider(); 167 | 168 | Assert.Throws(() => 169 | { 170 | new JobRunner(options, null!, serviceProvider, _loggerFactory); 171 | }); 172 | } 173 | 174 | [Fact] 175 | public void ShouldNotInstanceWithNullServiceProvider() 176 | { 177 | var options = new OptionsMonitorFake(JobSuccessful.Options); 178 | 179 | Assert.Throws(() => 180 | { 181 | new JobRunner(options, _jobCollection, null!, _loggerFactory); 182 | }); 183 | } 184 | 185 | [Fact] 186 | public void ShouldNotInstanceWithNullLoggerFactory() 187 | { 188 | var options = new OptionsMonitorFake(JobSuccessful.Options); 189 | var serviceProvider = new ServiceProviderFake(); 190 | 191 | Assert.Throws(() => 192 | { 193 | new JobRunner(options, _jobCollection, serviceProvider, null!); 194 | }); 195 | } 196 | 197 | [Fact] 198 | public async Task ShouldRunJobManually() 199 | { 200 | var options = new OptionsMonitorFake(JobSuccessful.Options); 201 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 202 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 203 | 204 | await jobRunner.RunAsync(nameof(JobSuccessful)); 205 | 206 | Assert.True(serviceProvider.GetService()!.Executed); 207 | } 208 | 209 | [Fact] 210 | public async Task ShouldLogErrorWhenRunManuallyJobNotEnqueued() 211 | { 212 | var options = new OptionsMonitorFake(JobSuccessful.Options); 213 | var serviceProvider = _serviceCollection.BuildServiceProvider(); 214 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 215 | 216 | await jobRunner.RunAsync(nameof(JobNotEnqueued)); 217 | 218 | Assert.Contains(_loggerFactory.Logger.Messages, message => 219 | message == $"Job {nameof(JobNotEnqueued)} is not in the queue."); 220 | } 221 | 222 | [Fact] 223 | public async Task ShouldLogJobFailAfterRunManually() 224 | { 225 | var options = new OptionsMonitorFake(JobWithError.Options); 226 | var serviceProvider = _serviceCollection.AddSingleton().BuildServiceProvider(); 227 | var jobRunner = new JobRunner(options, _jobCollection, serviceProvider, _loggerFactory); 228 | 229 | await jobRunner.RunAsync(nameof(JobWithError)); 230 | 231 | Assert.Contains(_loggerFactory.Logger.Messages, message => 232 | message == $"Job '{nameof(JobWithError)}' failed during running."); 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /docs/www/img/cronquery.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 36 | 38 | 55 | 72 | 73 | 133 | 138 | 146 | 163 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /src/CronQuery.Tests/Unit/Cron/ExpressionWithLTest.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Marx J. Moura 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | namespace CronQuery.Tests.Unit.Cron; 26 | 27 | using CronQuery.Cron; 28 | using Xunit; 29 | 30 | public sealed class ExpressionWithLTest 31 | { 32 | [Fact] 33 | public void ShouldGetNextLastDayInJanuary() 34 | { 35 | var expression = new CronExpression("* * * L * *"); 36 | var current = new DateTime(2018, 01, 01, 23, 59, 59); 37 | var expected = new DateTime(2018, 01, 31, 00, 00, 00); 38 | 39 | Assert.Equal(expected, expression.Next(current)); 40 | } 41 | 42 | [Fact] 43 | public void ShouldGetNextLastDayFromJanuary() 44 | { 45 | var expression = new CronExpression("* * * L * *"); 46 | var current = new DateTime(2018, 01, 31, 23, 59, 59); 47 | var expected = new DateTime(2018, 02, 28, 00, 00, 00); 48 | 49 | Assert.Equal(expected, expression.Next(current)); 50 | } 51 | 52 | [Fact] 53 | public void ShouldGetNextLastDayFromJanuaryInLeapYear() 54 | { 55 | var expression = new CronExpression("* * * L * *"); 56 | var current = new DateTime(2020, 01, 31, 23, 59, 59); 57 | var expected = new DateTime(2020, 02, 29, 00, 00, 00); 58 | 59 | Assert.Equal(expected, expression.Next(current)); 60 | } 61 | 62 | [Fact] 63 | public void ShouldGetNextLastDayInFebruary() 64 | { 65 | var expression = new CronExpression("* * * L * *"); 66 | var current = new DateTime(2018, 02, 01, 23, 59, 59); 67 | var expected = new DateTime(2018, 02, 28, 00, 00, 00); 68 | 69 | Assert.Equal(expected, expression.Next(current)); 70 | } 71 | 72 | [Fact] 73 | public void ShouldGetNextLastDayInFebruaryInLeapYear() 74 | { 75 | var expression = new CronExpression("* * * L * *"); 76 | var current = new DateTime(2020, 02, 01, 23, 59, 59); 77 | var expected = new DateTime(2020, 02, 29, 00, 00, 00); 78 | 79 | Assert.Equal(expected, expression.Next(current)); 80 | } 81 | 82 | [Fact] 83 | public void ShouldGetNextLastDayFromFebruary() 84 | { 85 | var expression = new CronExpression("* * * L * *"); 86 | var current = new DateTime(2018, 02, 28, 23, 59, 59); 87 | var expected = new DateTime(2018, 03, 31, 00, 00, 00); 88 | 89 | Assert.Equal(expected, expression.Next(current)); 90 | } 91 | 92 | [Fact] 93 | public void ShouldGetNextLastDayFromFebruaryInLeapYear() 94 | { 95 | var expression = new CronExpression("* * * L * *"); 96 | var current = new DateTime(2020, 02, 29, 23, 59, 59); 97 | var expected = new DateTime(2020, 03, 31, 00, 00, 00); 98 | 99 | Assert.Equal(expected, expression.Next(current)); 100 | } 101 | 102 | [Fact] 103 | public void ShouldGetNextLastDayInMarch() 104 | { 105 | var expression = new CronExpression("* * * L * *"); 106 | var current = new DateTime(2018, 03, 01, 23, 59, 59); 107 | var expected = new DateTime(2018, 03, 31, 00, 00, 00); 108 | 109 | Assert.Equal(expected, expression.Next(current)); 110 | } 111 | 112 | [Fact] 113 | public void ShouldGetNextLastDayFromMarch() 114 | { 115 | var expression = new CronExpression("* * * L * *"); 116 | var current = new DateTime(2018, 03, 31, 23, 59, 59); 117 | var expected = new DateTime(2018, 04, 30, 00, 00, 00); 118 | 119 | Assert.Equal(expected, expression.Next(current)); 120 | } 121 | 122 | [Fact] 123 | public void ShouldGetNextLastDayInApril() 124 | { 125 | var expression = new CronExpression("* * * L * *"); 126 | var current = new DateTime(2018, 04, 01, 23, 59, 59); 127 | var expected = new DateTime(2018, 04, 30, 00, 00, 00); 128 | 129 | Assert.Equal(expected, expression.Next(current)); 130 | } 131 | 132 | [Fact] 133 | public void ShouldGetNextLastDayFromApril() 134 | { 135 | var expression = new CronExpression("* * * L * *"); 136 | var current = new DateTime(2018, 04, 30, 23, 59, 59); 137 | var expected = new DateTime(2018, 05, 31, 00, 00, 00); 138 | 139 | Assert.Equal(expected, expression.Next(current)); 140 | } 141 | 142 | [Fact] 143 | public void ShouldGetNextLastDayInMay() 144 | { 145 | var expression = new CronExpression("* * * L * *"); 146 | var current = new DateTime(2018, 05, 01, 23, 59, 59); 147 | var expected = new DateTime(2018, 05, 31, 00, 00, 00); 148 | 149 | Assert.Equal(expected, expression.Next(current)); 150 | } 151 | 152 | [Fact] 153 | public void ShouldGetNextLastDayFromMay() 154 | { 155 | var expression = new CronExpression("* * * L * *"); 156 | var current = new DateTime(2018, 05, 31, 23, 59, 59); 157 | var expected = new DateTime(2018, 06, 30, 00, 00, 00); 158 | 159 | Assert.Equal(expected, expression.Next(current)); 160 | } 161 | 162 | [Fact] 163 | public void ShouldGetNextLastDayInJune() 164 | { 165 | var expression = new CronExpression("* * * L * *"); 166 | var current = new DateTime(2018, 06, 01, 23, 59, 59); 167 | var expected = new DateTime(2018, 06, 30, 00, 00, 00); 168 | 169 | Assert.Equal(expected, expression.Next(current)); 170 | } 171 | 172 | [Fact] 173 | public void ShouldGetNextLastDayFromJune() 174 | { 175 | var expression = new CronExpression("* * * L * *"); 176 | var current = new DateTime(2018, 06, 30, 23, 59, 59); 177 | var expected = new DateTime(2018, 07, 31, 00, 00, 00); 178 | 179 | Assert.Equal(expected, expression.Next(current)); 180 | } 181 | 182 | [Fact] 183 | public void ShouldGetNextLastDayInJuly() 184 | { 185 | var expression = new CronExpression("* * * L * *"); 186 | var current = new DateTime(2018, 07, 01, 23, 59, 59); 187 | var expected = new DateTime(2018, 07, 31, 00, 00, 00); 188 | 189 | Assert.Equal(expected, expression.Next(current)); 190 | } 191 | 192 | [Fact] 193 | public void ShouldGetNextLastDayFromJuly() 194 | { 195 | var expression = new CronExpression("* * * L * *"); 196 | var current = new DateTime(2018, 07, 31, 23, 59, 59); 197 | var expected = new DateTime(2018, 08, 31, 00, 00, 00); 198 | 199 | Assert.Equal(expected, expression.Next(current)); 200 | } 201 | 202 | [Fact] 203 | public void ShouldGetNextLastDayInAugust() 204 | { 205 | var expression = new CronExpression("* * * L * *"); 206 | var current = new DateTime(2018, 08, 01, 23, 59, 59); 207 | var expected = new DateTime(2018, 08, 31, 00, 00, 00); 208 | 209 | Assert.Equal(expected, expression.Next(current)); 210 | } 211 | 212 | [Fact] 213 | public void ShouldGetNextLastDayFromAugust() 214 | { 215 | var expression = new CronExpression("* * * L * *"); 216 | var current = new DateTime(2018, 08, 31, 23, 59, 59); 217 | var expected = new DateTime(2018, 09, 30, 00, 00, 00); 218 | 219 | Assert.Equal(expected, expression.Next(current)); 220 | } 221 | 222 | [Fact] 223 | public void ShouldGetNextLastDayInSeptember() 224 | { 225 | var expression = new CronExpression("* * * L * *"); 226 | var current = new DateTime(2018, 09, 01, 23, 59, 59); 227 | var expected = new DateTime(2018, 09, 30, 00, 00, 00); 228 | 229 | Assert.Equal(expected, expression.Next(current)); 230 | } 231 | 232 | [Fact] 233 | public void ShouldGetNextLastDayFromSeptember() 234 | { 235 | var expression = new CronExpression("* * * L * *"); 236 | var current = new DateTime(2018, 09, 30, 23, 59, 59); 237 | var expected = new DateTime(2018, 10, 31, 00, 00, 00); 238 | 239 | Assert.Equal(expected, expression.Next(current)); 240 | } 241 | 242 | [Fact] 243 | public void ShouldGetNextLastDayInOctober() 244 | { 245 | var expression = new CronExpression("* * * L * *"); 246 | var current = new DateTime(2018, 10, 01, 23, 59, 59); 247 | var expected = new DateTime(2018, 10, 31, 00, 00, 00); 248 | 249 | Assert.Equal(expected, expression.Next(current)); 250 | } 251 | 252 | [Fact] 253 | public void ShouldGetNextLastDayFromOctober() 254 | { 255 | var expression = new CronExpression("* * * L * *"); 256 | var current = new DateTime(2018, 10, 31, 23, 59, 59); 257 | var expected = new DateTime(2018, 11, 30, 00, 00, 00); 258 | 259 | Assert.Equal(expected, expression.Next(current)); 260 | } 261 | 262 | [Fact] 263 | public void ShouldGetNextLastDayInNovember() 264 | { 265 | var expression = new CronExpression("* * * L * *"); 266 | var current = new DateTime(2018, 11, 01, 23, 59, 59); 267 | var expected = new DateTime(2018, 11, 30, 00, 00, 00); 268 | 269 | Assert.Equal(expected, expression.Next(current)); 270 | } 271 | 272 | [Fact] 273 | public void ShouldGetNextLastDayFromNovember() 274 | { 275 | var expression = new CronExpression("* * * L * *"); 276 | var current = new DateTime(2018, 11, 30, 23, 59, 59); 277 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 278 | 279 | Assert.Equal(expected, expression.Next(current)); 280 | } 281 | 282 | [Fact] 283 | public void ShouldGetNextLastDayInDecember() 284 | { 285 | var expression = new CronExpression("* * * L * *"); 286 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 287 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 288 | 289 | Assert.Equal(expected, expression.Next(current)); 290 | } 291 | 292 | [Fact] 293 | public void ShouldGetNextLastDayFromDecember() 294 | { 295 | var expression = new CronExpression("* * * L * *"); 296 | var current = new DateTime(2018, 12, 31, 23, 59, 59); 297 | var expected = new DateTime(2019, 01, 31, 00, 00, 00); 298 | 299 | Assert.Equal(expected, expression.Next(current)); 300 | } 301 | 302 | [Fact] 303 | public void ShouldGetNextDayBeforeLastDayOfMonth() 304 | { 305 | var expression = new CronExpression("* * * L-3 * *"); 306 | var current = new DateTime(2018, 12, 01, 23, 59, 59); 307 | var expected = new DateTime(2018, 12, 28, 00, 00, 00); 308 | 309 | Assert.Equal(expected, expression.Next(current)); 310 | } 311 | 312 | [Fact] 313 | public void ShouldGetNextLastSunday() 314 | { 315 | var expression = new CronExpression("* * * * * 0L"); 316 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 317 | var expected = new DateTime(2018, 12, 30, 00, 00, 00); 318 | 319 | Assert.Equal(expected, expression.Next(current)); 320 | } 321 | 322 | [Fact] 323 | public void ShouldGetNextLastMonday() 324 | { 325 | var expression = new CronExpression("* * * * * 1L"); 326 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 327 | var expected = new DateTime(2018, 12, 31, 00, 00, 00); 328 | 329 | Assert.Equal(expected, expression.Next(current)); 330 | } 331 | 332 | [Fact] 333 | public void ShouldGetNextLastTuesday() 334 | { 335 | var expression = new CronExpression("* * * * * 2L"); 336 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 337 | var expected = new DateTime(2019, 01, 29, 00, 00, 00); 338 | 339 | Assert.Equal(expected, expression.Next(current)); 340 | } 341 | 342 | [Fact] 343 | public void ShouldGetNextLastWednesday() 344 | { 345 | var expression = new CronExpression("* * * * * 3L"); 346 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 347 | var expected = new DateTime(2018, 12, 26, 00, 00, 00); 348 | 349 | Assert.Equal(expected, expression.Next(current)); 350 | } 351 | 352 | [Fact] 353 | public void ShouldGetNextLastThursday() 354 | { 355 | var expression = new CronExpression("* * * * * 4L"); 356 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 357 | var expected = new DateTime(2018, 12, 27, 00, 00, 00); 358 | 359 | Assert.Equal(expected, expression.Next(current)); 360 | } 361 | 362 | [Fact] 363 | public void ShouldGetNextLastFriday() 364 | { 365 | var expression = new CronExpression("* * * * * 5L"); 366 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 367 | var expected = new DateTime(2018, 12, 28, 00, 00, 00); 368 | 369 | Assert.Equal(expected, expression.Next(current)); 370 | } 371 | 372 | [Fact] 373 | public void ShouldGetNextLastSaturday() 374 | { 375 | var expression = new CronExpression("* * * * * 6L"); 376 | var current = new DateTime(2018, 12, 25, 23, 59, 59); 377 | var expected = new DateTime(2018, 12, 29, 00, 00, 00); 378 | 379 | Assert.Equal(expected, expression.Next(current)); 380 | } 381 | } 382 | --------------------------------------------------------------------------------