{"_id":"56a0bda13697d80d002ac606","project":"54774d9af3736008009e9e0e","__v":21,"version":{"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","__v":20,"createdAt":"2016-01-21T11:14:38.131Z","releaseDate":"2016-01-21T11:14:38.131Z","categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Sockets","version_clean":"1.1.0","version":"1.1"},"parentDoc":null,"user":"54e341c4ce1b4017000dffef","category":{"_id":"56b098a43a5b810d00745e3f","pages":["56b09b3b135ec20d00baf946"],"project":"54774d9af3736008009e9e0e","__v":1,"version":"56a0bd9e3697d80d002ac5e7","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-02T11:53:08.831Z","from_sync":false,"order":14,"slug":"sockets","title":"Sockets"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-03-16T08:10:53.675Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"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]","excerpt":"What's a Schedule Socket and how does it work?","slug":"schedules","type":"basic","title":"Schedule Sockets"}

Schedule Sockets

What's a Schedule Socket and how does it work?

## Chapter Contents: 1. [Overview](#overview) 2. [Creating a Schedule Socket from the Dashboard](#section-creating-schedule-socket-from-the-dashboard) 3. [Creating Schedule Socket with `interval_sec` parameter](#section-creating-schedule-socket-with-interval_sec-parameter) 4. [Creating Schedule Socket with a `crontab` parameter](#section-creating-schedule-socket-with-a-crontab-parameter) [block:api-header] { "type": "basic", "title": "Overview" } [/block] Schedule 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). There are two ways of setting the desired time interval: + [interval_sec](#section-creating-schedule-socket-with-interval_sec-parameter) + [crontab](#section-creating-schedule-socket-with-a-crontab-parameter) We'll get into the details of setting a Schedule in code with these two options below, but before that - we will learn how to add a schedule using our [Dashboard](https://dashboard.syncano.io). ## Creating Schedule Socket from the Dashboard Schedules can be found under the `Tasks` tab on the Navigation Bar in Syncano's [Dashboard](https://dashboard.syncano.io). [block:image] { "images": [ { "image": [ "https://files.readme.io/URgj180GT4Gl3ksWbmqf_Add_schedule_01.png", "Add_schedule_01.png", "1279", "653", "#223e68", "" ] } ] } [/block] [block:image] { "images": [ { "image": [ "https://files.readme.io/GYVKF37tQ9yB7G4xZQhi_Add_schedule_02.png", "Add_schedule_02.png", "1279", "657", "#6c84a4", "" ] } ] } [/block] As you can see, you have quite a few options to choose from. If you need even more flexibility, read further, and see what can you accomplish using `interval_sec` and `crontab` parameters directly! ## Creating Schedule Socket with `interval_sec` parameter `interval_sec` is simply an interval (defined in seconds) at which the Script will run. [block:callout] { "type": "info", "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." } [/block] So let's say we'd like our Snippet Script to run every minute. This is how a Schedule creation call would look like: [block:code] { "codes": [ { "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/\"", "language": "curl" }, { "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", "language": "python" }, { "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});", "language": "javascript" }, { "code": "// N/A", "language": "java", "name": "Android" }, { "code": "// N/A", "language": "objectivec" }, { "code": "// N/A", "language": "objectivec", "name": "Swift" }, { "code": "# Coming soon!", "language": "ruby" }, { "code": "// NOT AVAILABLE", "language": "csharp", "name": "Unity" } ] } [/block] ## Creating Schedule Socket with a `crontab` parameter Crontab 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: [block:parameters] { "data": { "h-0": "", "h-1": "Field 1", "h-2": "Field 2", "h-3": "Field 3", "h-4": "Field 4", "h-5": "Field 5", "0-0": "Field type:", "0-1": "Minutes", "0-2": "Hours", "0-3": "Day of the month", "0-4": "Month", "0-5": "Day of the week", "1-0": "Allowed values:", "1-1": "0-59", "1-2": "0-23", "1-3": "1-31", "1-4": "1-12", "1-5": "0-6", "2-0": "Allowed special characters", "2-1": "`* , -`", "2-2": "`* , -`", "2-3": "`* , -`", "2-4": "`* , -`", "2-5": "`* , -`" }, "cols": 6, "rows": 2 } [/block] There are couple of special characters that you can use when constructing cron expressions: [block:parameters] { "data": { "h-0": "Character", "h-1": "Usage", "0-0": "Comma \n`,`", "1-0": "Hypen \n`-`", "2-0": "Asterisk \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.", "1-1": "Hyphens define ranges. For example, 12-16 in second field indicates every hour between 12:00 and 16:00, inclusive.", "2-1": "Stands for \"first-last\". Cron expression with `*` in first field will run every minute, with `*` in second field, every hour etc." }, "cols": 2, "rows": 3 } [/block] Couple of crontab examples: [block:parameters] { "data": { "0-0": "`5 0 * * *`", "1-0": "`0 9-18 * * *`", "1-1": "Schedule Socket will run every day at full hour from 9:00AM to 6:00PM", "2-0": "`0 9-18 * * 1-5`", "2-1": "Schedule Socket will run during weekdays at full hour from 9:00AM to 6:00PM", "0-1": "Schedule Socket will run every day, five minutes after midnight" }, "cols": 2, "rows": 3 } [/block] [block:image] { "images": [ { "image": [ "https://files.readme.io/8f9b512-Screen_Shot_2016-08-16_at_18.59.16.png", "Screen Shot 2016-08-16 at 18.59.16.png", 1007, 497, "#f7f7f7" ] } ] } [/block] [block:callout] { "type": "info", "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." } [/block] Putting it all together, a Schedule Socket with crontab parameter would look like this: [block:code] { "codes": [ { "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/\"", "language": "curl" }, { "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 * * *\")", "language": "python" }, { "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});", "language": "javascript" }, { "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});", "language": "javascript", "name": "Node.js" }, { "code": "// N/A", "language": "java", "name": "Android" }, { "code": "// N/A", "language": "objectivec" }, { "code": "// N/A", "language": "objectivec", "name": "Swift" }, { "code": "# Coming soon!", "language": "ruby" }, { "code": "// NOT AVAILABLE", "language": "csharp", "name": "Unity" } ] } [/block] So in the example above a Snippet Script with an id = 4 would run every day at midnight. [block:callout] { "type": "info", "title": "Learn More", "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)." } [/block]