{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Schedule Sockets","type":"basic","slug":"schedules","excerpt":"What's a Schedule Socket and how does it work?","body":"## Chapter Contents:\n\n1. [Overview](#overview)\n2. [Creating a Schedule Socket from the Dashboard](#section-creating-schedule-socket-from-the-dashboard)\n3. [Creating Schedule Socket with `interval_sec` parameter](#section-creating-schedule-socket-with-interval_sec-parameter)\n4. [Creating Schedule Socket with a `crontab` parameter](#section-creating-schedule-socket-with-a-crontab-parameter)\n[block:api-header]\n{\n \"type\": \"basic\",\n \"title\": \"Overview\"\n}\n[/block]\nSchedule Sockets are one of the available ways of running your Snippet Scripts. Thanks to Schedules you can execute Scripts at a desired date (e.g. every Thursday at 9PM) and/or time interval (e.g. every 5 minutes). \n\nThere are two ways of setting the desired time interval:\n+ [interval_sec](#section-creating-schedule-socket-with-interval_sec-parameter)\n+ [crontab](#section-creating-schedule-socket-with-a-crontab-parameter)\n\nWe'll get into the details of setting a Schedule \nin code with these two options below, but before that - we will learn how to add a schedule using our [Dashboard](https://dashboard.syncano.io).\n\n## Creating Schedule Socket from the Dashboard\n\nSchedules can be found under the `Tasks` tab on the Navigation Bar in Syncano's [Dashboard](https://dashboard.syncano.io).\n[block:image]\n{\n \"images\": [\n {\n \"image\": [\n \"https://files.readme.io/URgj180GT4Gl3ksWbmqf_Add_schedule_01.png\",\n \"Add_schedule_01.png\",\n \"1279\",\n \"653\",\n \"#223e68\",\n \"\"\n ]\n }\n ]\n}\n[/block]\n\n[block:image]\n{\n \"images\": [\n {\n \"image\": [\n \"https://files.readme.io/GYVKF37tQ9yB7G4xZQhi_Add_schedule_02.png\",\n \"Add_schedule_02.png\",\n \"1279\",\n \"657\",\n \"#6c84a4\",\n \"\"\n ]\n }\n ]\n}\n[/block]\nAs you can see, you have quite a few options to choose from. \nIf you need even more flexibility, read further, and see what can you accomplish using `interval_sec` and `crontab` parameters directly!\n\n## Creating Schedule Socket with `interval_sec` parameter\n\n`interval_sec` is simply an interval (defined in seconds) at which the Script will run. \n[block:callout]\n{\n \"type\": \"info\",\n \"body\": \"`interval_sec` has a valid range from `30` to `86400`s (from half a minute to 24 hours), so a daily Script execution is a maximum in this case. If you need more complex solution, use cron tables.\"\n}\n[/block]\nSo let's say we'd like our Snippet Script to run every minute. This is how a Schedule creation call would look like:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"curl -X POST \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\n-H \\\"Content-Type: application/json\\\" \\\\\\n-d '{\\\"label\\\":\\\"every 60 sec\\\", \\n \\\"script\\\": script_id, \\n \\\"interval_sec\\\": 60}' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/instance_name/schedules/\\\"\",\n \"language\": \"curl\"\n },\n {\n \"code\": \"import syncano\\nfrom syncano.models import Schedule\\n\\nsyncano.connect(api_key='API_KEY')\\n\\nSchedule.please.create(\\n instance_name=\\\"INSTANCE_NAME\\\",\\n label=\\\"every 60 sec\\\",\\n script=\\\"CODEBOX_ID\\\",\\n interval_sec=60\\n)\\n\",\n \"language\": \"python\"\n },\n {\n \"code\": \"var Syncano = require(\\\"syncano\\\"); // CommonJS\\nvar connection = Syncano({accountKey: \\\"ACCOUNT_KEY\\\"});\\nvar Schedule = connection.Schedule;\\n\\nvar options = {\\n\\tlabel: \\\"every 60 sec\\\", // Schedule label\\n\\tscript: 4, // Script Id to run\\n\\tinterval_sec: \\\"60\\\", // how often (in seconds) schedule should run\\n instanceName: \\\"INSTANCE_NAME\\\"\\n};\\n\\nSchedule.please().create(options).then(function(schedule) {\\n\\tconsole.log(\\\"schedule\\\", schedule);\\n});\",\n \"language\": \"javascript\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"java\",\n \"name\": \"Android\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"objectivec\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"objectivec\",\n \"name\": \"Swift\"\n },\n {\n \"code\": \"# Coming soon!\",\n \"language\": \"ruby\"\n },\n {\n \"code\": \"// NOT AVAILABLE\",\n \"language\": \"csharp\",\n \"name\": \"Unity\"\n }\n ]\n}\n[/block]\n## Creating Schedule Socket with a `crontab` parameter\n\nCrontab is a file that contains instructions that say \"run this command at this time on this date\". These instructions are passed in form of a cron expression which is a string comprising five fields separated by whitespace:\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"\",\n \"h-1\": \"Field 1\",\n \"h-2\": \"Field 2\",\n \"h-3\": \"Field 3\",\n \"h-4\": \"Field 4\",\n \"h-5\": \"Field 5\",\n \"0-0\": \"Field type:\",\n \"0-1\": \"Minutes\",\n \"0-2\": \"Hours\",\n \"0-3\": \"Day of the month\",\n \"0-4\": \"Month\",\n \"0-5\": \"Day of the week\",\n \"1-0\": \"Allowed values:\",\n \"1-1\": \"0-59\",\n \"1-2\": \"0-23\",\n \"1-3\": \"1-31\",\n \"1-4\": \"1-12\",\n \"1-5\": \"0-6\",\n \"2-0\": \"Allowed special characters\",\n \"2-1\": \"`* , -`\",\n \"2-2\": \"`* , -`\",\n \"2-3\": \"`* , -`\",\n \"2-4\": \"`* , -`\",\n \"2-5\": \"`* , -`\"\n },\n \"cols\": 6,\n \"rows\": 2\n}\n[/block]\nThere are couple of special characters that you can use when constructing cron expressions:\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Character\",\n \"h-1\": \"Usage\",\n \"0-0\": \"Comma \\n`,`\",\n \"1-0\": \"Hypen \\n`-`\",\n \"2-0\": \"Asterisk \\n`*`\",\n \"0-1\": \"Commas are used to separate items of a list. For example, using \\\"1,3,5\\\" in the 5th field (day of week) means Mondays, Wednesdays and Fridays.\",\n \"1-1\": \"Hyphens define ranges. For example, 12-16 in second field indicates every hour between 12:00 and 16:00, inclusive.\",\n \"2-1\": \"Stands for \\\"first-last\\\". Cron expression with `*` in first field will run every minute, with `*` in second field, every hour etc.\"\n },\n \"cols\": 2,\n \"rows\": 3\n}\n[/block]\nCouple of crontab examples:\n[block:parameters]\n{\n \"data\": {\n \"0-0\": \"`5 0 * * *`\",\n \"1-0\": \"`0 9-18 * * *`\",\n \"1-1\": \"Schedule Socket will run every day at full hour from 9:00AM to 6:00PM\",\n \"2-0\": \"`0 9-18 * * 1-5`\",\n \"2-1\": \"Schedule Socket will run during weekdays at full hour from 9:00AM to 6:00PM\",\n \"0-1\": \"Schedule Socket will run every day, five minutes after midnight\"\n },\n \"cols\": 2,\n \"rows\": 3\n}\n[/block]\n\n[block:image]\n{\n \"images\": [\n {\n \"image\": [\n \"https://files.readme.io/8f9b512-Screen_Shot_2016-08-16_at_18.59.16.png\",\n \"Screen Shot 2016-08-16 at 18.59.16.png\",\n 1007,\n 497,\n \"#f7f7f7\"\n ]\n }\n ]\n}\n[/block]\n\n[block:callout]\n{\n \"type\": \"info\",\n \"body\": \"If you'd like to know more about crontabs, [UNIX man pages](http://man7.org/linux/man-pages/man5/crontab.5.html) are a great place to start.\"\n}\n[/block]\nPutting it all together, a Schedule Socket with crontab parameter would look like this:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"curl -X POST \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\n-H \\\"Content-Type: application/json\\\" \\\\\\n-d '{\\\"label\\\":\\\"every 60 sec\\\", \\n \\\"script\\\": script_id, \\n \\\"crontab\\\": \\\"0 9 * * *\\\"}' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/instance_name/schedules/\\\"\",\n \"language\": \"curl\"\n },\n {\n \"code\": \"import syncano\\nfrom syncano.models import Schedule\\n\\nsyncano.connect(api_key='API_KEY')\\n\\nSchedule.please.create(\\n instance_name=\\\"INSTANCE_NAME\\\",\\n label=\\\"every 60 sec\\\",\\n script=\\\"SCRIPT_ID\\\",\\n crontab=\\\"0 9-18 * * *\\\")\",\n \"language\": \"python\"\n },\n {\n \"code\": \"var Syncano = require(\\\"syncano\\\"); // CommonJS\\nvar connection = Syncano({accountKey: \\\"ACCOUNT_KEY\\\"});\\nvar Schedule = connection.Schedule;\\n\\n\\nvar options = {\\n\\tlabel: \\\"every day\\\", // Schedule label\\n\\tscript: 4, // Script Id to run\\n\\tcrontab: \\\"0 0 * * 0\\\", // when schedule should run (cron syntax),\\n instanceName: \\\"INSTANCE_NAME\\\"\\n};\\n\\nSchedule.please().create(options).then(function(schedule) {\\n\\tconsole.log(\\\"schedule\\\", schedule);\\n});\",\n \"language\": \"javascript\"\n },\n {\n \"code\": \"var syncano = new Syncano();\\nsyncano.connect(\\\"ACCOUNT_KEY\\\");\\nsyncano.setInstance(\\\"instanceName\\\");\\n\\nvar params = {\\n\\tlabel: \\\"every day\\\", // Schedule label\\n\\tcodebox: 4, // CodeBox Id to run\\n\\tcrontab: \\\"0 0 * * 0\\\" // when schedule should run (cron syntax)\\n}\\n\\nsyncano.Schedules.create(params).then(function (output) {\\n\\tconsole.log(output);\\n}, function (err) {\\n\\tconsole.log(\\\"ERR :\\\" + err)\\n});\",\n \"language\": \"javascript\",\n \"name\": \"Node.js\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"java\",\n \"name\": \"Android\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"objectivec\"\n },\n {\n \"code\": \"// N/A\",\n \"language\": \"objectivec\",\n \"name\": \"Swift\"\n },\n {\n \"code\": \"# Coming soon!\",\n \"language\": \"ruby\"\n },\n {\n \"code\": \"// NOT AVAILABLE\",\n \"language\": \"csharp\",\n \"name\": \"Unity\"\n }\n ]\n}\n[/block]\nSo in the example above a Snippet Script with an id = 4 would run every day at midnight.\n[block:callout]\n{\n \"type\": \"info\",\n \"title\": \"Learn More\",\n \"body\": \"For more details about the available methods for schedules, visit the [Schedule API reference](http://docs.syncano.com/v0.1.1/docs/schedules-list).\"\n}\n[/block]","updates":[],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56a0bda13697d80d002ac606","githubsync":"","project":"54774d9af3736008009e9e0e","createdAt":"2015-03-16T08:10:53.675Z","__v":21,"version":{"version":"1.1","version_clean":"1.1.0","codename":"Sockets","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","releaseDate":"2016-01-21T11:14:38.131Z","__v":20,"createdAt":"2016-01-21T11:14:38.131Z"},"parentDoc":null,"user":"54e341c4ce1b4017000dffef","category":{"sync":{"isSync":false,"url":""},"pages":["56b09b3b135ec20d00baf946"],"title":"Sockets","slug":"sockets","order":14,"from_sync":false,"reference":false,"_id":"56b098a43a5b810d00745e3f","project":"54774d9af3736008009e9e0e","__v":1,"createdAt":"2016-02-02T11:53:08.831Z","version":"56a0bd9e3697d80d002ac5e7"}}
Schedule Sockets
What's a Schedule Socket and how does it work?